Valid XHTML 1.0! Valid CSS!

DateFormatter.js Hatena bookmark

Summary

It is format pattern of the date function of PHP and the date format library of JavaScript which has compatibility. However, because it is not the case that all patterns are mounted, please note. Japanese day of the week it is possible as the extended function which is not in PHP, to indicate the month name of the old calendar.

Download

Please download from link below.

Tutorial

DateFormatter.js is loaded.

<script type="text/javascript" src="./DateFormatter.js"></script>

The format is done with format method of DateFormatter object. For example, when indicating present date time, it makes like below.

var now = new Date();
alert(DateFormatter.format(now, "Y/m/d H:i:s"));

As for indicatory result “2007/01/15 16:30:12”.

When the escape it does format letter, the # is attached immediately before. For example, when “Ymd is date” we would like to indicate, it becomes like below.

var now = new Date();
alert(DateFormatter.format(now, "#Y#m#d #i#s Ymd"));

In order each time without coding the call of DateFormatter not to have, there is also a method of expanding Date object.

Date.prototype.format = function(pattern) {
  return DateFormatter.format(this, pattern);
}
var now = new Date();
alert(now.format("Y/m/d H:i:s"));

The male is completed is not, but by the fact that it makes global function, from also method of writing which PHP seems is possible.

function date(pattern, time) {
  var d = new Date();
  if (time != null) {
    d.setTime(time);
  }
  return DateFormatter.format(d, pattern);
}
alert(date("Y/m/d H:i:s"));

Format

PatternExplanation
YA full numeric representation of a year, 4 digits
yA two digit representation of a year
mNumeric representation of a month, with leading zeros
nNumeric representation of a month, without leading zeros
FA full textual representation of a month, such as January or March
MA short textual representation of a month, three letters
OJapanese old month name
dDay of the month, 2 digits with leading zeros
jDay of the month without leading zeros
wNumeric representation of the day of the week
lA full textual representation of the day of the week
DA textual representation of a day, three letters
NISO-8601 numeric representation of the day of the week
JA Japanese textual representation of the day of the week
g12-hour format of an hour without leading zeros
G24-hour format of an hour without leading zeros
h12-hour format of an hour with leading zeros
H24-hour format of an hour with leading zeros
iMinutes with leading zeros
sSeconds, with leading zeros
aLowercase Ante meridiem and Post meridiem (am or pm)
AUppercase Ante meridiem and Post meridiem (AM or PM)
SEnglish ordinal suffix for the day of the month, 2 characters
zThe day of the year (starting from 0)
tNumber of days in the given month
LWhether it's a leap year

Tool

It is form for the test. Please try inputting format pattern.

 

License

It makes the BSD license. Use of the software (execution) it is free without restriction, but the case of redistribution, with the source code that, it is necessary to include the list disclaimer of copyright notice condition with that binary.