使用moment.js将日期转换为字符串“MM / dd / yyyy” [英] Using moment.js to convert date to string "MM/dd/yyyy"

查看:65
本文介绍了使用moment.js将日期转换为字符串“MM / dd / yyyy”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我有这段代码:

var sTimestamp =
moment($("#start_ts").datepicker("getDate")).format("MM/dd/yyyy");

但它没有把它变成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

如果我使用compose in提琴手并将身体改为:

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

startTimestamp=03/13/2013&endTimestamp=03/14/2013&pageSize=50&pageNum=0

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

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?

顺便说一下,我假设datepicker.getDate返回一个日期对象,因为这是jQuery docs告诉的内容我。

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

谢谢,

推荐答案

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

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

这篇关于使用moment.js将日期转换为字符串“MM / dd / yyyy”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆