Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Momentjs : How to prevent "Invalid date"?

Tags:

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.

like image 230
R3tep Avatar asked Mar 11 '15 17:03

R3tep


People also ask

Is Moment date valid?

isValid() is the method available on moment which tells if the date is valid or not. MomentJS also provides many parsing flags which can be used to check for date validation.

Should you still use MomentJS?

Moment. js is a fantastic time & date library with lots of great features and utilities. However, if you are working on a performance sensitive web application, it might cause a huge performance overhead because of its complex APIs and large bundle size.

Is MomentJS immutable?

Mutability 1.0.The moment object in Moment. js is mutable. This means that operations like add, subtract, or set change the original moment object.

How can I set a valid empty date in MomentJS?

If you use the moment adapter and moment dates, you should set your component's date variable to null , and not to moment(null) . Internally, the datepicker will ask the adapter if the date is valid but moment(null). isValid() returns false thus triggering the parse validator that will return an error.


1 Answers

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.

like image 100
T.J. Crowder Avatar answered Sep 21 '22 05:09

T.J. Crowder