[英]Parse string to date with moment.js
我想用 moment.js 2014-02-27T10:00:00和输出日月年(2014 年 3 月 14 日)解析以下字符串我一直在阅读文档但没有成功http://momentjs.com/docs/# /解析/现在/
I always seem to find myself landing here only to realize that the title and question are not quite aligned.我似乎总是发现自己来到这里才意识到标题和问题并不完全一致。
If you want a moment date from a string :如果您想从字符串中获取时刻日期:
const myMomentObject = moment(str, 'YYYY-MM-DD')
From moment documentation :从时刻文档:
Instead of modifying the native Date.prototype, Moment.js creates a wrapper for the Date object.
Moment.js 没有修改原生 Date.prototype,而是为 Date 对象创建了一个包装器。
If you instead want a javascript Date object from a string :如果您想要一个字符串中的javascript Date对象:
const myDate = moment(str, 'YYYY-MM-DD').toDate();
No need for moment.js to parse the input since its format is the standard one :不需要 moment.js 来解析输入,因为它的格式是标准格式:
var date = new Date('2014-02-27T10:00:00');
var formatted = moment(date).format('D MMMM YYYY');
http://es5.github.io/#x15.9.1.15 http://es5.github.io/#x15.9.1.15
moment was perfect for what I needed.时刻非常适合我的需要。 NOTE it ignores the hours and minutes and just does it's thing if you let it.
请注意,它会忽略小时和分钟,如果您愿意,它只会做它的事情。 This was perfect for me as my API call brings back the date and time but I only care about the date.
这对我来说非常完美,因为我的 API 调用带回了日期和时间,但我只关心日期。
function momentTest() {
var varDate = "2018-01-19 18:05:01.423";
var myDate = moment(varDate,"YYYY-MM-DD").format("DD-MM-YYYY");
var todayDate = moment().format("DD-MM-YYYY");
var yesterdayDate = moment().subtract(1, 'days').format("DD-MM-YYYY");
var tomorrowDate = moment().add(1, 'days').format("DD-MM-YYYY");
alert(todayDate);
if (myDate == todayDate) {
alert("date is today");
} else if (myDate == yesterdayDate) {
alert("date is yesterday");
} else if (myDate == tomorrowDate) {
alert("date is tomorrow");
} else {
alert("It's not today, tomorrow or yesterday!");
}
}
let startDate = "2019-01-16T20:00:00.000"; let endDate = "2019-02-11T20:00:00.000"; let sDate = new Date(startDate); let eDate = new Date(endDate);
startDate = moment(sDate); endDate = moment(eDate);
Maybe try the Intl polyfill for IE8 or the olyfill service ?也许尝试 IE8 的 Intl polyfill 或 olyfill 服务?
or或者
https://github.com/andyearnshaw/Intl.js/ https://github.com/andyearnshaw/Intl.js/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.