node-dateformat

A node.js package for Steven Levithan’s excellent dateFormat() function.

Modifications

  • Removed the Date.prototype.format method. Sorry folks, but extending native prototypes is for suckers.
  • Added a module.exports = dateFormat; statement at the bottom

Usage

As taken from Steven’s post, modified to match the Modifications listed above:

  1. var dateFormat = require('dateformat');
  2. var now = new Date();
  3. // Basic usage
  4. dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");
  5. // Saturday, June 9th, 2007, 5:46:21 PM
  6. // You can use one of several named masks
  7. dateFormat(now, "isoDateTime");
  8. // 2007-06-09T17:46:21
  9. // ...Or add your own
  10. dateFormat.masks.hammerTime = 'HH:MM! "Can\'t touch this!"';
  11. dateFormat(now, "hammerTime");
  12. // 17:46! Can't touch this!
  13. // When using the standalone dateFormat function,
  14. // you can also provide the date as a string
  15. dateFormat("Jun 9 2007", "fullDate");
  16. // Saturday, June 9, 2007
  17. // Note that if you don't include the mask argument,
  18. // dateFormat.masks.default is used
  19. dateFormat(now);
  20. // Sat Jun 09 2007 17:46:21
  21. // And if you don't include the date argument,
  22. // the current date and time is used
  23. dateFormat();
  24. // Sat Jun 09 2007 17:46:22
  25. // You can also skip the date argument (as long as your mask doesn't
  26. // contain any numbers), in which case the current date/time is used
  27. dateFormat("longTime");
  28. // 5:46:22 PM EST
  29. // And finally, you can convert local time to UTC time. Simply pass in
  30. // true as an additional argument (no argument skipping allowed in this case):
  31. dateFormat(now, "longTime", true);
  32. // 10:46:21 PM UTC
  33. // ...Or add the prefix "UTC:" to your mask.
  34. dateFormat(now, "UTC:h:MM:ss TT Z");
  35. // 10:46:21 PM UTC
  36. // You can also get the ISO 8601 week of the year:
  37. dateFormat(now, "W");
  38. // 42

License

(c) 2007-2009 Steven Levithan stevenlevithan.com, MIT license.