Javascript Momentjs:如何防止“无效日期”?

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

Momentjs : How to prevent "Invalid date"?

javascriptmomentjsinvalidation

提问by R3tep

I have the following code :

我有以下代码:

var fomattedDate = moment(myDate).format("L");

Sometimes moment(myDate).format("L")returns "Invalid date", I want to know if there is a way to prevent that and return an empty string instead.

有时会moment(myDate).format("L")返回“无效日期”,我想知道是否有办法防止这种情况发生并返回一个空字符串。

回答by T.J. Crowder

TL;DR

TL; 博士

If your goal is to find out whether you have a valid date, use Moment's isValid:

如果您的目标是确定您是否有有效日期,请使用 Moment's isValid

var end_date_moment, end_date;
jsonNC.end_date = jsonNC.end_date.replace(" ", "T");
end_date_moment = moment(jsonNC.end_date);
end_date = end_date_moment.isValid() ? end_date_moment.format("L") : "";

...which will use ""for the end_datestring if the date is invalid.

...如果日期无效,它将""用于end_date字符串。

Details

细节

There are two very different things going on here.

这里发生了两件非常不同的事情。

First:

第一的:

0000-00-00T00:00:00isan invalid date. There's no month prior to January (which is month #1 in that format), nor a day of a month prior to day #1. So 0000-00-00makes no sense.

0000-00-00T00:00:00无效日期。没有在 1 月之前的月份(即该格式中的第 1 个月),也没有在第 1 天之前的一个月中的某一天。所以0000-00-00没有意义。

0000-01-01T00:00:00would be valid — and moment("0000-01-01T00:00:00").format("L")happily returns "01/01/0000"for it.

0000-01-01T00:00:00将是有效的 - 并moment("0000-01-01T00:00:00").format("L")愉快地返回"01/01/0000"它。

If you use a valid date (such as your 2015-01-01T00:00:00example), the code is fine.

如果您使用有效日期(例如您的2015-01-01T00:00:00示例),则代码很好。

Second:

第二:

console.log(Object.prototype.toString.call(end_date));

It returns [object String] even with a valid date, so the if condition doesn't working in my case.

console.log(Object.prototype.toString.call(end_date));

即使具有有效日期,它也会返回 [object String],因此 if 条件在我的情况下不起作用。

Of course it does: formatreturns a string, and you're using formatto get end_date.

当然可以:format返回一个字符串,而您正在使用formatget end_date

If you want to know if a MomentJS object has an invalid date, you can check like this:

如果你想知道一个 MomentJS 对象是否有一个无效的日期,你可以这样检查:

if (theMomentObject.isValid()) {
    // It has as valid date
} else {
    // It doesn't
}

If you want to know if a Dateobject has an invalid date:

如果您想知道Date对象是否具有无效日期:

if (!isNaN(theDateObject)) {
    // It has as valid date
} else {
    // It doesn't
}

...because isNaNwill coerce the date to its primitive form, which is the underlying number of milliseconds since Jan 1 1970 00:00:00 GMT, and when a date has an "invalid" date, the number it contains is NaN. So isNaN(theDateObject)is true when the date is invalid.

...因为isNaN会将日期强制为其原始形式,即自 1970 年 1 月 1 日 00:00:00 GMT 以来的基本毫秒数,并且当日期具有“无效”日期时,它包含的数字是NaN。所以,isNaN(theDateObject)当日期是无效的也是如此。