Sam
Sam

Reputation: 30292

MAX DateTime value in JavaScript

In .NET, there's var myMaxDate = DateTime.MaxValue; which assigns the value of 12/31/9999 23:59:59.

I need to send this value to my ASP.NET Core API. I tried producing this value in JavaScript using the following code but this doesn't seem to produce a valid date/time value.

var myRequest = {
   id: 123,
   name: "Some name",
   date: new Date("9999", "12", "31", 23, 59, 59).toISOString()
};

How do I produce this value in ISO string?

Upvotes: 2

Views: 1714

Answers (1)

Lux
Lux

Reputation: 18240

You can use this to get the JS Date object:

new Date("9999-12-31T23:59:59")

however if you just call .toISOString() it's a bit useless. Just do this:

date: "9999-12-31T23:59:59.000Z",

However your error was that the monthIndex (second parameter to the Date() constructor) is 0-11 not 1-12. So for december you choose 11:

new Date("9999", "11", "31", 23, 59, 59).toISOString()

However this is one of the reasons I would always provide a ISO8601 String when possible instead of magic numbers to the Date() constructor. ISO8601 is a universal standard over all languages.

Upvotes: 2

Related Questions