Date format and parser for node.js and browser
@version    0.7.1
@date       2015-03-21
@stability  2 - Unstable

Date format – Build Coverage

A small library for parsing and formatting dates that extends native Date object.


npm install date-format-lite --save


Format Dates

date-format-lite adds format(mask, [zone]) method to native Date.prototype.

var now = new Date()          // Date {Wed Jul 10 2013 16:47:36 GMT+0300 (EEST)}
now.format("iso")             // 2013-07-10T13:47:36Z
now.format("hh:mm")           // 16:47 (local time)
now.format("UTC:hh:mm")       // 13:47
now.format("hh:mm", 2.5)      // 16:17

Parse Dates

date-format-lite adds date([outFormat], [outZone], [inZone]) method to native String.prototype and Number.prototype.

"2013-07-10".date()           // Date {Wed Jul 10 2013 03:00:00 GMT+0300 (EEST)}
"2013-07-10T13:47:36Z".date() // Date {Wed Jul 10 2013 16:47:36 GMT+0300 (EEST)}
"10/07/2013".date()           // Date {Wed Jul 10 2013 03:00:00 GMT+0300 (EEST)}
Date.middleEndian = true
"10/07/2013".date()           // Date {Mon Oct 07 2013 03:00:00 GMT+0300 (EEST)}
// Change format
"10/07/2013".date("YYYY-MM-DD")// 2013-07-10

Add custom formats = '"DayNo "D'
now.format("my")              // DayNo 10

Change default format

Date.masks.default = 'YYYY-MM-DD hh:mm:ss'
now.format()         // 2013-07-10 13:47:36

Change language

// Add to estonian-lang.js
Date.dayNames = "P E T K N R L pühapäev esmaspäev teisipäev kolmapäev neljapäev reede laupäev".split(" ")
Date.monthNames = "Jaan Veeb Märts Apr Mai Juuni Juuli Aug Sept Okt Nov Dets jaanuar veebruar märts aprill mai juuni juuli august september oktoober november detsember".split(" ")

// Change AM and PM = "a.m." = "p.m."

See tests for more examples





Browser Support

It should work IE6 and up but automated testing is currently broken.


Copyright (c) 2012-2015 Lauri Rooden <[email protected]>
The MIT License

Fork me on GitHub
Tags: date, time, format, parse, litejs

Have an update or suggestion for this note? You can edit it and send me a pull request.

Since 2013 to 2015