Skip to content

Instantly share code, notes, and snippets.

@theladyjaye
Created August 19, 2013 22:09
Show Gist options
  • Select an option

  • Save theladyjaye/6274766 to your computer and use it in GitHub Desktop.

Select an option

Save theladyjaye/6274766 to your computer and use it in GitHub Desktop.
datetime module for ISO8601 UTC Dates
define([
], function(){
var months = ['January', 'February', 'March', 'April', 'May',
'June', 'July', 'August', 'September', 'October',
'November', 'December'];
// All values assumed to be ISO 8601 and in UTC
// 2013-08-29T14:29Z
// 2013-08-29T14:29.000Z
// 2013-08-29T14:29+0000
// 2013-08-29T14:29:30.123+0000
var isoRegex = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(?:\:(\d{2})(?:\.(\d+))?)?(?:Z|\+0000)?/;
function UTCStringToMilliseconds(value){
// 2013-08-29T15:02:00Z
// milliseconds and seconds are optional
// if omitted, 0's are assumed.
var c = isoRegex.exec(value);
var year, month, day, hours, minutes, seconds, millisecond = 0;
year = c[1];
month = (c[2] - 1);
day = c[3];
hour = c[4];
minute = c[5];
second = c[6] || 0;
millisecond = c[7] || 0;
return Date.UTC(year, month, day, hour, minute, second, millisecond);
}
function UTCStringToSeconds(value){
var utcMilliSeconds = UTCStringToMilliseconds(value);
return parseInt((utcMilliSeconds / 1000), 10);
}
function UTCStringToLocalDate(value){
var utcSeconds = UTCStringToSeconds(value);
var date = new Date(0);
date.setUTCSeconds(utcSeconds);
return date;
}
function monthDayYearStringForDate(date){
var dayValue = date.getDate();
var suffix = daySuffix(day);
var day = dayValue.toString() + suffix;
return months[date.getMonth()] + ' ' + day + ' ' + date.getFullYear();
}
function monthDayYearStringForUTCString(value){
var date = UTCStringToLocalDate(value);
return monthDayYearStringForDate(date);
}
function daySuffix(day){
switch (day)
{
case 1:
case 21:
case 31:
return 'st';
case 2:
case 22:
return 'nd';
case 3:
case 23:
return 'rd';
default:
return 'th';
}
}
function relativeDate(value){
// compatibility
var now = +new Date();
var date = UTCStringToMilliseconds(value);
var totalSeconds = parseInt((now - date) / 1000, 10);
var time = '';
if (totalSeconds < 10) {
return 'just now';
} else if(totalSeconds < 60) {
time = totalSeconds;
return time + ' seconds ago';
} else if(totalSeconds < 120) {
return 'about a minute ago';
} else if(totalSeconds < 3600) {
time = parseInt(totalSeconds / 60, 10);
return time + ' minutes ago';
} else if(totalSeconds < 7200) {
return 'about an hour ago';
} else if(totalSeconds < 86400) {
time = parseInt(totalSeconds / 3600, 10);
return time + ' hours ago';
} else if(totalSeconds < 172800) {
return '1 day ago';
} else if(totalSeconds < 604800) {
time = parseInt(totalSeconds / 86400, 10);
return time + ' days ago';
} else if(totalSeconds < 1209600) {
return '1 week ago';
} else {
time = parseInt(totalSeconds / 604800, 10);
return time + ' weeks ago';
}
}
return {
relativeDate: relativeDate,
UTCStringToMilliseconds: UTCStringToMilliseconds,
UTCStringToSeconds: UTCStringToSeconds,
UTCStringToLocalDate: UTCStringToLocalDate,
monthDayYearStringForDate: monthDayYearStringForDate,
monthDayYearStringForUTCString:monthDayYearStringForUTCString
};
});
@theladyjaye
Copy link
Copy Markdown
Author

Parses ISO 8601 UTC Dates to JS Local Time Dates. Includes some relative date formatting as some simple date formatting. Should handle the following formats:

2013-08-29T14:29Z
2013-08-29T14:29:00Z
2013-08-29T14:29:00.000Z
2013-08-29T14:29+0000
2013-08-29T14:29:00+0000
2013-08-29T14:29:00.000+0000

If seconds or milliseconds are not present, 0 is assumed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment