Immutable DateTime library for Web Atoms in JavaScript similar to .Net DateTime and TimeSpan
- Immutable DateTime
- Support for TimeSpan (differences between dates)
- Simple Add/Difference
- Support for properties
- Backward compatibility with JavaScript's 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.
const d = DateTime.now();
console.log(d instance of Date); // prints true..
// however intellisense does not
// show up Date methods
d.year
Year, Month, Day, Hour, Minute, Second and Millisecond are all properties.
const d = DateTime.now();
console.log(`${d.year}-${d.month}-${d.day}`);
const d = DateTime.now();
// returns new instance of DateTime
// with time part trimmed...
const day = d.date;
const d = DateTime.now();
const t = d.time;
console.log(t); // prints 10.00 PM (local time)
const d1 = new DateTime(2010, 1, 1);
const d2 = new DateTime(2012, 1, 1);
// returns TimeSpan
const diff = d2.diff(d1);
// prints 730
console.log(diff.totalDays);
const t = TimeSpan.fromDays(2);
const d1 = new DateTime(2010, 1, 1);
const d2 = d1.add(t);
// prints 2010-01-03
console.log(d2);