- php: http://php.net/manual/en/function.date.php
- .net: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
- bash: http://ss64.com/bash/date.html
- emacs: format-time-string
Created
April 18, 2012 05:08
-
-
Save yangg/2411213 to your computer and use it in GitHub Desktop.
Format Multiple Dates in Javascript
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
/** | |
*<code> | |
var date = new Date(); | |
var date2 = new Date(); | |
date2.setMonth(date2.getMonth() + 2); | |
console.log(date, date2); | |
console.log('getDays:\t\t',datehelper.getDays(date, date2)); | |
for(var n in datehelper) { | |
if(/get\w+Of/.test(n)) { | |
console.log(n + ":\t", datehelper[n](date)); | |
} | |
} | |
</code> | |
*/ | |
var datehelper = { | |
getDays: function(date1, date2, offset) { | |
return (date2.getTime() - date1.getTime()) / (24*3600*1000) + 1 + (offset | 0); | |
}, | |
createDateFunction: function(date, dayofmonth, month) { | |
var newDate = new Date(date); | |
typeof month != 'undefined' && newDate.setMonth(month); | |
newDate.setDate(dayofmonth); | |
return newDate; | |
}, | |
getFirstDayOfWeek: function(date) { | |
return this.createDateFunction(date, date.getDate() - date.getDay()); | |
}, | |
getLastDayOfWeek: function(date) { | |
return this.createDateFunction(date, date.getDate() + 6 - date.getDay()); | |
}, | |
getFirstDayOfMonth: function(date) { | |
return this.createDateFunction(date, 1); | |
}, | |
getLastDayOfMonth: function(date) { | |
return this.createDateFunction(date, 0, date.getMonth() + 1); | |
}, | |
getFirstDayOfQuarter: function(date) { | |
return this.createDateFunction(date, 1, date.getMonth() - (date.getMonth() % 3)); | |
}, | |
getLastDayOfQuarter: function(date) { | |
return this.createDateFunction(date, 0, date.getMonth() + (3 - (date.getMonth() % 3))); | |
} | |
}; |
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
/** | |
* Format mutiple dates | |
*/ | |
var utils = utils || {}; | |
utils.formatDate = function(mask, date1, date2) { | |
if(Object.prototype.toString.call(date1) != '[object Date]') { | |
date1 = new Date(date1); | |
date2 = new Date(date2); | |
} | |
var formatters = this.dateFormatters, date = date1; | |
return mask.replace(/([a-zA-Z])\1{0,3}|\|/g, function(flag) { | |
if(flag == '|') { date = date2; return ''; } | |
return flag in formatters ? formatters[flag](date) : flag; | |
}); | |
}; | |
utils.dateFormatters = function() { | |
function pad(n) { return ('0' + n).substr(-2); } | |
return { | |
yy: function(d) { return pad(d.getFullYear()); }, | |
yyyy: function(d) { return d.getFullYear(); }, | |
M: function(d) { return d.getMonth() + 1; }, | |
MM: function(d) { return pad(d.getMonth() + 1); }, | |
d: function(d) { return d.getDate(); }, | |
dd: function(d) { return pad(d.getDate()); }, | |
HH: function(d) { return pad(d.getHours()); }, | |
mm: function(d) { return pad(d.getMinutes()); }, | |
ss: function(d) { return pad(d.getSeconds()); } | |
} | |
}(); | |
utils.dateFormatters.i18n = { lang: typeof navigator != 'undefined' && navigator.language }; | |
utils.dateFormatters.i18n.monthNames = { | |
"zh-CN" : ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"], | |
"en-US": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] | |
}; | |
utils.dateFormatters.MMM = function(d) { | |
return this.i18n.monthNames[this.i18n.lang || 'en-US'][d.getMonth()]; | |
}; | |
var d1 = new Date(), d2 = new Date(d1); | |
d2.setDate(d2.getDate() + 6); | |
console.log(utils.formatDate('yyyy/M/d - |M/d', d1, d2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment