date-format-lite

Date format and parser for node.js and browser

npm install date-format-lite
11 downloads in the last day
111 downloads in the last week
623 downloads in the last month

Date format – Build Coverage

NPM

Lite version of Date format and parse for node.js and browser that extends native objects. Download compressed (2031 bytes, 1130 bytes gzipped) or uncompressed source.

How to use

In browser

<script src=date-format.js></script>

In node.js

npm install date-format-lite

require("date-format-lite")

Usage

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

// Parse
"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.middle_endian = 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

Define default format

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

Define custom formats

Date.masks.my = '"DayNo "D'
now.format("my")              // DayNo 10

Use another 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
Date.am = "a.m."
Date.pm = "p.m."

See tests for more examples

Syntax

  • Y - A two digit representation of a year without leading zeros. Examples: 99 or 3
  • YY - A two digit representation of a year. Examples: 99 or 03
  • YYYY - A full numeric representation of a year, 4 digits. Examples: 1999 or 2003
  • M - Numeric representation of a month, without leading zeros. 1 through 12
  • MM - Numeric representation of a month, with leading zeros. 01 through 12
  • MMM - A short textual representation of a month, three letters. Jan through Dec
  • MMMM - A full textual representation of a month, such as January or March. January through December
  • D - Day of the month without leading zeros. 1 to 31
  • DD - Day of the month, 2 digits with leading zeros. 01 to 31
  • DDD - A textual representation of a day, three letters. Mon through Sun
  • DDDD - A full textual representation of the day of the week. Sunday through Saturday
  • H - 12-hour format of an hour without leading zeros. 1 through 12
  • HH - 12-hour format of an hour with leading zeros. 01 through 12
  • h - 24-hour format of an hour without leading zeros. 0 through 23
  • hh - 24-hour format of an hour with leading zeros. 00 through 23
  • m - Minutes without leading zeros. 0 through 59
  • mm - Minutes with leading zeros. 00 to 59
  • s - Seconds without leading zeros. 0 through 59
  • ss - Seconds with leading zeros. 00 to 59
  • S - Milliseconds without leading zeros. 0 through 999
  • SS - Milliseconds with leading zeros. 000 to 999
  • u - Milliseconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
  • U - Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
  • A - Ante meridiem and Post meridiem. AM or PM
  • Z - Difference to Greenwich time (GMT) with colon between hours and minutes. Example: GMT +02:00
  • "text" - text, quotes should be escaped, eg '"a \"quoted text\"" YYYY'
ISO-8601
  • w - Day of the week. 1 (for Monday) through 7 (for Sunday)
  • W - Week number of year, first week is the week with 4 January in it
  • o - ISO-8601 year number. This has the same value as YYYY, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead

Notes

  • If no UTC relation information is given with a time representation, the time is assumed to be in local time.
  • If the time is in UTC, add a Z directly after the time without a space.

Browser Support

browser support

Licence

Copyright (c) 2012 Lauri Rooden <lauri@rooden.ee>
The MIT License

npm loves you