Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Time for a new Date()

keithamus
February 23, 2012

Time for a new Date()

Introducing a new Date library, Tempus (http://tempus-js.com)

keithamus

February 23, 2012
Tweet

Other Decks in Programming

Transcript

  1. Hi

  2. d = new Date() d.getFullYear() + '-' + d.getMonth() +

    '-' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds(); -> "2012-1-22 11:27:27"
  3. // valueOf(); + new Date(); 1329910810336 // toString(); '' +

    new Date(); "Wed Feb 22 2012 11:38:17 GMT+0000 (GMT)"
  4. “When the [[DefaultValue]] internal method of O is called with

    no hint, then it behaves as if the hint were Number, unless O is a Date object (see 15.9.6), in which case it behaves as if the hint were String.” es5.github.com/x8.html#x8.12.8
  5. Python • Short accessors: date.year • Good ISO8601 support •

    strftime formatting • ‘naive dates’ (timezones) • iterators, calendar class, assertions, ranges
  6. PHP • DateTime class • OK ISO8601 support • strftime

    formatting • format constants (RSS, W3C) • date math, timezones
  7. Java • Lots of classes for stu! • SimpleDateFormat strings

    • Assertions (equals, after... ) • timezone support
  8. Good Library Rules • Small when gzipped (<5kb) • Good

    documentation • Terse code internal & ext. • Good patterns (chaining) • Good browser support • Cross JS code, browser/Node
  9. Steal the Good Parts • Common language features • ISO8601

    Support • strftime support • short accessors • timezone support • assertions (equalTo, lessThan)
  10. d = new Tempus() d.getYear() -> 12 d.getYear(13).getFullYear() -> 2013

    d.setFullYear(1980).year() -> 80 Fix *Year()
  11. d = new Tempus("2012-02-22T12:35:25.519+0000"); d = new Tempus("2012-02-22T12:35:25+00:00"); d =

    new Tempus("Wed, 22 Feb 2012 12:35:25 +0000"); d = new Tempus("Wed, 22 Feb 2012 12:35:25 GMT"); d.toISO8601String(); /* or */ d.toString("ISO8601"); -> "2012-02-22T12:35:25.519+0000" d.toW3CString() /* or */ d.toString("W3C"); -> "2012-02-22T12:35:25+00:00" /* So many more too */
  12. d = new Tempus(); d.toString("%d%o %B %Y, (%r) ") ->

    "22nd February 2012, (12:35:25 PM)"
  13. d = new Tempus( "22nd February 2012, (12:35:25 PM)" "%d%o

    %B %Y, (%r) " ); // Arrays! d = new Tempus( "2012/02/22" ["%Y/%m/%d", "%d/%m/%Y"] );
  14. new Tempus().eachDayOfMonth( function () { console.log(arguments); } ); -> 1

    [Tempus Instance - 1st Feb] -> 2 [Tempus Instance - 2nd Feb] -> 3 [Tempus Instance - 3rd Feb] ... -> 29 [Tempus Instance - 29th Feb]