Skip to content

Instantly share code, notes, and snippets.

@rafaelfaria
Created January 22, 2017 22:44
Show Gist options
  • Save rafaelfaria/09fd2ebccbccaffb407a6f1602062308 to your computer and use it in GitHub Desktop.
Save rafaelfaria/09fd2ebccbccaffb407a6f1602062308 to your computer and use it in GitHub Desktop.
dateFormat
var startTime = new Date();
function dateFormat(timestamp, past, in24Hours) {
timestamp = isNaN(Number(timestamp)) ? timestamp : Number(timestamp);
let formatedString = '';
let listMonths = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
let nowDate = new Date(),
timestampDate = new Date(timestamp),
elapsedMin = (nowDate - timestampDate) / 1000 / 60,
elapsedHours;
elapsedHours = Math.floor(elapsedMin / 60)
elapsedMin = Math.floor(elapsedMin);
let ordinal = function(number) {
let b = number % 10,
output = (~~(number % 100 / 10) === 1) ? 'th' :
(b === 1) ? 'st' :
(b === 2) ? 'nd' :
(b === 3) ? 'rd' : 'th';
return output;
}
let oneDigitNumber = function(num) {
num = '' + num;
return (num.indexOf('0') == 0 && num != '0') ? num.substring(1) : num;
}
let twoDigitNumber = function(num) {
num = Number(num);
return isNaN(num) ? '00' : (((num < 10) ? '0' : '') + num);
}
let hours = timestampDate.getHours();
let dateParts = {
month: twoDigitNumber(timestampDate.getMonth() + 1),
date: twoDigitNumber(timestampDate.getDate()),
year: twoDigitNumber(timestampDate.getFullYear()),
day: timestampDate.getDay(),
hour24: hours,
hour12: hours,
minutes: twoDigitNumber(timestampDate.getMinutes()),
ampm: 'AM'
}
// Format the date
dateParts.ordinal = ordinal(dateParts.date);
dateParts.date_1 = oneDigitNumber(dateParts.date);
// Format minutes
dateParts.minutes_1 = oneDigitNumber(dateParts.minutes)
// Format hours
if (dateParts.hour24 > 11) {
dateParts.ampm = 'PM';
}
dateParts.hour24 = twoDigitNumber(dateParts.hour24);
dateParts.hour24_1 = oneDigitNumber(dateParts.hour24);
if (dateParts.hour12 == 0) {
dateParts.hour12 = 12;
}
if (dateParts.hour12 > 12) {
dateParts.hour12 = dateParts.hour12 - 12;
}
dateParts.hour12 = twoDigitNumber(dateParts.hour12);
dateParts.hour12_1 = oneDigitNumber(dateParts.hour12);
// Format the elapsed time (X m ago or X h ago)
if (elapsedHours < 1) {
dateParts.elapsed = oneDigitNumber(elapsedMin) + 'm';
} else {
dateParts.elapsed = oneDigitNumber(elapsedHours) + 'h';
}
let patternReplace = function(formattedPart) {
let weekDay = weekdays[dateParts.day],
month = listMonths[Number(dateParts.month) - 1],
monthShort = month.substring(0, 3),
yearShort = dateParts.year.substring(2),
weekDayMid = weekDay.substring(0, 3),
weekDayShort = weekDay.substring(0, 2),
amPmLowercase = dateParts.ampm.toLowerCase();
formattedPart = formattedPart
// AM/PM - am/pm
.replace(/AA/g, dateParts.ampm)
.replace(/aa/g, amPmLowercase)
// Hours (24)
.replace(/HH/g, dateParts.hour24)
.replace(/H/g, dateParts.hour24_1)
// Hours (12)
.replace(/hh/g, dateParts.hour12)
.replace(/h/g, dateParts.hour12_1)
// Minutes
.replace(/II/g, dateParts.minutes)
.replace(/I/g, dateParts.minutes_1)
// Date
.replace(/DD/g, dateParts.date)
.replace(/D/g, dateParts.date_1)
// Ordinal for date
.replace(/O/g, dateParts.ordinal)
// Year
.replace(/YYYY/g, dateParts.year)
.replace(/YY/g, yearShort)
// Elapsed time (Hours and Minutes)
.replace(/GG/g, dateParts.elapsed)
// Month
.replace(/MM/g, month)
.replace(/M/g, monthShort)
// Week day
.replace(/EEEE/g, weekDay)
.replace(/EEE/g, weekDayMid)
.replace(/EE/g, weekDayShort);
return formattedPart;
}
let formatParts = function(pattern) {
let formatPatternParts = pattern.replace('\\\'', '%Q%')
.replace('\'\'', '%Q%')
.split('\'');
for (let i = 0; i < formatPatternParts.length; i++) {
if (i % 2 == 0) {
formatPatternParts[i] = patternReplace(formatPatternParts[i]);
}
}
return formatPatternParts.join('').replace('%Q%', '\'');
}
if (elapsedHours < 24 && in24Hours) {
formatedString = formatParts(in24Hours);
} else {
formatedString = formatParts(past);
}
return formatedString;
}
let t = 'Fri Jan 20 2017 22:33:28 GMT+1100 (AEDT)';
//let t = '1484897633537';
for (var i=0;i<1000;i++) {
dateFormat(t, 'DD M ', 'GG \'ago\'');
dateFormat(t, 'EEE, DD M');
dateFormat(t, 'DDO M YY HH:II');
dateFormat(t, 'hh:Iaa');
dateFormat(t, 'GG \'ago\'');
}
/*
AA - AM/PM
aa - am/pm
HH - hours (24) - 2 digits
H - hours (24) - 1 digit
hh - hours (12) - 2 digits
h - hours (12) - 1 digit
II - minutes - 2 digits
I - minutes - 1 digit
DD - date - 2 digits
D - date - 1 digit
O - date - ordinal
MM - month - Long name
M - month - Short name
EEEE - week - long name
EEE - week - 3 letters name
EE - week - 2 letters name
YYYY - year - Long
YY - year - Short
GG - elapse min/hours
*/
// later record end time
var endTime = new Date();
// time difference in ms
var timeDiff = endTime - startTime;
timeDiff /= 1000;
console.log(timeDiff);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment