jsDate Usage
Setup
First, include the source file:
<script type="text/javascript" src="jsdate.js"></script>
jsDate exposes one member, jsDate. If you have loaded jQuery prior to jsDate, jsDate is accessible as jQuery.jsDate (or $.jsDate depending on how you have jQuery configured). If jQuery is not loaded, jsDate is accessible as a global variable on the page.
Basic Usage, functional implementation
Simple date formatting can be accomplished through the jsDate.strftime() function:
>>> jsDate.strftime('Feb 8, 2006 8:48:32', '%Y-%m-%d %H:%M:%S'); "2006-02-08 08:48:32"
By default jsDate uses the perl strftime syntax. PHP syntax can also be used, by passing in an additional argument to the jsDate function:
>>> jsDate.strftime('Feb 8, 2006 8:48:32', '%j day of the %y year of the %C century in the %P', 'php'); "039 day of the 06 year of the 20 century in the am"
You can also pass a timestamp or a Date or jsDate object to the strftime function:
>>> jsDate.strftime(1139406512000, '%j day of the %y year of the %C century in the %P', 'php') "039 day of the 06 year of the 20 century in the am" >>> dd = new Date(1139406512000) Wed Feb 08 2006 08:48:32 GMT-0500 (Eastern Standard Time) >>> jsDate.strftime(dd, '%j day of the %y year of the %C century in the %P', 'php') "039 day of the 06 year of the 20 century in the am"
strftime Format Codes
Perl Syntax: Code Result Description == Years == %Y 2008 Four-digit year %y 08 Two-digit year == Months == %m 09 Two-digit month %#m 9 One or two-digit month %B September Full month name %b Sep Abbreviated month name == Days == %d 05 Two-digit day of month %#d 5 One or two-digit day of month %e 5 One or two-digit day of month %A Sunday Full name of the day of the week %a Sun Abbreviated name of the day of the week %w 0 Number of the day of the week (0 = Sunday, 6 = Saturday) == Hours == %H 23 Hours in 24-hour format (two digits) %#H 3 Hours in 24-hour integer format (one or two digits) %I 11 Hours in 12-hour format (two digits) %#I 3 Hours in 12-hour integer format (one or two digits) %p PM AM or PM == Minutes == %M 09 Minutes (two digits) %#M 9 Minutes (one or two digits) == Seconds == %S 02 Seconds (two digits) %#S 2 Seconds (one or two digits) %s 1206567625723 Unix timestamp (Seconds past 1970-01-01 00:00:00) == Milliseconds == %N 008 Milliseconds (three digits) %#N 8 Milliseconds (one to three digits) == Timezone == %O 360 difference in minutes between local time and GMT %Z Mountain Standard Time Name of timezone as reported by browser %G 06:00 Hours and minutes between GMT == Shortcuts == %F 2008-03-26 %Y-%m-%d %T 05:06:30 %H:%M:%S %X 05:06:30 %H:%M:%S %x 03/26/08 %m/%d/%y %D 03/26/08 %m/%d/%y %#c Wed Mar 26 15:31:00 2008 %a %b %e %H:%M:%S %Y %v 3-Sep-2008 %e-%b-%Y %R 15:31 %H:%M %r 03:31:00 PM %I:%M:%S %p == Characters == %n Newline %t Tab %% % Percent Symbol PHP Syntax: Code Result Description === Days === %a Sun through Sat An abbreviated textual representation of the day %A Sunday - Saturday A full textual representation of the day %d 01 to 31 Two-digit day of the month (with leading zeros) %e 1 to 31 Day of the month, with a space preceding single digits. %j 001 to 366 Day of the year, 3 digits with leading zeros %u 1 - 7 (Mon - Sun) ISO-8601 numeric representation of the day of the week %w 0 - 6 (Sun - Sat) Numeric representation of the day of the week === Week === %U 13 Full Week number, starting with the first Sunday as the first week %V 01 through 53 ISO-8601:1988 week number, starting with the first week of the year with at least 4 weekdays, with Monday being the start of the week %W 46 A numeric representation of the week of the year, starting with the first Monday as the first week === Month === %b Jan through Dec Abbreviated month name, based on the locale %B January - December Full month name, based on the locale %h Jan through Dec Abbreviated month name, based on the locale (an alias of %b) %m 01 - 12 (Jan - Dec) Two digit representation of the month === Year === %C 19 Two digit century (year/100, truncated to an integer) %y 09 for 2009 Two digit year %Y 2038 Four digit year === Time === %H 00 through 23 Two digit representation of the hour in 24-hour format %I 01 through 12 Two digit representation of the hour in 12-hour format %l 1 through 12 Hour in 12-hour format, with a space preceeding single digits %M 00 through 59 Two digit representation of the minute %p AM/PM UPPER-CASE 'AM' or 'PM' based on the given time %P am/pm lower-case 'am' or 'pm' based on the given time %r 09:34:17 PM Same as %I:%M:%S %p %R 00:35 Same as %H:%M %S 00 through 59 Two digit representation of the second %T 21:34:17 Same as %H:%M:%S %X 03:59:16 Preferred time representation based on locale, without the date %z -0500 or EST Either the time zone offset from UTC or the abbreviation %Z -0500 or EST The time zone offset/abbreviation option NOT given by %z === Time and Date === %D 02/05/09 Same as %m/%d/%y %F 2009-02-05 Same as %Y-%m-%d (commonly used in database datestamps) %s 305815200 Unix Epoch Time timestamp (same as the time() function) %x 02/05/09 Preferred date representation, without the time === Miscellaneous === %n --- A newline character ( ) %t --- A Tab character ( ) %% --- A literal percentage character (%)
Advanced Usage, jsDate objects
You can also create a jsDate instance which can be manipulated and re-used:
>>> var d = new jsDate(); >>> d.toString(); "Thu Dec 02 2010 15:27:36 GMT-0500 (Eastern Standard Time)" >>> d.strftime('%Y/%m/%d %#I:%M %p'); "2010/12/02 3:28 PM" >>> d.add(3, 'days'); >>> d.strftime('%Y/%m/%d %#I:%M %p'); "2010/12/05 3:28 PM" >>> d.add(-82, 'minutes'); >>> d.strftime('%Y/%m/%d %#I:%M %p'); "2010/12/05 2:06 PM" >>> d.setFullYear(2012, 9, 15); >>> d.strftime('%Y/%m/%d %#I:%M %p'); "2012/10/15 2:06 PM" >>> d.set('Jan 1, 1999 3:43 pm'); >>> d.strftime('%Y/%m/%d %#I:%M %p'); "1999/01/01 3:43 PM"
Localazation
jsDate is locale capable. When loaded, jsDate will set it's locale based on the lang property of the html document. If the document has no lang property, or if jsDate does not have a localization for the given lang property, it will default to 'en-US'. Currently, 'en', 'fr', 'de', 'ar', 'ru', 'pt' and 'pt-BR' localizations are defined. The 'en-US' and 'en-GB' localizations are mapped to 'en'. Note that the "morning" and "afternoon" (am/pm) suffixes are not yet localized. Below are some locale examples:
>>> d = new jsDate('1999-01-01 03:43:00'); >>> d.strftime('%A %B %d, %Y %#I:%M %p'); "Friday January 01, 1999 3:43 PM" >>> d.locale = 'fr'; >>> d.strftime('%A %B %d, %Y %#I:%M %p'); "Vendredi Janvier 01, 1999 3:43 PM"
The functional interface also accepts a localization argument. Note that both a syntax and/or locale argument can be supplied in any order, jsDate will figure out which is which:
>>> jsDate.strftime('1999-01-01 03:43:00', '%A %B %d, %Y %#I:%M %p', 'fr'); "Vendredi Janvier 01, 1999 3:43 AM"
The strftime function can also be called without a format string. In this case, a jsDate instance will look for a format string defined on it's "formatString" property. If that is empty, it will default to the "formatString" property for the given locale. The jsDate functional interface will default to the locale format string. NOTE: currently all locales have the same format string "%Y-%m-%d %H:%M:%S". This property is not yet localized.
>>> d = new jsDate('1999-01-01 03:43:00'); >>> d.strftime(); "1999-01-01 03:43:00" >>> d.formatString = '%A %B %d, %Y %#I:%M %p'; >>> d.strftime(); "Friday January 01, 1999 3:43 AM" >>> jsDate.strftime(d.getTime()) "1999-01-01 03:43:00"