Skip to content

Instantly share code, notes, and snippets.

@Badgerati
Last active March 4, 2016 12:44
Show Gist options
  • Save Badgerati/7776317 to your computer and use it in GitHub Desktop.
Save Badgerati/7776317 to your computer and use it in GitHub Desktop.
Converts the date passed into a string of the format passed.
/**
* Converts the date passed into a string of the format passed.
*
* If the date is null, then today's date is used.
* If the format is null, then a default format of 'dd/MM/yyyy hh:mm:ss' is used.
*
* Possible format attributes are:
*
* - dd - 2 digit date (01, 12, 30).
* - ddd - Ordinal date (1st, 12th, 23rd).
*
* - DDD - 3 letter day name (Mon, Tue, Wed).
* - DDDD - Full day name (Monday, Wednesday).
*
* - MM - 2 digit month (01, 09, 12).
* - MMM - 3 letter month name (Jan, Feb Mar).
* - MMMM - Full month name (January, February).
*
* - yy - 2 digit year (01, 10, 13).
* - yyyy - Full year (2001, 2013).
*
* - hh - 2 digit hour in 12hr time (01, 12, 05).
* - hhh - 2 digit hour in 24hr time (01, 12, 17).
*
* - mm - 2 digit minute (01, 33, 59).
*
* - ss - 2 digit seconds (01, 33, 59).
*
* Examples:
*
* - 'dd/MM/yyyy hh:mm:ss'
* -> '09/03/2005 05:21:14'
*
* - 'DDDD, ddd MMMM yyyy hhh:mm:ss'
* -> 'Tuesday, 3rd December 2013 19:37:41'
*/
function toDateString(date, format) {
if (date == null) {
date = new Date();
}
if (format == null || format == '') {
format = 'dd/MM/yyyy hh:mm:ss';
}
var currentDay = date.getDay();
var currentDate = date.getDate();
var currentMonth = date.getMonth();
var currentHour = date.getHours();
var currentYear = date.getFullYear();
//date
if (contains(format, 'ddd')) {
var ordinalDate = currentDate.toString();
if (currentDate == 11 || currentDate == 12 || currentDate == 13) {
ordinalDate = currentDate + 'th';
}
else if (endsWith(ordinalDate, '1')) {
ordinalDate += 'st';
}
else if (endsWith(ordinalDate, '2')) {
ordinalDate += 'nd';
}
else if (endsWith(ordinalDate, '3')) {
ordinalDate += 'rd';
}
else {
ordinalDate += 'th';
}
format = format.replace('ddd', ordinalDate);
}
else if (contains(format, 'dd')) {
format = format.replace('dd', toTwoDigits(currentDate));
}
//day
if (contains(format, 'DDDD')) {
format = format.replace('DDDD', DayNames[currentDay][1]);
}
else if (contains(format, 'DDD')) {
format = format.replace('DDD', DayNames[currentDay][0]);
}
//month
if (contains(format, 'MMMM')) {
format = format.replace('MMMM', MonthName[currentMonth][1]);
}
else if (contains(format, 'MMM')) {
format = format.replace('MMM', MonthName[currentMonth][0]);
}
else if (contains(format, 'MM')) {
format = format.replace('MM', toTwoDigits(currentMonth + 1));
}
//year
if (contains(format, 'yyyy')) {
format = format.replace('yyyy', currentYear);
}
else if (contains(format, 'yy')) {
currentYear = currentYear.toString();
format = format.replace('yy', currentYear.substring(currentYear.length - 2, currentYear.length));
}
//hour
if (contains(format, 'hhh')) {
format = format.replace('hhh', toTwoDigits(currentHour));
}
else if (contains(format, 'hh')) {
currentHour = currentHour <= 12 ? currentHour : currentHour - 12;
format = format.replace('hh', toTwoDigits(currentHour));
}
//minute
if (contains(format, 'mm')) {
format = format.replace('mm', toTwoDigits(date.getMinutes()));
}
//seconds
if (contains(format, 'ss')) {
format = format.replace('ss', toTwoDigits(date.getSeconds()));
}
return format;
}
/**
* Array date helpers
*/
var DayNames = [
[ 'Sun', 'Sunday' ],
[ 'Mon', 'Monday' ],
[ 'Tue', 'Tuesday' ],
[ 'Wed', 'Wednesday' ],
[ 'Thu', 'Thursday' ],
[ 'Fri', 'Friday' ],
[ 'Sat', 'Saturday' ]
];
var MonthName = [
[ 'Jan', 'January'],
[ 'Feb', 'February'],
[ 'Mar', 'March'],
[ 'Apr', 'April'],
[ 'May', 'May'],
[ 'Jul', 'June'],
[ 'Jul', 'July'],
[ 'Aug', 'August'],
[ 'Sep', 'September'],
[ 'Oct', 'October'],
[ 'Nov', 'November'],
[ 'Dec', 'December']
];
/**
* Contains helper function
*/
function contains(str, value) {
return str.indexOf(value) >= 0;
}
/**
* endsWith helper function
*/
function endsWith(str, value) {
return str.indexOf(value, str.length - value.length) >= 0;
}
/**
* toTwoDigits helper function
*/
function toTwoDigits(value) {
return value < 10 ? '0' + value : value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment