Last active
August 29, 2015 13:57
-
-
Save jmervine/9769416 to your computer and use it in GitHub Desktop.
See example: http://jsfiddle.net/jmervine/dqA9r/5/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Author: Joshua P. Mervine <[email protected]> [github.com/jmervine] | |
See: http://jsfiddle.net/jmervine/dqA9r/5/ | |
Use: | |
<script src="http://cdn.mervine.net/FormattedDate.js" /> | |
<script src="http://cdn.mervine.net/FormattedDate.min.js" /> | |
var d = new FormattedDate("Tue Mar 25 2014 13:53:34 GMT-0700 (PDT)"); | |
d.stringify(); | |
=> 1 hour ago | |
var d = new FormattedDate("Tue Mar 25 2014 09:23:34 GMT-0700 (PDT)"); | |
d.stringify(); | |
=> 5 hours ago | |
var d = new FormattedDate("Mon Mar 24 2014 02:23:34 GMT-0700 (PDT)"); | |
d.stringify(); | |
=> 03/25/2014 02:23:34 | |
var d = new FormattedDate(); | |
d.stringify(); | |
=> 0 minutes ago | |
d.stringify('day'); | |
=> 26 | |
d.stringify('month'); | |
=> 03 | |
d.stringify('year'); | |
=> 2014 | |
d.stringify('hour'); | |
=> 14 | |
d.stringify('minute'); | |
=> 23 | |
d.stringify('seconds'); | |
=> 34 | |
d.stringify('ms'); | |
=> 845 | |
d.stringify('day_name'); | |
=> Monday | |
d.stringify('something bad'); | |
=> undefined | |
d.format('MN - DN - HH:mm:ss MSms'); | |
=> March - Monday - 14:23:34 845ms | |
*/ | |
function FormattedDate() { | |
var args = Array.prototype.concat.apply([null], arguments); | |
var days = [ "Sunday", "Monday", "Tuesday", "Wednesday", | |
"Thursday", "Friday", "Saturday" ]; | |
var months = ["January", "Feburary", "March", "April", "May", "June", "July", | |
"August", "September", "October", "November", "December"]; | |
this.date = this.now = new (Function.prototype.bind.apply(Date, args)); | |
this.day = this.days = this.date.getDate()+1; | |
this.dayName = this.dayname = this.day_name = days[this.date.getDay()-1]; | |
this.dayShort = this.dayshort = this.day_short = this.dayName.slice(0,3); | |
this.month = this.months = this.mo = this.mos = this.date.getMonth()+1; | |
this.monthName = this.monthname = this.month_name = months[this.month-1]; | |
this.monthShort = this.monthshort = this.month_short = this.monthName.slice(0,3); | |
this.year = this.years = this.yr = this.yrs = this.date.getFullYear(); | |
this.hour = this.hours = this.hr = this.hrs = this.date.getHours(); | |
this.minute = this.minutes = this.min = this.date.getMinutes(); | |
this.second = this.seconds = this.sec = this.secs = this.date.getSeconds(); | |
this.millisecond = this.milliseconds = this.ms = this.date.getMilliseconds(); | |
this.time = this.epoch = this.date.getTime(); | |
this.today = !!(new Date(new Date()-(24*60*60*1000)) < this.date); | |
} | |
FormattedDate.prototype = { | |
stringify: function stringify(entity, format) { | |
format = format || "MM/DD/YY HH:mm:ss"; | |
if (entity) { | |
if (typeof this[entity] === 'string') return this[entity]; | |
if (entity !== 'date') { | |
var num = this[entity]; | |
if (num < 10) return "0"+num; | |
return ""+num; | |
} | |
} | |
if (this.today) { | |
var now = new FormattedDate(); | |
var hoursAgo = (now.hour - this.hour); | |
var s = "s"; | |
if (hoursAgo === 0) { | |
var minutesAgo = (now.minute - this.minute); | |
if (minutesAgo === 1) s = ""; | |
return minutesAgo + " minute" + s +" ago"; | |
} | |
if (hoursAgo === 1) s = ""; | |
return hoursAgo + " hour" + s + " ago"; | |
} | |
return this.format(format); | |
}, | |
format: function(str) { | |
var mapping = { 'YY': 'yr', 'MM': 'mo', 'DD': 'day', 'dd': 'day', | |
'HH': 'hr', 'hh': 'hr', 'mm': 'min', 'SS': 'sec', 'ss': 'sec', | |
'DN': 'dayName', 'MN': 'monthName', 'MS': 'ms' }; | |
var that = this; | |
Object.keys(mapping).forEach(function(map) { | |
str = str.replace(map, that.stringify(mapping[map]), "g"); | |
}); | |
return str; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* author: github.com/jmervine, compressed via http://jscompress.com/ */ | |
function FormattedDate(){var e=Array.prototype.concat.apply([null],arguments);var t=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];var n=["January","Feburary","March","April","May","June","July","August","September","October","November","December"];this.date=this.now=new(Function.prototype.bind.apply(Date,e));this.day=this.days=this.date.getDate()+1;this.dayName=this.dayname=this.day_name=t[this.date.getDay()-1];this.dayShort=this.dayshort=this.day_short=this.dayName.slice(0,3);this.month=this.months=this.mo=this.mos=this.date.getMonth()+1;this.monthName=this.monthname=this.month_name=n[this.month-1];this.monthShort=this.monthshort=this.month_short=this.monthName.slice(0,3);this.year=this.years=this.yr=this.yrs=this.date.getFullYear();this.hour=this.hours=this.hr=this.hrs=this.date.getHours();this.minute=this.minutes=this.min=this.date.getMinutes();this.second=this.seconds=this.sec=this.secs=this.date.getSeconds();this.millisecond=this.milliseconds=this.ms=this.date.getMilliseconds();this.time=this.epoch=this.date.getTime();this.today=!!(new Date(new Date-24*60*60*1e3)<this.date)}FormattedDate.prototype={stringify:function(t,n){n=n||"MM/DD/YY HH:mm:ss";if(t){if(typeof this[t]==="string")return this[t];if(t!=="date"){var r=this[t];if(r<10)return"0"+r;return""+r}}if(this.today){var i=new FormattedDate;var s=i.hour-this.hour;var o="s";if(s===0){var u=i.minute-this.minute;if(u===1)o="";return u+" minute"+o+" ago"}if(s===1)o="";return s+" hour"+o+" ago"}return this.format(n)},format:function(e){var t={YY:"yr",MM:"mo",DD:"day",dd:"day",HH:"hr",hh:"hr",mm:"min",SS:"sec",ss:"sec",DN:"dayName",MN:"monthName",MS:"ms"};var n=this;Object.keys(t).forEach(function(r){e=e.replace(r,n.stringify(t[r]),"g")});return e}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment