r7 - 23 Jun 2006 - 11:04:05 - MatthewEernisseYou are here: OSAF >  Documentation Web  >  ScoobyJavaScriptLibraries > JavaScriptStrftime

Date.strftime

Library

date.js

Syntax

dt.strftime(format); (where dt is a JavaScript Date object)

Date.strftime(format, timestamp);

Parameters

format A string indicating how the date is to be formatted.

timestamp A Unix timestamp of the date to be formatted.

Description

Returns a string formatted according to the given format string. Accepts either a JavaScript Date object, or a Unix-style timestamp.

This is a very basic implementation that only uses English-language formatting, but it is set up to make it relatively easy to add your own localized language.

Formatting

The format string may include the following specifiers:

%a -- Abbreviated weekday name (ex. Thu)
%A -- Full weekday name (ex. Thursday)
%b -- Abbreviated month name (ex. Nov)
%B -- Full month name (ex. November)
%c -- Appropriate date and time representation (ex. Thu Nov 03 13:10:35 2005)
%C -- Century number (the year divided by 100 and truncated to an integer, range 00 to 99), single digits are preceded by zero (ex. 20)
%d -- Day of month (range 01 to 31), single digits are preceded by zero (ex. 03)
%D -- Date as %m/%d/%y (ex. 11/03/05)
%e -- Day of the month as a decimal number (range ' 1' to '31') -- a single digit is preceded by a space (ex. ' 3')
%h -- Same as %b (ex. Nov)
%H -- Hour as a decimal number using a 24-hour clock (range 00 to 23), single digits are preceded by zero (ex. 13)
%I -- Hour as a decimal number using a 12-hour clock (range 01 to 12), single digits are preceded by zero (ex. 01)
%j -- Day of the year as a decimal number (range 001 to 366) -- zero-padded to three digits (ex. 307)
%k -- Hour as a decimal number using a 24-hour clock (range ' 0' to '23'), single digits are preceded by a space (ex. '13')
%l -- Hour as a decimal number using a 12-hour clock (range ' 1' to '12'), single digits are preceded by a space (ex. ' 1')
%m -- Month as a decimal number (range 01 to 12), single digits are preceded by zero (ex. 11)
%M -- Minute as a decimal number (range 00 to 59), single digits are preceded by zero (ex. 10)
%p -- Either 'AM' or 'PM' according to the given time value (ex. PM)
%r -- Appropriate time representation in 12-hour clock format with %p (ex. 01:10:35 PM)
%R -- Time as %H:%M (ex. 13:10)
%S -- Second as a decimal number (range 00 to 59), single digits are preceded by zero (ex. 35)
%T -- Time as %H:%M:%S (ex. 13:10:35)
%u -- Weekday as a decimal number (range 1 to 7), with 1 representing Monday (ex. 4)
%w -- Weekday as a decimal number (range 0 to 6), with 0 representing Sunday (ex. 4)
%x -- Appropriate date representation without the time (ex. 11/03/05)
%X -- Appropriate time representation without the date (ex. 01:10:35)
%y -- Year as a decimal number without a century (range 00 to 99), single digits are preceded by zero (ex. 05)
%Y -- Year as a decimal number including the century (ex. 2005)
%% -- A literal percent character (ex. %)

Examples

The example below will return a string that says "My birthdate was Friday, December 27, 1968."

var bd = new Date('12/27/1968');
return bd.strftime('My birthdate was %A, %B %e, %Y.');

You can also use it with a Unix timestamp, as a static method of Date, by passing the timestamp as a second param. The example below will return a string that says "I found a guitar in October of 2112."

var dt = new Date('10/01/2112');
var stamp = dt.getTime();
return Date.strftime('I found a guitar in %B of %Y.', stamp);
Edit | WYSIWYG | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r7 < r6 < r5 < r4 < r3 | More topic actions
 
Open Source Applications Foundation
Except where otherwise noted, this site and its content are licensed by OSAF under an Creative Commons License, Attribution Only 3.0.
See list of page contributors for attributions.