javascript moment.js - 更改已格式化的过去日期的格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19807302/
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-10-27 16:52:13  来源:igfitidea点击:

moment.js - change format of already formatted past date

javascriptdateformattingmomentjs

提问by Kosmetika

I need to get a past date with moment.js (http://momentjs.com/) which I receive in specific format:

我需要使用我收到的特定格式的moment.js ( http://momentjs.com/)获取过去的日期:

moment('31.10.2013', 'dd.mm.yy');

That returns me a strange response where I see a current date in _doption:

这会返回一个奇怪的响应,我在_d选项中看到当前日期:

// returns
_a: Array[7]
_d: Wed Nov 06 2013 00:10:00 GMT+0200 (EET)
_f: "dd.mm.yy"
_i: "26.10.2013"
_isUTC: false
_l: undefined
_pf: Object
_strict: undefined

I assume that is the problem when I do formatting:

我认为这是我格式化时的问题:

moment('31.10.2013', 'dd.mm.yy').format('YYYY/MM/DD');
// returns current date (why??!)
// "2013/11/06"

So what is wrong here, could I format the past date?

那么这里有什么问题,我可以格式化过去的日期吗?

回答by Ted Johnson

At first I thought it was the format, but it was actually the format in, lower case means something different than uppercase both in and out.

起初我以为是格式,但实际上是格式,小写的意思与大写的输入和输出不同。

moment('31.10.2013', 'DD.MM.YYYY').format('YYYY/MM/DD')
>> "2013/10/31"

moment('31.10.2013', 'dd.mm.yyyy').format('YYYY/MM/DD')
>> "2013/11/06"

So fix your input mask, not the format.

所以修复你的输入掩码,而不是格式。