Date (object extension)

Categories:Objects

Overview

Summary

Additional functionality for native Date object

Contents

API

format(format)

Helper function to get a nicely formatted date string, simulates PHP's date function

Parameters:

  • format String A String format

Returns:

  •   String A formatted date string

The following example formats a date object in standard British format:

var date = new Date();
trace(date.format('l jS F Y'));
Thursday 14th June 2012

The following formats are valid within the format parameter:

Dates

Date
  • dday of the month, 2 digits with leading zeros; i.e. "01" to "31"
  • jday of the month without leading zeros; i.e. "1" to "31"
  • SEnglish ordinal suffix for the day of the month, 2 characters; i.e. "th", "nd"
  • tnumber of days in the given month; i.e. "28" to "31"
Day
  • Dday of the week, textual, 3 letters; i.e. "Fri"
  • l (lowercase 'L')day of the week, textual, long; i.e. "Friday"
  • wday of the week, numeric, i.e. "0" (Sunday) to "6" (Saturday)
Month
  • Fmonth, textual, long; i.e. "January"
  • mmonth; i.e. "01" to "12"
  • Mmonth, textual, 3 letters; i.e. "Jan"
  • nmonth without leading zeros; i.e. "1" to "12"
Year
  • Yyear, 4 digits; i.e. "1999"
  • yyear, 2 digits; i.e. "99"

Times

Hour
  • ghour, 12-hour format without leading zeros; i.e. "1" to "12"
  • Ghour, 24-hour format without leading zeros; i.e. "0" to "23"
  • hhour, 12-hour format; i.e. "01" to "12"
  • Hhour, 24-hour format; i.e. "00" to "23"
Minute
  • iminutes; i.e. "00" to "59"
Second
  • sseconds; i.e. "00" to "59"
am /pm
  • a"am" or "pm"
  • A"AM" or "PM"

Other

Units passed
  • Useconds since the epoch
  • zday of the year; i.e. "0" to "365"
  • WISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0) (Saturday)
Time zone
  • I (capital i)"1" if Daylight Savings Time, "0" otherwise.
  • Odifference to Greenwich time in hours; i.e. "+0200"
  • Ttime zone setting of this machine; i.e. "MDT"
  • Ztime zone offset in seconds (i.e. "-43200" to "43200"). The offset for time zones west of UTC is always negative, and for those east of UTC is always positive.
Other
  • BSwatch Internet time
  • Lboolean for whether it is a leap year; i.e. "0" or "1"
  • rRFC 822 formatted date; i.e. "Thu, 21 Dec 2000 16:01:07 +0200" (added in PHP 4.0.4)

Comments are closed.