JS 时间转换时间戳 并做时间判断

来源:互联网 发布:js window 关闭事件 编辑:程序博客网 时间:2024/06/06 02:37

<script>function transdate(endTime){var date=new Date();date.setFullYear(endTime.substring(0,4));date.setMonth(endTime.substring(5,7)-1);date.setDate(endTime.substring(8,10));date.setHours(endTime.substring(11,13));date.setMinutes(endTime.substring(14,16));date.setSeconds(endTime.substring(17,19));return Date.parse(date)/1000;}//alert(transdate('2010-03-19 13:16'));//alert(transdate('2010-03-20 13:16'));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 now = new Date();var nowStr = now.format("yyyy-MM-dd hh:mm"); //获取系统当前时间alert(nowStr);alert(transdate(nowStr));</script>


0 0
原创粉丝点击