Namespace: Dates:SaneDate

Dates:SaneDate

Source:

Members

(static) SaneDate

SaneDate is a reimplementation of JavaScript date objects, trying to iron out all the small fails which make you want to pull your hair while keeping the cool stuff in a streamlined manner.

SaneDates operate between the years 0 and 9999. If you create a new SaneDate, it starts off in local mode, always working and returning local information, but you may activate UTC mode by defining .utc = true;.

Parsing an ISO string creates a local SaneDate if no timezone is defined, if you define "Z" or an offset, the given string is interpreted as UTC info, so "2012-12-12T12:00:00" will set all parts as local information, meaning, that the UTC representation may differ according to your timezone, while "2012-12-12T12:00:00Z" will set all parts as UTC information, meaning that this is exactly what you get as the UTC representation, but your local info will differ according to your timezone. "2012-12-12T12:00:00+02:00" on the other hand, will create UTC information, with a negative offset of two hours, since this says: this datetime is two hours in the UTC future, so the resulting UTC info will be at 10 o'clock, while your local info will behave according to your timezone in regard to that info.

The relevant date parts of a SaneDate, which are also available as attributes to get and set are: "year", "month", "date" (not day!), "hours", "minutes", "seconds" and "milliseconds".

Additionally, set UTC mode, using the "utc" property.

SaneDates are very exception-happy and won't allow anything, that changes or produces a date in an unexpected manner. All automagic behaviour of JS dates is an error here, so setting a month to 13 and expecting a year jump will not work. Dates are very sensitive information and often used for contractual stuff, so anything coming out differently than you defined it in the first place is very problematic. Every change to any single property triggers a check, if any side effects occurred at all and if the change exactly results in the exact info being represented. Any side effect or misrepresentation results in an exception, since something happened we did not expect or define.

Months and week days are not zero based in SaneDates but begin with 1. Week days are not an attribute (and not settable), but accessible via .getWeekDay().

This whole implementation is heavily built around iso strings, so building a date with one and getting one to transfer should be forgiving, easy and robust. Something like '1-2-3 4:5:6.7' is a usable iso string for SaneDate, but getIsoString() will return correctly formatted '0001-02-03T04:05:06.700'.

See class documentation below for details.

Source:
See:
Example
let date = new SaneDate('1-2-3 4:5:6.7');
date = new SaneDate('2016-4-7');
date = new SaneDate('2016-04-07 13:37:00');
date = new SaneDate(2016, 4, 7);
date = new SaneDate(2016, 4, 7, 13, 37, 0, 999);
date.year = 2000;
date.forward('hours', 42);