Skip to content

Instantly share code, notes, and snippets.

@abbotto
Last active August 29, 2015 14:25
Show Gist options
  • Select an option

  • Save abbotto/49c2629c8cd741efe182 to your computer and use it in GitHub Desktop.

Select an option

Save abbotto/49c2629c8cd741efe182 to your computer and use it in GitHub Desktop.
Pass in an ISO-8601 date string and get back an object containing the date details
// Example Usage: http://codepen.io/o0110o/pen/gpdKYp
/**
* Pad
* @param {String}
* @param {String}
* @param {Integer}
* @return {String}
*/
// Borrowed from: https://github.com/o0110o/meta-js/blob/master/src/util.js
_pad = function (str, pad, width) {
// Fallbacks
pad = pad || '0';
str = str + '';
// Add the padding
while (str.length < width) {
str = pad + str;
}
return str;
}
/**
* DateMap
* @param {String}
* @param {String}
* @return {Object}
*/
var _dateMap = function (date, mode) {
// Months and days
months = ['January', 'Febuary', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
shortMonths = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
shortWeekdays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
checkDate = new Date(date);
modeMatch = /standard|std|military|mil/i.test(date);
// Get the current time related data returned in an object
if ((typeof date !== 'undefined' && date.substring) || !date) {
// Get the current time related data returned in an object
if (!date || modeMatch) {
// Under the right conditions, set mode to the value of date
if (!mode && modeMatch) {
mode = date;
};
date = new Date(Date.now());
} else {
date = new Date(date); // Appending UTC converts the str to GMT... Go figure.
}
// Fallback to military mode by default
mode = mode || 'military';
// Used in date object
getDay = date.getDay();
getMS = (new Date).getMilliseconds();
getDate = date.getDate();
getMonth = date.getMonth();
getFullYear = date.getFullYear();
getHours = date.getHours();
getMinutes = date.getMinutes();
getSeconds = date.getSeconds();
timezone = -(new Date().getTimezoneOffset() / 60);
// Date object
dateObj = {
'D': getDate,
'DD': _pad(getDate, '0', 2),
'M': getMonth + 1,
'MM': _pad(getMonth + 1, '0', 2),
'MMM': shortMonths[getMonth],
'MMMM': months[getMonth],
'YYYY': getFullYear,
'd': (getDay + 1),
'dd': _pad((getDay + 1), '0', 2),
'ddd': shortWeekdays[getDay],
'dddd': weekdays[getDay],
'h': getHours,
'hh': _pad(getHours, '0', 2),
'm': getMinutes,
'mm': _pad(getMinutes, '0', 2),
's': getSeconds,
'ss': _pad(getSeconds, '0', 2),
'ms': getMS,
'ZZ': timezone,
'PP': 'PM',
'pp': 'pm'
}
// AM/PM
if (dateObj.h > 12) {
if (dateObj.h === 24) {
dateObj.PP = 'AM';
dateObj.pp = 'am';
}
} else if (dateObj.h < 12) {
dateObj.PP = 'AM';
dateObj.pp = 'am';
};
// Set milliseconds to null if not getting current time via Date.now()
if ((checkDate !== "Invalid Date" && !isNaN(checkDate))) {
dateObj.ms = null;
};
// Use 12-hour mode
if (mode === 'standard') {
dateObj.hh = dateObj.h;
diff = dateObj.h - 12;
if (dateObj.h > 12) {
dateObj.h = dateObj.hh = diff;
} else if (dateObj.h < 10) {
dateObj.hh = _pad(dateObj.h, '0', 2);
};
}
return dateObj;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment