使用moment.js将日期转换为字符串“MM / dd / yyyy”

时间:2022-08-25 10:47:43

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.

我需要从jquery datepicker获取日期值将其转换为字符串格式“MM / dd / yyyy”,以便它可以做正确的ajax帖子。当页面加载或更改datepicker时,将进行jquery ajax调用。

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:

但它并没有把它变成“MM / dd / yyyy”。当我使用小提琴手来检查线下发送的是什么时,这就是身体:

startTimestamp=03%2FTh%2Fyyyy&endTimestamp=03%2FTh%2Fyyyy&pageSize=50&pageNum=0

If I use the compose in fiddler and change the body to:

如果我在fiddler中使用compose并将body更改为:

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?

我得到了正确的答复。所以,我的问题是,是否有办法使用moment.js获取日期对象并将其格式化为字符串“MM / dd / yyyy”?或者从datepicker获取日期的方式有问题吗?

Btw, I am assuming that datepicker.getDate returns a date object since that's what the jQuery docs tell me.

顺便说一句,我假设datepicker.getDate返回一个日期对象,因为这是jQuery文档告诉我的。

Thank you,

谢谢,

4 个解决方案

#1


38  

I think you just have incorrect casing in the format string. According to the documentation this should work for you: MM/DD/YYYY

我认为你在格式字符串中只有不正确的大小写。根据文档,这应该适合你:MM / DD / YYYY

moment.js documentation

moment.js文档

#2


59  

StartDate = moment(StartDate).format('MM-YYYY');

...and MySQL date format:

...和MySQL日期格式:

StartDate = moment(StartDate).format('YYYY-MM-DD');

#3


6  

Try this:

尝试这个:

var momentObj = $("#start_ts").datepicker("getDate");

var yourDate = momentObj.format('L');

#4


-1  

.format('MM/DD/YYYY HH:mm:ss')

#1


38  

I think you just have incorrect casing in the format string. According to the documentation this should work for you: MM/DD/YYYY

我认为你在格式字符串中只有不正确的大小写。根据文档,这应该适合你:MM / DD / YYYY

moment.js documentation

moment.js文档

#2


59  

StartDate = moment(StartDate).format('MM-YYYY');

...and MySQL date format:

...和MySQL日期格式:

StartDate = moment(StartDate).format('YYYY-MM-DD');

#3


6  

Try this:

尝试这个:

var momentObj = $("#start_ts").datepicker("getDate");

var yourDate = momentObj.format('L');

#4


-1  

.format('MM/DD/YYYY HH:mm:ss')