admin管理员组文章数量:1134600
I need to take the date value from jquery datepicker turn it into string format "MM/dd/yyyy" so it can do the right ajax post. When the page loads or upon changing the datepicker, a jquery ajax call is made.
I have this code:
var sTimestamp =
moment($("#start_ts").datepicker("getDate")).format("MM/dd/yyyy");
But it doesn't turn it into "MM/dd/yyyy". When I use fiddler to check what is sent down the wire, this is the body:
startTimestamp=03%2FTh%2Fyyyy&endTimestamp=03%2FTh%2Fyyyy&pageSize=50&pageNum=0
If I use the compose in fiddler and change the body to:
startTimestamp=03/13/2013&endTimestamp=03/14/2013&pageSize=50&pageNum=0
I get the right response. So, my question is, is there a way to take a date object and format it to a string "MM/dd/yyyy" using moment.js? Or is there something wrong with the way I get the date from datepicker?
Btw, I am assuming that datepicker.getDate returns a date object since that's what the jQuery docs tell me.
Thank you,
I need to take the date value from jquery datepicker turn it into string format "MM/dd/yyyy" so it can do the right ajax post. When the page loads or upon changing the datepicker, a jquery ajax call is made.
I have this code:
var sTimestamp =
moment($("#start_ts").datepicker("getDate")).format("MM/dd/yyyy");
But it doesn't turn it into "MM/dd/yyyy". When I use fiddler to check what is sent down the wire, this is the body:
startTimestamp=03%2FTh%2Fyyyy&endTimestamp=03%2FTh%2Fyyyy&pageSize=50&pageNum=0
If I use the compose in fiddler and change the body to:
startTimestamp=03/13/2013&endTimestamp=03/14/2013&pageSize=50&pageNum=0
I get the right response. So, my question is, is there a way to take a date object and format it to a string "MM/dd/yyyy" using moment.js? Or is there something wrong with the way I get the date from datepicker?
Btw, I am assuming that datepicker.getDate returns a date object since that's what the jQuery docs tell me.
Thank you,
Share Improve this question asked Mar 14, 2013 at 14:19 oky_sabenioky_sabeni 7,82215 gold badges66 silver badges89 bronze badges5 Answers
Reset to default 172StartDate = moment(StartDate).format('MM-YYYY');
...and MySQL date format:
StartDate = moment(StartDate).format('YYYY-MM-DD');
I think you just have incorrect casing in the format string. According to the documentation this should work for you: MM/DD/YYYY
moment.js documentation
Use:
date.format("MM/DD/YYYY") or date.format("MM-DD-YYYY")}
Other Supported formats for reference:
Months:
M 1 2 ... 11 12
Mo 1st 2nd ... 11th 12th
MM 01 02 ... 11 12
MMM Jan Feb ... Nov Dec
MMMM January February ... November December
Day:
d 0 1 ... 5 6
do 0th 1st ... 5th 6th
dd Su Mo ... Fr Sa
ddd Sun Mon ... Fri Sat
dddd Sunday Monday ... Friday Saturday
Year:
YY 70 71 ... 29 30
YYYY 1970 1971 ... 2029 2030
Y 1970 1971 ... 9999 +10000 +10001
.format('MM/DD/YYYY HH:mm:ss')
Try this:
var momentObj = $("#start_ts").datepicker("getDate");
var yourDate = momentObj.format('L');
本文标签: javascriptUsing momentjs to convert date to string quotMMddyyyyquotStack Overflow
版权声明:本文标题:javascript - Using moment.js to convert date to string "MMddyyyy" - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736807356a1953744.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论