简体   繁体   中英

JavaScript moment format date string

I am using the Moment JavaScript library to take a date from my jQuery UI date picker. 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 . I need to submit this date in an XHR request using the format YYYYmmdd so the example above would turn into 20131217 . I tried using Moment like this:

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. 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.

you can use the format function:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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