javascript格式化日期时间方法汇总_javascript技巧
程序员文章站
2022-04-11 17:38:30
...
示例一:
TT
示例二:
示例三:
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 }
示例四:
Date.prototype.toString=function(format,loc){ var time={}; time.Year=this.getFullYear(); time.TYear=(""+time.Year).substr(2); time.Month=this.getMonth()+1; time.TMonth=time.Month0){ if(loc!=undefined && loc =="en"){ MMMArr=MMMArrEn.slice(0); WeekArr=WeekArrEn.slice(0); } format=format .replace(/yyyy/ig,time.Year) .replace(/yyy/ig,time.Year) .replace(/yy/ig,time.TYear) .replace(/y/ig,time.TYear) .replace(/MMM/g,MMMArr[time.Month-1]) .replace(/MM/g,time.TMonth) .replace(/M/g,time.Month) .replace(/dd/ig,time.TDay) .replace(/d/ig,time.Day) .replace(/HH/g,time.THour) .replace(/H/g,time.Hour) .replace(/hh/g,time.Thour) .replace(/h/g,time.hour) .replace(/mm/g,time.TMinute) .replace(/m/g,time.Minute) .replace(/ss/ig,time.TSecond) .replace(/s/ig,time.Second) .replace(/fff/ig,time.Millisecond) .replace(/ff/ig,oNumber.toFixed(2)*100) .replace(/f/ig,oNumber.toFixed(1)*10) .replace(/EEE/g,WeekArr[time.Week]); } else{ format=time.Year+"-"+time.Month+"-"+time.Day+" "+time.Hour+":"+time.Minute+":"+time.Second; } return format; } var d=new Date(); console.log(d.toString()); //2014-7-27 9:26:52 console.log(d.toString("")); //2014-7-27 9:26:52 console.log(d.toString("yyyy-MM-dd HH:mm:ss")); //2014-07-27 09:26:52 console.log(d.toString("yyyy年MM月dd日 HH:mm:ss")); //2014年07月27日 09:26:52 console.log(d.toString("yyyy-MM-dd HH:mm:ss fff")); //2014-07-27 09:26:52 237 console.log(d.toString("yyyy年 MMM dd EEE")); //2014年 七月 27 星期五 console.log(d.toString("yyyy MMM dd EEE","en")); //2014 Jul 27 Fri
示例五:时间个性化输出功能
/* 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; }
以上所述就是本文的全部内容了,希望大家能够喜欢。
上一篇: 如何使用Java读文件和写文件代码讲解
推荐阅读
-
javascript显示动态时间的方法汇总
-
javascript 格式化时间日期函数代码脚本之家修正版
-
javascript实现的距离现在多长时间后的一个格式化的日期
-
JavaScript 中Date对象的格式化代码方法汇总
-
JavaScript 格式化返回当前日期和时间
-
javascript实现时间日期的格式化的方法汇总
-
防止按钮在短时间内被多次点击的方法_javascript技巧
-
扩展JS Date对象时间格式化功能的小例子_javascript技巧
-
JavaScript根据日期和时间据 1970/1/1 午夜(GMT 时间)之间的毫秒数的方法parse()
-
JavaScript显示当然日期和时间即年月日星期和时间_javascript技巧