您好,欢迎访问一九零五行业门户网

javascript日期格式化方法汇总_javascript技巧

方法一:
// 对date的扩展,将 date 转化为指定格式的string// 月(m)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位符,毫秒(s)只能用 1 个占位符(是 1-3 位的数字) // 例子: // (new date()).format(yyyy-mm-dd hh:mm:ss.s) ==> 2006-07-02 08:09:04.423 // (new date()).format(yyyy-m-d h:m:s.s) ==> 2006-7-2 8:9:4.18 date.prototype.format = function (fmt) { //author: meizz var o = { m+: this.getmonth() + 1, //月份 d+: this.getdate(), //日 h+: this.gethours(), //小时 m+: this.getminutes(), //分 s+: this.getseconds(), //秒 q+: math.floor((this.getmonth() + 3) / 3), //季度 s: this.getmilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(regexp.$1, (this.getfullyear() + ).substr(4 - regexp.$1.length)); for (var k in o) if (new regexp(( + k + )).test(fmt)) fmt = fmt.replace(regexp.$1, (regexp.$1.length == 1) ? (o[k]) : ((00 + o[k]).substr(( + o[k]).length))); return fmt;}
调用方式:
var time1 = new date().format(yyyy-mm-dd);
var time2 = new date().format(yyyy-mm-dd hh:mm:ss);
方法二:

方法三:
date.prototype.format = function (mask) { var d = this; var zeroize = function (value, length) { if (!length) length = 2; value = string(value); for (var i = 0, zeros = ''; i 99) m = math.round(m / 10); return zeroize(m); case 'tt': return d.gethours() 9 ? (this.getmonth() + 1).tostring() : '0' + (this.getmonth() + 1));str = str.replace(/m/g, (this.getmonth() + 1));str = str.replace(/w|w/g, week[this.getday()]);str = str.replace(/dd|dd/, this.getdate() > 9 ? this.getdate().tostring() : '0' + this.getdate());str = str.replace(/d|d/g, this.getdate());str = str.replace(/hh|hh/, this.gethours() > 9 ? this.gethours().tostring() : '0' + this.gethours());str = str.replace(/h|h/g, this.gethours());str = str.replace(/mm/, this.getminutes() > 9 ? this.getminutes().tostring() : '0' + this.getminutes());str = str.replace(/m/g, this.getminutes());str = str.replace(/ss|ss/, this.getseconds() > 9 ? this.getseconds().tostring() : '0' + this.getseconds());str = str.replace(/s|s/g, this.getseconds());return str}

方法六:
date.prototype.format = function(formatstr) {var str = formatstr;var week = ['日', '一', '二', '三', '四', '五', '六'];str = str.replace(/yyyy|yyyy/, this.getfullyear());str = str.replace(/yy|yy/, (this.getyear() % 100) > 9 ? (this.getyear() % 100).tostring() : '0' + (this.getyear() % 100));str = str.replace(/mm/, (this.getmonth() + 1) > 9 ? (this.getmonth() + 1).tostring() : '0' + (this.getmonth() + 1));str = str.replace(/m/g, (this.getmonth() + 1));str = str.replace(/w|w/g, week[this.getday()]);str = str.replace(/dd|dd/, this.getdate() > 9 ? this.getdate().tostring() : '0' + this.getdate());str = str.replace(/d|d/g, this.getdate());str = str.replace(/hh|hh/, this.gethours() > 9 ? this.gethours().tostring() : '0' + this.gethours());str = str.replace(/h|h/g, this.gethours());str = str.replace(/mm/, this.getminutes() > 9 ? this.getminutes().tostring() : '0' + this.getminutes());str = str.replace(/m/g, this.getminutes());str = str.replace(/ss|ss/, this.getseconds() > 9 ? this.getseconds().tostring() : '0' + this.getseconds());str = str.replace(/s|s/g, this.getseconds());return str}

方法七:
/*1、= 1min && = 60min && = 1day && = 1year, 显示具体日期“xxxx年xx月xx日 xx:xx”*/function timeformat(time){var date = new date(time),curdate = new date(),year = date.getfullyear(),month = date.getmonth() + 10,day = date.getdate(),hour = date.gethours(),minute = date.getminutes(),curyear = curdate.getfullyear(),curhour = curdate.gethours(),timestr; if(year curhour){timestr = month +'月'+ day +'日 '+ hour +':'+ minute;}else if(pasth >= 1){timestr = '今天 ' + hour +':'+ minute +'分';}else{var pastm = curdate.getminutes() - minute;if(pastm > 1){timestr = pastm +'分钟前';}else{timestr = '刚刚';}}}return timestr;}
是不是第七种更加的个性化一些呢,个人推荐这种
其它类似信息

推荐信息