js 的一些操作时间

时间格式转换(1):

Date.prototype.format = function(format) {
var date = {
"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+)/i.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for (var k in date) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1
? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
}
}
return format;
}
var d = new Date().format('yyyy-MM-dd');

时间格式转换(2):

function tran_data(date_str){
date_str = date_str.replace(/-/g,"/");//替换字符,变成标准格式
var d2=new Date();//取今天的日期
var d1 = new Date(Date.parse(date_str));
return d1;
}

时间加减:

function showdate(n)
{
var arr=Array();
var uom = new Date();
uom.setDate(uom.getDate()+n);
arr[0]= uom.getFullYear();
if(uom.getMonth()+1<10){
arr[1]="0"+(uom.getMonth()+1);
}else{
arr[1]=uom.getMonth()+1;
}
arr[2]=uom.getDate();
return arr;
}

时间比较

var start_time=tran_data(start_dd); ///转换时间戳比较  tran_data 为时间格式转换(2)
var end_time=tran_data(end_dd);

原文地址:https://www.cnblogs.com/chenkg/p/3727662.html