简体   繁体   English

JavaScript时刻格式日期字符串

[英]JavaScript moment format date string

I am using the Moment JavaScript library to take a date from my jQuery UI date picker. 我正在使用Moment JavaScript库从我的jQuery UI日期选择器中获取日期。 The date picker is currently using a format of mm/dd/YYYY in order to display a value to a user, so today would show as: 12/17/2013 . 日期选择器当前使用的格式为mm/dd/YYYY ,以便向用户显示值,因此今天显示为: 12/17/2013 I need to submit this date in an XHR request using the format YYYYmmdd so the example above would turn into 20131217 . 我需要使用格式YYYYmmdd在XHR请求中提交此日期,因此上面的示例将变为20131217 I tried using Moment like this: 我试过像这样使用Moment:

var t_date = moment($("#datepicker").attr('value').replace('/', '-'), "MM-DD-YYYY");

The result for t_date however, comes out to be a large double, almost like a unix timestamp. 然而, t_date的结果是一个大的双倍,几乎就像一个unix时间戳。 At that point, I'm not entirely sure how to have Moment convert it to the string result I need above so I can embed it into a RESTful URL for my XHR request. 那时,我不完全确定如何让Moment将其转换为我上面需要的字符串结果,这样我就可以将它嵌入到我的XHR请求的RESTful URL中。

you can use the format function: 你可以使用format功能:

var t_date = moment($("#datepicker").attr('value').replace('/', '-'),'MM-DD-YYYY')
                     .format('YYYYMMDD');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM