Skip to content

Instantly share code, notes, and snippets.

@kupriyanenko
Created February 1, 2017 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kupriyanenko/2ecdae0a0e5b7ea5f377ad0f98c743aa to your computer and use it in GitHub Desktop.
Save kupriyanenko/2ecdae0a0e5b7ea5f377ad0f98c743aa to your computer and use it in GitHub Desktop.
/**
* Formatter
*
* date(Object Date, String)
* http://pubs.opengroup.org/onlinepubs/007908799/xsh/strftime.html
* + bonus: milliseconds as %SSS
*/
/**
* Accept source object for converting
* @param {Date|String|Number} obj
* @param {String} str
* @param {String=} locale
* @return {String}
*/
export default function date (obj, str, locale = navigator.language) {
// Fix time string for some JS engine and convert it to a Date
if (!obj) {
obj = new Date();
}
/**
* bad time string format "2014-08-22T12:06:30+0200" (for iOS with spaces)
* good time string format "2014-08-19T18:59:21+02:00"
*/
if (typeof obj === 'string' && /(\-|\+)\d{4}$/.test(obj)) {
obj = new Date(obj.slice(0, obj.length - 2) + ':' + obj.slice(obj.length - 2));
}
/**
* bad time string format "2014-08-22 12:06:30.0200" (for iOS with spaces)
* goot time string format "2014-08-22T12:06:30.0200"
*/
else if (typeof obj === 'string' && / /.test(obj)) {
obj = new Date(obj.replace(' ', 'T'));
} else if (!(obj instanceof Date)) {
obj = new Date(obj);
}
/**
* Convert Date to a String
* @private
* @param {String} str
* @return {String}
*/
var _formatDate = function(str) {
if (typeof str !== 'string') {
str = String(str);
}
return str.replace(/%%|%(SSS|[abcCdDeEHIjmMprRSTxXyYzZ])/g, function(match, code, index, str) {
if (match === '%%') {
return '%';
} else {
return _formatDate[code](obj);
}
});
};
/**
* Pad a number with leading zeros
* @private
* @param {Number} n
* @param {String} placer
* @param {Number} len
* @return {String}
*/
_formatDate._padZero = function(n, placer, len) {
if (typeof placer === 'undefined') {
placer = '0';
}
if (typeof len === 'undefined') {
len = 2;
}
n = placer + n.toString();
return n.substr(-len);
};
_formatDate.a = function(date) {
return date.toDateString().split(' ')[0];
};
_formatDate.b = function(date) {
return date.toDateString().split(' ')[1];
};
_formatDate.C = function(date) {
return date.getFullYear().toString().substr(0, 2);
};
_formatDate.c = function(date) {
return date.toLocaleString();
};
_formatDate.D = function() {
return _formatDate('%m/%d/%y');
};
_formatDate.d = function(date) {
return _formatDate._padZero(date.getDate());
};
_formatDate.e = function(date) {
return _formatDate._padZero(date.getMonth() + 1, ' ');
};
_formatDate.E = function(date) {
var sufix,
value,
month = date.getMonth() + 1;
switch (locale) {
case 'en-US':
sufix = ['th', 'st', 'nd', 'rd'];
value = month % 100;
month += sufix[(value - 20) % 10] || sufix[value] || sufix[0];
break;
case 'ru-RU':
month += '-e';
break;
case 'da-DK':
case 'sv-SE':
month += ':e';
break;
case 'de-DE':
month += '.';
break;
case 'nl-NL':
case 'fr-FR':
month += 'e';
break;
case 'ro-RO':
month += 'a';
break;
default:
month += '';
break;
}
return month;
};
_formatDate.H = function(date) {
return _formatDate._padZero(date.getHours());
};
_formatDate.I = function(date) {
return _formatDate._padZero(date.getHours() % 12);
};
_formatDate.j = function(date) {
var day = 0,
i = 0,
len = date.getMonth();
for (; i < len; i++) {
day += 30;
}
return _formatDate._padZero(day + date.getDate(), '0', 3);
};
_formatDate.m = function(date) {
return _formatDate._padZero(date.getMonth() + 1);
};
_formatDate.M = function(date) {
return _formatDate._padZero(date.getMinutes());
};
_formatDate.p = function(date) {
return ['AM', 'PM'][Math.floor(date.getHours() / 12)];
};
_formatDate.r = function(date) {
return _formatDate('%I:%M:%S %p');
};
_formatDate.R = function(date) {
return _formatDate('%H:%M');
};
_formatDate.S = function(date) {
return _formatDate._padZero(date.getSeconds());
};
_formatDate.SSS = function(date) {
return date.getMilliseconds();
};
_formatDate.T = function() {
return _formatDate('%H:%M:%S');
};
_formatDate.x = function(date) {
return date.toLocaleDateString();
};
_formatDate.X = function(date) {
return date.toLocaleTimeString();
};
_formatDate.y = function(date) {
return date.getFullYear().toString().substr(2);
};
_formatDate.Y = Date.prototype.getFullYear.bind(obj);
_formatDate.Z = function(date) {
var timeZone = date.getTimezoneOffset();
return (timeZone > 0 ? '+' : '-') + _formatDate._padZero(Math.abs(timeZone), '0', 4);
};
if (Object.prototype.toString.call(obj) === '[object Date]') {
/**
* Date formatter
* @param {String} str
* @return {String}
*/
return _formatDate(str);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment