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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15411833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:59:39  来源:igfitidea点击:

Using moment.js to convert date to string "MM/dd/yyyy"

javascriptjquerymomentjs

提问by okysabeni

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 发布。当页面加载或更改日期选择器时,会进行 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 并将主体更改为:

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,

谢谢,

采纳答案by Robbie

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 文档

回答by

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

...and MySQL date format:

...和 ​​MySQL 日期格式:

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

回答by Hyman

Try this:

尝试这个:

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

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

回答by Hitesh Sahu

Use:

用:

date.format("MM/DD/YYYY") or date.format("MM-DD-YYYY")}

date.format("MM/DD/YYYY") 或 date.format("MM-DD-YYYY")}

Other Supported formats for reference:

其他支持格式供参考:

Months:

月份:

M1 2 ... 11 12

Mo1st 2nd ... 11th 12th

MM01 02 ... 11 12

MMMJan Feb ... Nov Dec

MMMMJanuary February ... November December

1 2 ... 11 12

1 日 2 日 ... 11 日 12 日

毫米01 02 ... 11 12

MMM一月二月 ... 十一月 十二月

MMMM一月 二月 ... 十一月 十二月

Day:

日:

d0 1 ... 5 6

do0th 1st ... 5th 6th

ddSu Mo ... Fr Sa

dddSun Mon ... Fri Sat

ddddSunday Monday ... Friday Saturday

d0 1 ... 5 6

0th 1st ... 5th 6th

dd苏沫 ... Fr Sa

ddd周日 周一 ... 周五 周六

dddd星期日 星期一 ... 星期五 星期六

Year:

年:

YY70 71 ... 29 30

YYYY1970 1971 ... 2029 2030

Y1970 1971 ... 9999 +10000 +10001

YY70 71 ... 29 30

YYYY1970 1971 ... 2029 2030

1970 1971 ... 9999 +10000 +10001

回答by Purva

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