Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting JavaScript equivalent of DateTime.MinValue

Tags:

javascript

In C#, a date can have a min value which is 1/1/0001 12:00:00 AM. This is nice in validation situations.

I tried producing this value in JavaScript but it's not working:

var minDate = new Date("1/1/0001 12:00:00");

If I convert this to toISOString(), I'm getting "2001-01-01T18:00:00.000Z".

My API backend in C# is expecting a date value and I don't want to arbitrarily create my own version of DateTime.MinValue in JavaScript. Is there a way for me to produce this value in JavaScript?

like image 811
Sam Avatar asked Mar 26 '18 04:03

Sam


1 Answers

When a string passed into a Date constructor is not in extended ISO format the parsing is completely implementation dependant. If it is in ISO format the parsing is well-defined and it will work exactly the way you want:

var myDate = new Date('0001-01-01T00:00:00Z');
console.log( myDate.getUTCFullYear() );
like image 60
Paul Avatar answered Sep 23 '22 02:09

Paul