Javascript 更改Yahoo UI中日期的格式

Javascript 更改Yahoo UI中日期的格式,javascript,yui,Javascript,Yui,我使用Yahoo UI中的日历,如下所示: calDate=(calDate.getMonth()+1)+ “/”+calDate.getDate()+“/”+ calDate.getFullYear() 这将显示MMDDYYYY格式 我想将此格式更改为YYYY-MM-DD,以便将其插入MySQL数据库。 我将代码更改为: calDate=calDate.getFullYear()+'-' +(calDate.getMonth()+1)+'-'+calDate.getDate() 它起作用了,但

我使用Yahoo UI中的日历,如下所示:

calDate=(calDate.getMonth()+1)+ “/”+calDate.getDate()+“/”+ calDate.getFullYear()

这将显示
MMDDYYYY
格式

我想将此格式更改为
YYYY-MM-DD
,以便将其插入MySQL数据库。 我将代码更改为:

calDate=calDate.getFullYear()+'-' +(calDate.getMonth()+1)+'-'+calDate.getDate()


它起作用了,但现在的问题是,当我想更改所选日期时,日历不显示日期,而是显示
NAN

您应该在将日期插入服务器端之前更改其格式,让客户端拥有它想要的格式,然后再更改日期。

您应该在将日期插入服务器端之前更改其格式,让客户端拥有它想要的格式,然后再更改日期。

通常最好将日期作为unix时间戳传递到后端,并在数据库中进行转换。在为服务器构建POST数据的代码中,可以传递如下内容:

var t = calDate.getTime()/1000; alert(YAHOO.util.Date.format(calDate, {format: "%Y-%m-%d" }));
比调用
getFullYear、getMonth
getDate
容易得多,通常最好将日期作为unix时间戳传递到后端,并在数据库中转换。在为服务器构建POST数据的代码中,可以传递如下内容:

var t = calDate.getTime()/1000; alert(YAHOO.util.Date.format(calDate, {format: "%Y-%m-%d" }));
比调用
getFullYear、getMonth
getDate

容易得多bluesmoon还就日期格式编写了两篇关于YUIBlog的优秀文章:


  • -Eric

    bluesmoon也在YUIBlog上写了两篇关于日期格式的优秀文章:

  • -埃里克