1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| function formatDate (date, format) { var convert, res = ''; convert = { 'y': function (str) { return (date.getFullYear() + '').slice(-(str.length < 4 ? 2 : str.length)); }, 'M': function (str) { var length, m, month, monthName; length = str.length < 6 ? str.length : 6; month = date.getMonth() + 1; monthName = [ ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'], ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'] ]; if (length === 1) { m = month; } else if (length === 2) { m = ('0' + month).slice(-2); } else { m = monthName[length - 3][month - 1]; } return m; }, 'd': function (str) { var d = date.getDay(); return str.length === 1 ? d : ('0' + d).slice(-2); }, 'H': function (str) { var h = date.getHours(); res = ''; return str.length === 1 ? h : ('0' + h).slice(-2); }, 'h': function (str) { var h; h = date.getHours(); res = ' AM'; if (h > 12) { h = h - 12; res = ' PM'; } return str.length === 1 ? h : ('0' + h).slice(-2); }, 'm': function (str) { var m = date.getMinutes(); return str.length === 1 ? m : ('0' + m).slice(-2); }, 's': function (str) { var s = date.getSeconds(); return str.length === 1 ? s : ('0' + s).slice(-2); }, 'S': function (str) { var s = date.getMilliseconds(); return str.length === 1 ? s : ('00' + s).slice(-3); }, 'D': function (str) { var d, name, length; name = [ ['Sun', 'Mon','Tue','Wed','Thu','Fri','Sat'], ['Sunday', 'Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'], ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'], ['周日','周一','周二','周三','周四','周五','周六'] ]; d = date.getDay(); length = str.length < 4 ? str.length : 4; return name[length - 1][d]; } } return format.replace(/y+|M+|d+|H+|h+|m+|s+|S+|D+/g, function (str) { return convert[str.charAt(0)](str); }) + res; }
|