Javascript Moment.js 将日期字符串转换为日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38251763/
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
Moment.js to convert date string into date
提问by noobcoder
Following up from my previous post: Javascript Safari: new Date() with strings returns invalid date when typedI am using Moment.js to convert a date string into a date field based on user input in the text box. This is to prevent the problem I described in the linked post for Safari and Firefox not able to render the date, when Chrome is fine. Here is the code snipper:
继我之前的帖子之后:Javascript Safari:带有字符串的 new Date() 在输入时返回无效日期我正在使用 Moment.js 根据用户在文本框中输入的内容将日期字符串转换为日期字段。这是为了防止我在 Safari 和 Firefox 的链接帖子中描述的问题,当 Chrome 正常时无法呈现日期。这是代码截图:
var tempDate = moment(userInputFieldDate).format('DD-MM-YYYY');
alert(tempDate);
In Chrome, it does work fine (it use to work with the Javascript Date object too) but gives me the moment.js deprecation warning
在 Chrome 中,它确实工作正常(它也用于处理 Javascript Date 对象)但给了我 moment.js 弃用警告
Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407for more info. Arguments: [object Object] Error
弃用警告:moment 构造回退到 js 日期。这是不鼓励的,并将在即将发布的主要版本中删除。请参阅https://github.com/moment/moment/issues/1407了解更多信息。参数:[object Object] 错误
On Firefox and Safari is just gives an UNDEFINED DATE in the alert window. So not entirely sure what should I be doing to convert the date string to Date object. Any suggestions ?
在 Firefox 和 Safari 上,只是在警告窗口中给出一个 UNDEFINED DATE。所以不完全确定我应该怎么做才能将日期字符串转换为 Date 对象。有什么建议 ?
回答by Matyas
If you are getting a JS based date String
then first use the new Date(String)
constructor and then pass the Date
object to the moment
method. Like:
如果您正在获取基于 JS 的日期,String
则首先使用new Date(String)
构造函数,然后将Date
对象传递给moment
方法。喜欢:
var dateString = 'Thu Jul 15 2016 19:31:44 GMT+0200 (CEST)';
var dateObj = new Date(dateString);
var momentObj = moment(dateObj);
var momentString = momentObj.format('YYYY-MM-DD'); // 2016-07-15
In case dateString
is 15-07-2016
, then you should use the moment(date:String, format:String)
method
如果dateString
是15-07-2016
,那么您应该使用该moment(date:String, format:String)
方法
var dateString = '07-15-2016';
var momentObj = moment(dateString, 'MM-DD-YYYY');
var momentString = momentObj.format('YYYY-MM-DD'); // 2016-07-15
回答by gakeko betsi
if you have a string of date, then you should try this.
如果你有一个日期字符串,那么你应该试试这个。
const FORMAT = "YYYY ddd MMM DD HH:mm";
const theDate = moment("2019 Tue Apr 09 13:30", FORMAT);
// Tue Apr 09 2019 13:30:00 GMT+0300
const theDate1 = moment("2019 Tue Apr 09 13:30", FORMAT).format('LL')
// April 9, 2019
or try this :
或试试这个:
const theDate1 = moment("2019 Tue Apr 09 13:30").format(FORMAT);