Immutable DateTime/TimeSpan for TypeScript based on .NET DateTime
Publish Date: Oct 3 '19
10 0
Immutable DateTime
Date object is not immutable, and also modifying it is little complicated. There is no TimeSpan equivalent in JavaScript. So this library was created to provide DateTime/TimeSpan similar to .NET.
I know about moment-js, but I needed something very simpler.
Features
Immutable DateTime
Support for TimeSpan (differences between dates)
Simple Add/Difference
Support for properties (year, month, day, hour, minute, second .. all are readonly properties)
Backward compatibility with JavaScript's Date
Compatibility with Date
In order to make usage simple, you can pass DateTime to any method that uses Date and everything will still work. To prevent intellisense from listing all Date's methods, we have used a hack to create new Date object in constructor of DateTime.
For easy access, all to*String methods of Date are available in intellisense.
constd=DateTime.now();console.log(dinstanceofDate);// prints true..console.log(dinstanceofDateTime);// prints true..// however intellisense does not// show up Date methodsd.year
This library does not pollute Date's prototype, logically DateTime is a new class and has its own way of functioning.
Json Parser
You will have to hook some code to change prototype of Date objects sent through JSON.parse. d.prototype = DateTime.prototype.
Usage
Properties
Year, Month, Day, Hour, Minute, Second and Millisecond are all properties.
// now is a readonly property which returns new// instance of DateTime of current system timeconstd=DateTime.now;console.log(`${d.year}-${d.month}-${d.day}`);
Trim Time
constd=DateTime.now;// returns new instance of DateTime// with time part trimmed...constday=d.date;
TimeSpan
constd=DateTime.now;// t is of type TimeSpanconstt=d.time;console.log(t);// prints 10.00 PM (local time)
Difference in TimeSpan
constd1=newDateTime(2010,1,1);constd2=newDateTime(2012,1,1);// returns TimeSpanconstdiff=d2.diff(d1);// prints 730console.log(diff.totalDays);
Add TimeSpan
constt=TimeSpan.fromDays(2);constd1=newDateTime(2010,1,1);constd2=d1.add(t);// prints 2010-01-03console.log(d2);
Type Information
exportdefaultclassDateTime{staticgettoday():DateTime;staticgetutcNow():DateTime;staticgetnow():DateTime;staticparse(s:string):DateTime;gethour():number;getminute():number;getsecond():number;getmillisecond():number;getday():number;getdayOfWeek():number;getmonth():number;getyear():number;gettimeZoneOffset():TimeSpan;getmsSinceEpoch():number;/** Strips time of the day and returns Date only */getdate():DateTime;getasJSDate():Date;gettime():TimeSpan;constructor();constructor(time?:number|string);constructor(year?:number,month?:number,date?:number,hours?:number,minutes?:number,seconds?:number,ms?:number);add(d:DateTime|TimeSpan):DateTime;add(days:number,hours?:number,minutes?:number,seconds?:number,milliseconds?:number):DateTime;addMonths(m:number):DateTime;addYears(y:number):DateTime;diff(rhs:Date|DateTime):TimeSpan;equals(d:DateTime):boolean;// for easy access, following methods// are available on intellisensetoLocaleString (locales?:string|string[],options?:Intl.DateTimeFormatOptions):string;toLocaleDateString (locales?:string|string[],options?:Intl.DateTimeFormatOptions):string;toLocaleTimeString (locales?:string|string[],options?:Intl.DateTimeFormatOptions):string;toUTCString():string;toISOString():string;toJSON(key?:any):string;toTimeString():string;toDateString():string;}
Immutable DateTime library for Web Atoms in JavaScript similar to .Net DateTime and TimeSpan
Features
Immutable DateTime
Support for TimeSpan (differences between dates)
Simple Add/Difference
Support for properties
Support for Comparison
Support for valueOf which makes it easier to compare and sort dates
Backward compatibility with JavaScript's Date
Compatibility
In order to make usage simple, you can pass DateTime to any method that uses Date and everything will still work. To prevent intellisense from listing all Date's methods, we have used a hack to create new Date object in constructor of DateTime.
For easy access, all to*String methods of Date are available in intellisense.
constd=DateTime.now();console.log(dinstanceofDate);// prints true..// however intellisense does not// show up for Date methods except for toLocaleDateString etcd.year