• Stars
    star
    651
  • Rank 69,175 (Top 2 %)
  • Language
    JavaScript
  • Created about 14 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

strftime for JavaScript

strftime

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.

version 0.10.2 on npm node version 0.2 and up MIT License

Installation

node:

npm install strftime

bower:

bower install strftime

component:

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.

Changelog

View the changelog.

Usage

    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

Supported Specifiers

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: full weekday name
  • a: abbreviated weekday name
  • B: full month name
  • b: abbreviated month name
  • C: AD century (year / 100), padded to 2 digits
  • c: equivalent to %a %b %d %X %Y %Z in en_US (based on locale)
  • D: equivalent to %m/%d/%y in en_US (based on locale)
  • d: day of the month, padded to 2 digits (01-31)
  • e: day of the month, padded with a leading space for single digit values (1-31)
  • F: equivalent to %Y-%m-%d in en_US (based on locale)
  • H: the hour (24-hour clock), padded to 2 digits (00-23)
  • h: the same as %b (abbreviated month name)
  • I: the hour (12-hour clock), padded to 2 digits (01-12)
  • j: day of the year, padded to 3 digits (001-366)
  • k: the hour (24-hour clock), padded with a leading space for single digit values (0-23)
  • L: the milliseconds, padded to 3 digits [Ruby extension]
  • l: the hour (12-hour clock), padded with a leading space for single digit values (1-12)
  • M: the minute, padded to 2 digits (00-59)
  • m: the month, padded to 2 digits (01-12)
  • n: newline character
  • o: day of the month as an ordinal (without padding), e.g. 1st, 2nd, 3rd, 4th, ...
  • P: "am" or "pm" in lowercase (Ruby extension, based on locale)
  • p: "AM" or "PM" (based on locale)
  • R: equivalent to %H:%M in en_US (based on locale)
  • r: equivalent to %I:%M:%S %p in en_US (based on locale)
  • S: the second, padded to 2 digits (00-60)
  • s: the number of seconds since the Epoch, UTC
  • T: equivalent to %H:%M:%S in en_US (based on locale)
  • t: tab character
  • U: week number of the year, Sunday as the first day of the week, padded to 2 digits (00-53)
  • u: the weekday, Monday as the first day of the week (1-7)
  • v: equivalent to %e-%b-%Y in en_US (based on locale)
  • W: week number of the year, Monday as the first day of the week, padded to 2 digits (00-53)
  • w: the weekday, Sunday as the first day of the week (0-6)
  • X: equivalent to %T or %r in en_US (based on locale)
  • x: equivalent to %D in en_US (based on locale)
  • Y: the year with the century
  • y: the year without the century, padded to 2 digits (00-99)
  • Z: the time zone name, replaced with an empty string if it is not found
  • z: the time zone offset from UTC, with a leading plus sign for UTC and zones east of UTC and a minus sign for those west of UTC, hours and minutes follow each padded to 2 digits and with no delimiter between them

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.

Bundled Locales

  • de_DE
  • en_CA
  • en_US
  • es_MX
  • fr_FR
  • it_IT
  • nl_NL
  • pt_BR
  • ru_RU
  • tr_TR
  • zh_CN

Contributors

License

Copyright 2010 - 2023 Sami Samhuri [email protected]

MIT license

More Repositories

1

json-diff

show the differences between JSON-encoded objects
JavaScript
79
star
2

elisp.js

Emacs Lisp interpreter in JavaScript
JavaScript
78
star
3

format

printf-like formatting for JavaScript
JavaScript
51
star
4

compiler

an x86 compiler written in ruby
Ruby
31
star
5

LayerVideoCompositor

An example of using a custom video compositor to overlay a layer on a video.
Swift
22
star
6

gitter

GitHub client for node and browsers (v3 API)
JavaScript
20
star
7

cpwebsocket

WebSockets for Cappuccino
Objective-J
14
star
8

config

config files for linux and os x
Emacs Lisp
10
star
9

repl-edit

edit Node repl commands with your text editor
JavaScript
6
star
10

bin

unix shell scripts
Perl
5
star
11

mojo.el

Mojo package for Emacs
Emacs Lisp
4
star
12

kernel

kernel based on http://www.jamesmolloy.co.uk/tutorial_html/
C
4
star
13

ThePusher

Github post-receive hook router
JavaScript
4
star
14

Marshmallows

Marshmallows sweeten up your Cocoa for iOS
Objective-C
3
star
15

samhuri.net

the contents of samhuri.net
JavaScript
3
star
16

lake

Lake Scheme
C
3
star
17

5by5Browser

5by5 episode browser for my favourite shows
Objective-C
2
star
18

batteries

a general purpose node library
JavaScript
2
star
19

Advanced-NSOperations

WWDC 2015 sample code, updated in 2022 for modern tools
Swift
2
star
20

blog

scratch pad for an old blog (moved to https://samhuri.net now)
Emacs Lisp
2
star
21

gurulogic.ca

guru logic website
CSS
2
star
22

NorthWatcher

cron for filesystem changes
JavaScript
1
star
23

Mojo.Ext

Extensions to Mojo for webOS
JavaScript
1
star
24

biggist

embiggens gists
JavaScript
1
star
25

CappuccinoIssue725

Objective-J
1
star
26

watch-yourself

personal blood pressure and heart rate monitor
Ruby
1
star
27

DeferredVis-server

JavaScript
1
star
28

mystery7-simulator

Simulate spins for Mystery7
Ruby
1
star
29

elschemo

A scheme implementation in Haskell based on J.D. Tang's tutorial
Haskell
1
star
30

stormy-weather

A web server blueprint using Sinatra and Redis
Ruby
1
star
31

ControlCenter_iOS

A SwiftUI approximation of the new Control Center on macOS 11
Swift
1
star
32

SJSAssetExportSession

An alternative to AVAssetExportSession with customizable audio and video settings
Swift
1
star