, JavaScript默认的时间格式我们一般情况下不会用,所以需要进行格式化,下面说说我总结的JavaScript时间格式化方法。
很多时候,我们可以利用JavaScript中日期对象的内置方法来格式化,如:
var d=新的日期(); console.log (d);//输出:星期一2013年11月04 21:50:33格林尼治标准时间+ 0800(中国标准时间) console.log (d.toDateString ());//日期字符串,输出:星期一2013年11月04 console.log (d.toGMTString ());//格林威治时间,输出:星期一,2013年11月04 14:03:05格林尼治时间 console.log (d.toISOString ());//国际标准组织(ISO)格式、输出:2013 - 11 - 04 - t14:03:05.420z console.log (d.toJSON ());//输出:2013 - 11 - 04 - t14:03:05.420z console.log (d.toLocaleDateString ());//转换为本地日期格式,视环境而定,输出:2013年11月4日 console.log (d.toLocaleString ());//转换为本地日期和时间格式,视环境而定,输出:2013年11月4日下午10:03:05 console.log (d.toLocaleTimeString ());//转换为本地时间格式,视环境而定,输出:下午10:03:05 console.log (d.toString ());//转换为字符串,输出:星期一2013年11月04 22:03:05格林尼治标准时间+ 0800(中国标准时间) console.log (d.toTimeString ());//转换为时间字符串,输出:22:03:05格林尼治标准时间+ 0800(中国标准时间) console.log (d.toUTCString ());//转换为世界时间,输出:星期一,2013年11月04 14:03:05格林尼治时间
如果上面的方法不能满足我们的要求,也可以自定义函数来格式化时间,如:
//对日期的扩展,将日期转化为指定格式的字符串//月(M)、日(d),小时(h),分(M),秒(s),季度(q)可以用1 - 2个占位符,//年(y)可以用1 - 4个占位符,毫秒(S)只能用1个占位符(是1 - 3位的数字)//例子://(新日期())。格式(“yyyy-MM-dd hh: mm: ss.S”)==比;2006-07-02 08:09:04.423//(新日期())。格式(“yyyy-M-d h: m:轮”)==比;2006-7-2 8:9:4.18 Date.prototype。格式=函数(fmt){//作者: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()//毫秒 }; 如果(/(y +)/test (fmt)) fmt=fmt.replace (RegExp。1美元,(this.getFullYear () + " ")。substr(长度)4 - RegExp。1.美元); 在o (var k) 如果新的正则表达式(“(“+ k +”)”)。test (fmt)) fmt=fmt.replace (RegExp。1美元,(RegExp。1美元。长度==1)& # 63;(o [k]):((“00”+ o [k])。substr ((" + o [k]) . length))); 返回fmt; }
调用:
var time1=new () .Format日期(“yyyy-MM-dd”); var time2=new日期()。格式(“yyyy-MM-dd HH: mm: ss”);>之前
& lt;脚本语言=" javascript " type=" text/javascript祝辞 & lt; !——/* * *对日期的扩展,将日期转化为指定格式的字符串*月(M)、日(d), 12个小时(h), 24小时(h),分(M),秒(s),周(E)、季度(q) 可以用1 - 2个占位符*年(y)可以用1 - 4个占位符,毫秒(S)只能用1个占位符(是1 - 3位的数字)*如:*(新 日期())。模式(“yyyy-MM-dd hh: mm: ss.S”)==比;2006-07-02 08:09:04.423 *())(新日期。模式(yyyy-MM-dd E HH: mm: ss)==比;2009-03-10二20:09:04 *())(新日期。模式(yyyy-MM-dd EE hh: mm: ss)==比;2009-03-10周二08:09:04 *())(新日期。模式(yyyy-MM-dd EEE hh: mm: ss)==比;2009-03-10星期二08:09:04 *())(新日期。模式(“yyyy-M-d h: m:轮”)==比;2006-7-2 8:9:4.18 */Date.prototype.pattern=函数(fmt) { var o={ “M +”: this.getMonth() + 1,//月份 “d +”: this.getDate()//日 “h +”: this.getHours 12==0 () % & # 63;//12:this.getHours() % 12日小时 “H +”: this.getHours()//小时 “m +”: this.getMinutes()//分 “s +”: this.getSeconds()//秒 “q +”: Math.floor (this.getMonth() + 3)/3),//季度 “S”: this.getMilliseconds()//毫秒 }; var={周 “0”:“/u65e5”, “1”:“/u4e00”, “2”:“/u4e8c”, “3”:“/u4e09”, “4”:“/u56db”, “5”:“/u4e94”, “6”:“/u516d” }; 如果(/(y +)/test (fmt)) { fmt=fmt.replace (RegExp。1美元,(this.getFullYear () + " ")。substr(长度)4 - RegExp。1.美元); } 如果(/(E +)/test (fmt)) { fmt=fmt.replace (RegExp。1美元((RegExp。1.美元length> 1) & # 63;(RegExp。1.美元length> 2 & # 63;:“/u661f/u671f/u5468”): " ") +星期[this.getDay () + "]); } 在o k (var) { 如果新的正则表达式(“(“+ k +”)”)。test (fmt)) { fmt=fmt.replace (RegExp。1美元,1.美元(RegExp。长度==1)& # 63;(o [k]):((“00”+ o [k])。substr ((" + o [k]) . length))); } } 返回fmt; } var=new日期(); window.alert(日期。模式(yyyy-MM-dd hh: mm: ss));//? & lt;/script>JavaScript中日期对象的格式化代码方法汇总