Skip to content

Instantly share code, notes, and snippets.

@leplay
Last active February 23, 2018 05:58
Show Gist options
  • Select an option

  • Save leplay/939cd270d418e56b67b0 to your computer and use it in GitHub Desktop.

Select an option

Save leplay/939cd270d418e56b67b0 to your computer and use it in GitHub Desktop.
FormatDate.js
(function (window) {
define([], function () {
var FormatDate = function (format, date) {
date = date ? new Date(parseInt(date, 10)) : new Date();
var output = format.replace(/y{4}|D{2}|M{2}|d{2}|h{2}|H{2}|m{2}|s{2}/g, function (keyword) {
var result = '';
switch (keyword) {
case 'yyyy':
result = date.getFullYear();
break;
case 'MM':
result = date.getMonth() + 1;
if (result < 10) {
result = '0' + result;
}
break;
case 'dd':
result = date.getDate();
if (result < 10) {
result = '0' + result;
}
break;
case 'HH':
var HH = date.getHours();
if (HH < 10) {
result = '0' + HH;
} else {
result = HH;
}
break;
case 'hh':
var hh = date.getHours();
if (hh > 12) {
hh = hh - 12;
}
if (hh < 10) {
result = '0' + hh;
} else {
result = hh;
}
break;
case 'mm':
var mm = date.getMinutes();
if (mm < 10) {
result = '0' + mm;
} else {
result = mm;
}
break;
case 'ss':
var ss = date.getSeconds();
if (ss < 10) {
result = '0' + ss;
} else {
result = ss;
}
break;
case 'DD':
var DD = date.getDay();
break;
}
return result;
});
};
return FormatDate;
});
}(this));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment