js常用时间方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js常用时间方法相关的知识,希望对你有一定的参考价值。
//获取一段时间日期 YYYY-MM-DD
function getDays(dayNum) {
var oDate = new Date(); //获取当前时间
var dayArr = new Array();
for(var i = 0; i < dayNum; i++) {
var str = new Date(oDate.getFullYear(), oDate.getMonth(), oDate.getDate() + i);
dayArr.push(gmtToDate(str)); //把未来几天的时间放到数组里,通过gmtToDate(str)转换
}
return dayArr; //返回一个数组。
}
//GMT返回YYYY-MM-DD
function gmtToDate(str) {
var date = str;
var seperator1 = "-";
var year = date.getFullYear();
var month = date.getMonth() + 1;
var strDate = date.getDate();
if(month >= 1 && month <= 9) {
month = "0" + month;
}
if(strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = year + seperator1 + month + seperator1 + strDate;
return currentdate;
}
//获取当前的日期时间 格式“yyyy-MM-dd HH:MM:SS”
function getNowFormatDate() {
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
var year = date.getFullYear();
var month = date.getMonth() + 1;
var strDate = date.getDate();
var strSeconds = date.getSeconds();
if(month >= 1 && month <= 9) {
month = "0" + month;
}
if(strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
if(hours >= 0 && hours <= 9) {
hours = "0" + hours;
}
if(minutes >= 0 && minutes <= 9) {
minutes = "0" + minutes;
}
if(seconds >= 0 && seconds <= 9) {
seconds = "0" + seconds;
}
var currentdate = year + seperator1 + month + seperator1 + strDate +
" " + hours + seperator2 + minutes +
seperator2 + seconds;
return currentdate;
}
//unix时间戳转为 yyyy-MM-dd HH:MM:SS
function getNowFormatDateHMS(str) {
var date = new Date(str);
var seperator1 = "-";
var seperator2 = ":";
var year = date.getFullYear();
var month = date.getMonth() + 1;
var strDate = date.getDate();
var strSeconds = date.getSeconds();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
if(hours >= 0 && hours <= 9) {
hours = "0" + hours;
}
if(minutes >= 0 && minutes <= 9) {
minutes = "0" + minutes;
}
if(seconds >= 0 && seconds <= 9) {
seconds = "0" + seconds;
}
var currentdate = year + seperator1 + month + seperator1 + strDate +‘ ‘+ hours + seperator2 + minutes + seperator2 + seconds;
return currentdate;
}
//判断当前时间是否在某一时间段内 在返回true 不在返回false
function is_now_time_range(beginTime, endTime) {
var strb = beginTime.split(":");
if(strb.length != 2) {
beginTime = strb[0] + ‘:‘ + strb[1];
}
var stre = endTime.split(":");
if(stre.length != 2) {
endTime = stre[0] + ‘:‘ + stre[1];
}
var b = new Date();
var e = new Date();
var n = new Date();
b.setHours(strb[0]);
b.setMinutes(strb[1]);
e.setHours(stre[0]);
e.setMinutes(stre[1]);
if(n.getTime() - b.getTime() > 0 && n.getTime() - e.getTime() < 0) {
return true;
} else {
return false;
}
}
//判断某时间是否在某一时间段内 在返回true 不在返回false
function is_time_range(beginTime, endTime, nowTime) {
var strb = beginTime.split(":");
if(strb.length != 2) {
beginTime = strb[0] + ‘:‘ + strb[1];
}
var stre = endTime.split(":");
if(stre.length != 2) {
endTime = stre[0] + ‘:‘ + stre[1];
}
var strn = nowTime.split(":");
if(stre.length != 2) {
nowTime = strn[0] + ‘:‘ + strn[1];
}
var b = new Date();
var e = new Date();
var n = new Date();
b.setHours(strb[0]);
b.setMinutes(strb[1]);
e.setHours(stre[0]);
e.setMinutes(stre[1]);
n.setHours(strn[0]);
n.setMinutes(strn[1]);
if(n.getTime() - b.getTime() > 0 && n.getTime() - e.getTime() < 0) {
return true;
} else {
return false;
}
}
//判断俩时间差 大于返回true 否则返回false
function isTimeDifference(oneTime, anotherTime) {
var stro = oneTime.split(":");
if(stro.length != 2) {
oneTime = stro[0] + ‘:‘ + stro[1];
}
var stra = anotherTime.split(":");
if(stre.length != 2) {
anotherTime = stre[0] + ‘:‘ + stre[1];
}
var o = new Date();
var a = new Date();
o.setHours(stro[0]);
o.setMinutes(stro[1]);
a.setHours(stra[0]);
a.setMinutes(stra[1]);
if(o.getTime() > a.getTime()) {
return true;
} else {
return false;
}
}
以上是关于js常用时间方法的主要内容,如果未能解决你的问题,请参考以下文章