【www.bbyears.com--Access】
经常遇到要对时间戳进行格式化,但基本上都是临时需要随时写,从未整理过,今天又遇到对时间戳进行格式化,于是记载下来,以后需要直接拿过来用。
废话不多说,先把各种格式化方法贴给大家
var myDate = new Date();
myDate.getYear(); //获取当前年份(2位)
myDate.getFullYear(); //获取完整的年份(4位,1970-????)
myDate.getMonth(); //获取当前月份(0-11,0代表1月)
myDate.getDate(); //获取当前日(1-31)
myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours(); //获取当前小时数(0-23)
myDate.getMinutes(); //获取当前分钟数(0-59)
myDate.getSeconds(); //获取当前秒数(0-59)
myDate.getMilliseconds(); //获取当前毫秒数(0-999)
myDate.toLocaleDateString(); //获取当前日期
var mytime=myDate.toLocaleTimeString(); //获取当前时间
myDate.toLocaleString( ); //获取日期与时间
可以说是Web项目中不可或缺的一个Javascript类库,它可以帮助你快速的解决客户端编程的许多问题,下面贴出一个用js格式化时间的方法。
Date.prototype.format =function(format) { var o = { "M+" : this.getMonth()+1, //month "d+" : this.getDate(), //day "h+" : this.getHours(), //hour "m+" : this.getMinutes(), //minute "s+" : this.getSeconds(), //second "q+" : Math.floor((this.getMonth()+3)/3), //quarter "S" : this.getMilliseconds() //millisecond } if(/(y+)/.test(format)) format=format.replace(RegExp.$1, (this.getFullYear()+"").substr(4- RegExp.$1.length)); for(var k in o)if(new RegExp("("+ k +")").test(format)) format = format.replace(RegExp.$1, RegExp.$1.length==1? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); return format; }
以上代码必须先声明,然后在使用。使用方法:
var d =new Date().format('yyyy-MM-dd'); 先看代码: Date.prototype.format = function (format) { format = format || 'YYYY-MM-DD hh:mm:ss'; var date = { YYYY: this.getFullYear(), YY: this.getYear(), MM: (this.getMonth() > 8 ? '' : '0') + (this.getMonth() + 1), M: this.getMonth() + 1, DD: (this.getDate() > 9 ? '' : '0') + this.getDate(), D: this.getDate(), hh: this.getHours(), mm: (this.getMinutes() > 9 ? '' : '0') + this.getMinutes(), ss: (this.getSeconds() > 9 ? '' : '0') + this.getSeconds(), h: this.getHours(), m: this.getMinutes(), s: this.getSeconds() }; var arr = format.match(/[a-zA-Z]+/g); for (var i = 0; i < arr.length; i++) { format = format.replace(arr[i], date[arr[i]] || ''); } return format; }
具体的参数可以看函数内date对象。
如:new Date("2014-7-23 21:40:54").format("YYYY-MM-DD hh:mm:ss") 得到的结果为:2014-07-23 21:41:36;
如:new Date("2014-7-23 21:41:56").format("YYYY-M-DD hh:mm:ss") 得到的结果为:2014-7-23 21:42:39
注意上面两个例子中的月份,格式中为MM则对应07,为M则对应7.
双字母表示小于10前面补0,单字母则表示小于10不补0。 月、日、分、秒 都可以支持这样设置 。
日期对象为:Mon Sep 21 15:24:14 UTC+0800 2015
<script type="text/javascript"> Date.prototype.format = function(format) { var o = { "M+" : this.getMonth()+1, //month "d+" : this.getDate(), //day "h+" : this.getHours(), //hour "m+" : this.getMinutes(), //minute "s+" : this.getSeconds(), //second "q+" : Math.floor((this.getMonth()+3)/3), //quarter "S" : this.getMilliseconds() //millisecond } if(/(y+)/.test(format)) { format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length)); } for(var k in o) { if(new RegExp("("+ k +")").test(format)) { format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); } } return format; } var d = new Date(); document.write('日期对象为:'); document.write(d); var str = d.format('yyyy-MM-dd'); var today = document.getElementById("todayButton"); today.value = str; </script>
例子3