UNPKG

790 BJavaScriptView Raw
1'use strict';
2
3/**
4 * Pad a `number` with a ten's place zero.
5 *
6 * @param {number} number
7 * @return {string}
8 */
9function pad(number) {
10 var n = number.toString();
11 return n.length === 1 ? '0' + n : n;
12}
13
14/**
15 * Turn a `date` into an ISO string.
16 *
17 * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
18 *
19 * @param {Date} date
20 * @return {string}
21 */
22function toISOString(date) {
23 return date.getUTCFullYear()
24 + '-' + pad(date.getUTCMonth() + 1)
25 + '-' + pad(date.getUTCDate())
26 + 'T' + pad(date.getUTCHours())
27 + ':' + pad(date.getUTCMinutes())
28 + ':' + pad(date.getUTCSeconds())
29 + '.' + String((date.getUTCMilliseconds()/1000).toFixed(3)).slice(2, 5)
30 + 'Z';
31}
32
33/*
34 * Exports.
35 */
36
37module.exports = toISOString;