blob: 2670a9d3384da97d015af0794ed9a76242e01243 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
var abs = Math.abs;
export function toISOString() {
// inspired by https://github.com/dordille/moment-isoduration/blob/master/moment.isoduration.js
var Y = abs(this.years());
var M = abs(this.months());
var D = abs(this.days());
var h = abs(this.hours());
var m = abs(this.minutes());
var s = abs(this.seconds() + this.milliseconds() / 1000);
var total = this.asSeconds();
if (!total) {
// this is the same as C#'s (Noda) and python (isodate)...
// but not other JS (goog.date)
return 'P0D';
}
return (total < 0 ? '-' : '') +
'P' +
(Y ? Y + 'Y' : '') +
(M ? M + 'M' : '') +
(D ? D + 'D' : '') +
((h || m || s) ? 'T' : '') +
(h ? h + 'H' : '') +
(m ? m + 'M' : '') +
(s ? s + 'S' : '');
}
|