[英]Moment.js - How to convert date string into date?
Following up from my previous post: Javascript Safari: new Date() with strings returns invalid date when typed跟进我之前的帖子: Javascript Safari: new Date() with strings 输入时返回无效日期
I am using Moment.js to convert a date string into a date field based on user input in the text box.我正在使用Moment.js根据文本框中的用户输入将日期字符串转换为日期字段。
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.这是为了防止我在 Safari 和 Firefox 的链接帖子中描述的问题无法在 Chrome 正常时呈现日期。
Here is the code snipper:这是代码截图:
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.弃用警告:moment 构造回退到 js 日期。 This is discouraged and will be removed in upcoming major release.这是不鼓励的,并将在即将发布的主要版本中删除。 Please refer to https://github.com/moment/moment/issues/1407 for more info.请参阅https://github.com/moment/moment/issues/1407了解更多信息。 Arguments: [object Object] Error参数:[object Object] 错误
On Firefox and Safari is just gives an UNDEFINED DATE
in the alert window.在 Firefox 和 Safari 上,只是在警告窗口中给出一个UNDEFINED DATE
。 So not entirely sure what should I be doing to convert the date string to a Date object.所以不完全确定我应该怎么做才能将日期字符串转换为 Date 对象。
Any suggestions on this issue?关于这个问题有什么建议吗?
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.如果您要获取基于 JS 的日期String
则首先使用new Date(String)
构造函数,然后将Date
对象传递给moment
方法。 Like:像:
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
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);
Sweet and Simple!甜美又简单!
moment('2020-12-04T09:52:03.915Z').format('lll');
Dec 4, 2020 4:58 PM 2020 年 12 月 4 日下午 4:58
moment.locale(); // en
moment().format('LT'); // 4:59 PM
moment().format('LTS'); // 4:59:47 PM
moment().format('L'); // 12/08/2020
moment().format('l'); // 12/8/2020
moment().format('LL'); // December 8, 2020
moment().format('ll'); // Dec 8, 2020
moment().format('LLL'); // December 8, 2020 4:59 PM
moment().format('lll'); // Dec 8, 2020 4:59 PM
moment().format('LLLL'); // Tuesday, December 8, 2020 4:59 PM
moment().format('llll'); // Tue, Dec 8, 2020 4:59 PM
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.