strftime for JavaScript
660
stars
219
commits
JavaScript
primary language
Oct 31, 2024
updated
strftime for JavaScript. Works in (at least) node.js and browsers. Supports localization and timezones. Most standard specifiers from C are supported as well as some other extensions from Ruby.
node:
npm install strftime
bower install strftime
component install samsonjs/strftime
yarn:
yarn add strftime
Or you can copy strftime.js wherever you want to use it, whether that's with a <script> tag or require or anything else.
var strftime = require('strftime') // not required in browsers
console.log(strftime('%B %d, %Y %H:%M:%S')) // => April 28, 2011 18:21:08
console.log(strftime('%F %T', new Date(1307472705067))) // => 2011-06-07 18:51:45
If you want to localize it:
var strftime = require('strftime') // not required in browsers
var it_IT = {
identifier: 'it-IT',
days: ['domenica', 'lunedi', 'martedi', 'mercoledi', 'giovedi', 'venerdi', 'sabato'],
shortDays: ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'],
months: ['gennaio', 'febbraio', 'marzo', 'aprile', 'maggio', 'giugno', 'luglio', 'agosto', 'settembre', 'ottobre', 'novembre', 'dicembre'],
shortMonths: ['gen', 'feb', 'mar', 'apr', 'mag', 'giu', 'lug', 'ago', 'set', 'ott', 'nov', 'dic'],
AM: 'AM',
PM: 'PM',
am: 'am',
pm: 'pm',
formats: {
D: '%m/%d/%y',
F: '%Y-%m-%d',
R: '%H:%M',
X: '%T',
c: '%a %b %d %X %Y',
r: '%I:%M:%S %p',
T: '%H:%M:%S',
v: '%e-%b-%Y',
x: '%D'
}
}
var strftimeIT = strftime.localize(it_IT)
console.log(strftimeIT('%B %d, %Y %H:%M:%S')) // => aprile 28, 2011 18:21:08
console.log(strftimeIT('%B %d, %Y %H:%M:%S', new Date(1307472705067))) // => giugno 7, 2011 18:51:45
Some locales are bundled and can be used like so:
var strftime = require('strftime') // not required in browsers
var strftimeIT = strftime.localizeByIdentifier('it_IT')
console.log(strftimeIT('%B %d, %Y %H:%M:%S')) // => aprile 28, 2011 18:21:08
console.log(strftimeIT('%B %d, %Y %H:%M:%S', new Date(1307472705067))) // => giugno 7, 2011 18:51:45
The full list of bundled locales is below.
Time zones can be passed in as an offset from GMT in minutes.
var strftime = require('strftime') // not required in browsers
var strftimePDT = strftime.timezone(-420)
var strftimeCEST = strftime.timezone(120)
console.log(strftimePDT('%B %d, %y %H:%M:%S', new Date(1307472705067))) // => June 07, 11 11:51:45
console.log(strftimeCEST('%F %T', new Date(1307472705067))) // => 2011-06-07 20:51:45
Alternatively you can use the timezone format used by ISO 8601, +HHMM or -HHMM.
var strftime = require('strftime') // not required in browsers
var strftimePDT = strftime.timezone('-0700')
var strftimeCEST = strftime.timezone('+0200')
console.log(strftimePDT('%F %T', new Date(1307472705067))) // => 2011-06-07 11:51:45
console.log(strftimeCEST('%F %T', new Date(1307472705067))) // => 2011-06-07 20:51:45
Extensions from Ruby are noted in the following list.
Unsupported specifiers are rendered without the percent sign.
e.g. %q becomes q. Use %% to get a literal % sign.
%a %b %d %X %Y %Z in en_US (based on locale)%m/%d/%y in en_US (based on locale)%Y-%m-%d in en_US (based on locale)%H:%M in en_US (based on locale)%I:%M:%S %p in en_US (based on locale)%H:%M:%S in en_US (based on locale)%e-%b-%Y in en_US (based on locale)%T or %r in en_US (based on locale)%D in en_US (based on locale)For more detail see man 3 strftime as the format specifiers should behave identically. If behaviour differs please file a bug.
Any specifier can be modified with -, _, 0, or : as well, as in Ruby. Using %- will omit any leading zeroes or spaces, %_ will force spaces for padding instead of the default, and %0 will force zeroes for padding. There's some redundancy here as %-d and %e have the same result, but it solves some awkwardness with formats like %l. Using %: for time zone offset, as in %:z will insert a colon as a delimiter.
Copyright 2010 - 2024 Sami Samhuri sami@samhuri.net
JavaScript
99.2%
strftime for JavaScript
660
stars
219
commits
JavaScript
primary language
Oct 31, 2024
updated
strftime for JavaScript. Works in (at least) node.js and browsers. Supports localization and timezones. Most standard specifiers from C are supported as well as some other extensions from Ruby.
node:
npm install strftime
bower install strftime
component install samsonjs/strftime
yarn:
yarn add strftime
Or you can copy strftime.js wherever you want to use it, whether that's with a <script> tag or require or anything else.
var strftime = require('strftime') // not required in browsers
console.log(strftime('%B %d, %Y %H:%M:%S')) // => April 28, 2011 18:21:08
console.log(strftime('%F %T', new Date(1307472705067))) // => 2011-06-07 18:51:45
If you want to localize it:
var strftime = require('strftime') // not required in browsers
var it_IT = {
identifier: 'it-IT',
days: ['domenica', 'lunedi', 'martedi', 'mercoledi', 'giovedi', 'venerdi', 'sabato'],
shortDays: ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'],
months: ['gennaio', 'febbraio', 'marzo', 'aprile', 'maggio', 'giugno', 'luglio', 'agosto', 'settembre', 'ottobre', 'novembre', 'dicembre'],
shortMonths: ['gen', 'feb', 'mar', 'apr', 'mag', 'giu', 'lug', 'ago', 'set', 'ott', 'nov', 'dic'],
AM: 'AM',
PM: 'PM',
am: 'am',
pm: 'pm',
formats: {
D: '%m/%d/%y',
F: '%Y-%m-%d',
R: '%H:%M',
X: '%T',
c: '%a %b %d %X %Y',
r: '%I:%M:%S %p',
T: '%H:%M:%S',
v: '%e-%b-%Y',
x: '%D'
}
}
var strftimeIT = strftime.localize(it_IT)
console.log(strftimeIT('%B %d, %Y %H:%M:%S')) // => aprile 28, 2011 18:21:08
console.log(strftimeIT('%B %d, %Y %H:%M:%S', new Date(1307472705067))) // => giugno 7, 2011 18:51:45
Some locales are bundled and can be used like so:
var strftime = require('strftime') // not required in browsers
var strftimeIT = strftime.localizeByIdentifier('it_IT')
console.log(strftimeIT('%B %d, %Y %H:%M:%S')) // => aprile 28, 2011 18:21:08
console.log(strftimeIT('%B %d, %Y %H:%M:%S', new Date(1307472705067))) // => giugno 7, 2011 18:51:45
The full list of bundled locales is below.
Time zones can be passed in as an offset from GMT in minutes.
var strftime = require('strftime') // not required in browsers
var strftimePDT = strftime.timezone(-420)
var strftimeCEST = strftime.timezone(120)
console.log(strftimePDT('%B %d, %y %H:%M:%S', new Date(1307472705067))) // => June 07, 11 11:51:45
console.log(strftimeCEST('%F %T', new Date(1307472705067))) // => 2011-06-07 20:51:45
Alternatively you can use the timezone format used by ISO 8601, +HHMM or -HHMM.
var strftime = require('strftime') // not required in browsers
var strftimePDT = strftime.timezone('-0700')
var strftimeCEST = strftime.timezone('+0200')
console.log(strftimePDT('%F %T', new Date(1307472705067))) // => 2011-06-07 11:51:45
console.log(strftimeCEST('%F %T', new Date(1307472705067))) // => 2011-06-07 20:51:45
Extensions from Ruby are noted in the following list.
Unsupported specifiers are rendered without the percent sign.
e.g. %q becomes q. Use %% to get a literal % sign.
%a %b %d %X %Y %Z in en_US (based on locale)%m/%d/%y in en_US (based on locale)%Y-%m-%d in en_US (based on locale)%H:%M in en_US (based on locale)%I:%M:%S %p in en_US (based on locale)%H:%M:%S in en_US (based on locale)%e-%b-%Y in en_US (based on locale)%T or %r in en_US (based on locale)%D in en_US (based on locale)For more detail see man 3 strftime as the format specifiers should behave identically. If behaviour differs please file a bug.
Any specifier can be modified with -, _, 0, or : as well, as in Ruby. Using %- will omit any leading zeroes or spaces, %_ will force spaces for padding instead of the default, and %0 will force zeroes for padding. There's some redundancy here as %-d and %e have the same result, but it solves some awkwardness with formats like %l. Using %: for time zone offset, as in %:z will insert a colon as a delimiter.
Copyright 2010 - 2024 Sami Samhuri sami@samhuri.net
JavaScript
99.2%