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?
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() );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With