index.js 790 B

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