Feature idea - Date.add

I was thinking if we could simplify the process of date arithmetic by adding an "add" method, which would add a certain value to the given date object and return the new date. I propose the following structure:

Date.prototype.add = function(unit:String, value:Number)

Where unit is a specified string to denote hours, minutes, seconds, days, months etc. and value is the actual value being added. If value is negative, it would end up subtracting that from the specified date.

The reason I am proposing this is that although adding days to a date object is fairly simple, there does not appear to be any straightforward way to add minutes, seconds, and such to a certain date/time. There have been javascript libraries like moment, but it sure would be nice to have this in the ES specs.

Thoughts?

Forgot to mention, looking for champions.

Temporal will provide functionality similar to this

2 Likes

@aclaymore Thank you. Another date-related convenience I would love to have would be to have a destructuring capability for a Date-like object. That is to say, something like -

const {date, month, year} = new CustomDate(<params>)

Would it be possible to do it with a Temporal.PlainDate object?

1 Like

Yes you can do:

const {day, month, year} = plainDate;

3 Likes