Introduction
A date and time format string
defines the text representation of a Date()
object, that results in a custom format string
. Any string
that is not a standard date and time format string
is interpreted as a custom date and time format string
.
You can download the format.date
JavaScript Utility, an application that enables you to apply format string
s to date and time values and displays the result in string
format.
Background
In formatting operations, custom date and time format string
s can be used with the format method of a Date()
instance.
The format method requires that an input string conforms exactly to a particular pattern for the parse operation to succeed, otherwise it will give you an error (e.g. ERROR: Not supported method [method name]).
The following table describes the custom date and time format and displays a result string
produced by each format.
Format | Description | Date Example | Output |
"d" | The day of the month, from 1 through 31. | 5/1/2014 1:45:30 PM | 1 |
"dd" | The day of the month, from 01 through 31. | 5/1/2014 1:45:30 PM | 01 |
"ddd" | The abbreviated name of the day of the week. | 5/15/2014 1:45:30 PM | Thu |
"dddd" | The full name of the day of the week. | 5/15/2014 1:45:30 PM | Thursday |
"f" | The tenths of a second in a date and time value. | 5/15/2014 13:45:30.617 | 6 |
"ff" | The hundredths of a second in a date and time value | 5/15/2014 13:45:30.617 | 61 |
"fff" | The milliseconds in a date and time value. | 5/15/2014 13:45:30.617 | 617 |
"F" | If non-zero, the tenths of a second in a date and time value. | 5/15/2014 13:45:30.617 | 6 |
"FF" | If non-zero, the hundredths of a second in a date and time value. | 5/15/2014 13:45:30.617 | 61 |
"FFF" | If non-zero, the milliseconds in a date and time value. | 5/15/2014 13:45:30.617 | 617 |
"h" | The hour, using a 12-hour clock from 1 to 12. | 5/15/2014 1:45:30 AM | 1 |
"hh" | The hour, using a 12-hour clock from 01 to 12. | 5/15/2014 1:45:30 AM | 01 |
"H" | The hour, using a 24-hour clock from 0 to 23. | 5/15/2014 1:45:30 AM | 1 |
"HH" | The hour, using a 24-hour clock from 00 to 23. | 5/15/2014 1:45:30 AM | 01 |
"m" | The minute, from 0 through 59. | 5/15/2014 1:09:30 AM | 9 |
"mm" | The minute, from 00 through 59. | 5/15/2014 1:09:30 AM | 09 |
"M" | The month, from 1 through 12. | 5/15/2014 1:45:30 PM | 6 |
"MM" | The month, from 01 through 12. | 5/15/2014 1:45:30 PM | 06 |
"MMM" | The abbreviated name of the month. | 6/15/2014 1:45:30 PM | Jun |
"MMMM" | The full name of the month. | 6/15/2014 1:45:30 PM | June |
"s" | The second, from 0 through 59. | 5/15/2014 1:45:09 PM | 9 |
"ss" | The second, from 00 through 59. | 5/15/2014 1:45:09 PM | 09 |
"t" | The first character of the AM/PM designator. | 5/15/2014 1:45:09 PM | P |
"tt" | The AM/PM designator. | 5/15/2014 1:45:09 PM | PM |
"y" | The year, from 0 to 99. | 5/15/2014 1:45:09 PM | 9 |
"yy" | The year, from 00 to 99. | 5/15/2014 1:45:09 PM | 09 |
"yyy" | The year, with a minimum of three digits. | 5/15/2009 1:45:30 PM | 2009 |
"yyyy" | The year as a four-digit number. | 5/15/2009 1:45:30 PM | 2009 |
"yyyyy" | The year as a five-digit number. | 5/15/2009 1:45:30 PM | 02009 |
"z" | Hours offset from UTC, with no leading zeros. | 5/15/2014 1:45:30 PM -07:00 | -7 |
"zz" | Hours offset from UTC, with a leading zero for a single-digit value. | 5/15/2014 1:45:30 PM -07:00 | -07 |
"zzz" | Hours and minutes offset from UTC. | 5/15/2014 1:45:30 PM -07:00 | -07:00 |
"st" | Date ordinal (st, nd, rd and th) display from day of the date. | 5/15/2014 1:45:30 PM | 15th |
Using the Code
var dayNames = ['Sun', 'Mon', 'Tues', 'Wednes', 'Thurs', 'Fri', 'Satur'];
var monthNames = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'];
Date.prototype.format = function (format) {
var wordSplitter = /\W+/, _date = this;
this.Date = function (format) {
var words = format.split(wordSplitter);
words.forEach(function(w) {
if (typeof(wordReplacer[w]) === "function") {
format = format.replace(w, wordReplacer[w]() );
}
else {
wordReplacer['e'](w);
}
});
return format.replace(/\s+(?=\b(?:st|nd|rd|th)\b)/g, "");
};
var wordReplacer = {
d : function() {
return _date.getDate();
},
dd : function() {
return _pad(_date.getDate(),2);
},
ddd : function() {
return dayNames[_date.getDay()].slice(0,3);
},
dddd : function() {
return dayNames[_date.getDay()] + 'day';
},
f : function() {
return parseInt(_date.getMilliseconds() / 100) ;
},
ff : function() {
return parseInt(_date.getMilliseconds() / 10) ;
},
fff : function() {
return _date.getMilliseconds() ;
},
F : function() {
return (_date.getMilliseconds() / 100 > 0) ? parseInt(_date.getMilliseconds() / 100) : '' ;
},
FF : function() {
return (_date.getMilliseconds() / 10 > 0) ? parseInt(_date.getMilliseconds() / 10) : '' ;
},
FFF : function() {
return (_date.getMilliseconds() > 0) ? _date.getMilliseconds() : '' ;
},
h : function() {
return _date.getHours() % 12 || 12;
},
hh : function() {
return _pad(_date.getHours() % 12 || 12, 2);
},
H : function() {
return _date.getHours();
},
HH : function() {
return _pad(_date.getHours(),2);
},
m : function() {
return _date.getMinutes()();
},
mm : function() {
return _pad(_date.getMinutes(),2);
},
M : function() {
return _date.getMonth() + 1;
},
MM : function() {
return _pad(_date.getMonth() + 1,2);
},
MMM : function() {
return monthNames[_date.getMonth()].slice(0, 3);
},
MMMM : function() {
return monthNames[_date.getMonth()];
},
s : function() {
return _date.getSeconds();
},
ss : function() {
return _pad(_date.getSeconds(),2);
},
t : function() {
return _date.getHours() >= 12 ? 'P' : 'A';
},
tt : function() {
return _date.getHours() >= 12 ? 'PM' : 'AM';
},
y : function() {
return Number(_date.getFullYear().toString().substr(2,2));
},
yy : function() {
return _pad(_date.getFullYear().toString().substr(2,2),2);
},
yyy : function() {
var _y = Number(_date.getFullYear().toString().substr(1,2));
return _y > 100 ? _y : _date.getFullYear();
},
yyyy : function() {
return _date.getFullYear();
},
yyyyy : function() {
return _pad(_date.getFullYear(),5);
},
z : function() {
return parseInt(_date.getTimezoneOffset() / 60) ;
},
zz : function() {
var _h = parseInt(_date.getTimezoneOffset() / 60);
if(_h < 0) _h = '-' + _pad(Math.abs(_h),2);
return _h;
},
zzz : function() {
var _h = parseInt(_date.getTimezoneOffset() / 60);
var _m = _date.getTimezoneOffset() - (60 * _h);
var _hm = _pad(_h,2) +':' + _pad(Math.abs(_m),2);
if(_h < 0) _hm = '-' + _pad(Math.abs(_h),2) +':' + _pad(Math.abs(_m),2);
return _hm;
},
st: function () {
var _day = wordReplacer.d();
return _day < 4 | _day > 20 && ['st', 'nd', 'rd'][_day % 10 - 1] || 'th';
},
e: function (method) {
throw 'ERROR: Not supported method [' + method + ']';
}
};
_pad = function (n, c) {
if ((n = n + '').length < c) {
return new Array((++c) - n.length).join('0') + n;
}
return n;
}
return this.Date(format);
}
In this section, I will show you how to use Date()
object with custom formatting.
The "dd" Custom Format
The "dd" custom format string represents the day of the month as a number from 01 through 31.
A single-digit day is formatted with a leading zero. The following example includes the "dd
" custom format specifier in a custom format string
.
date = new Date();
console.log(date.format('dd st-MMM-yyyy'));
The "dddd" Custom Format
The "dddd" custom format string represents the full name of the day of the week.
A day name of the day of week. The following example includes the "dddd
" custom format specifier in a custom format string
.
date = new Date();
console.log(date.format('dddd, dd/M/yy'));
The "ff" Custom Format
The "ff" custom format specifier represents the two most significant digits of the seconds fraction; that is, it represents the hundredths of a second in a date and time value.
The following example includes the "ff
" custom format specifier in a custom format string
.
date = new Date();
console.log(date.format('dddd, dd/M/yy hh:mm:ss.ff'));
The "HH" Custom Format
The "HH" custom format specifier (plus any number of additional "H" specifiers) represents the hour as a number from 00 through 23; that is, the hour is represented by a zero-based 24-hour clock that counts the hours since midnight. A single-digit hour is formatted with a leading zero.
The following example includes the "HH
" custom format specifier in a custom format string
.
date = new Date();
console.log(date.format('HH:mm:ss'));
The "tt" Custom Format
The "t" custom format specifier represents the first character of the AM/PM designator.
The following example includes the "tt
" custom format specifier in a custom format string
.
date = new Date();
console.log(date.format('dd-MM-y HH:mm:ss tt'));
The "st" Custom Format
The "st
" custom format string represents the ay ordinal of the day (eg. st, nd, rd and th).
The following example includes the "st
" custom format specifier in a custom format string
.
date = new Date();
console.log(date.format('ddd, dd st-MMM-yyyy HH:mm:ss tt zz'));
Points of Interest
Using the above date and time format utility, you can generate more than thousands of various formatting strings as you like or as per your requirements.
Please feel free to ask me if you would require any help for the same.
Your valuable feedback, comments, suggestions are highly appreciated.