Momentjs:如何防止“无效日期"? [英] Momentjs : How to prevent "Invalid date"?

查看:88
本文介绍了Momentjs:如何防止“无效日期"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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

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

解决方案

TL;DR

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

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

...如果日期无效,它将使用 "" 作为 end_date 字符串.

详情

这里发生了两件截然不同的事情.

首先:

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

0000-01-01T00:00:00 将有效 —和 moment("0000-01-01T00:00:00").format("L") 愉快地返回 "01/01/0000" .>

如果您使用有效日期(例如您的 2015-01-01T00:00:00 示例),则代码没问题.

第二:

<块引用>

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

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

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

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

if (theMomentObject.isValid()) {//它具有作为有效日期} 别的 {//它没有}

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

if (!isNaN(theDateObject)) {//它有作为有效日期} 别的 {//它没有}

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

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.

解决方案

TL;DR

If your goal is to find out whether you have a valid date, use 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_date string if the date is invalid.

Details

There are two very different things going on here.

First:

0000-00-00T00:00:00 is an 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-00 makes no sense.

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

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

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.

Of course it does: format returns a string, and you're using format to get end_date.

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

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

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

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

...because isNaN will 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.

这篇关于Momentjs:如何防止“无效日期"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆