JS 获取当前时间和日期,以及是否处于有效期内

JS 获取当前时间和日期,以及是否处于有效期内

当前时间和日期

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(); //获取日期与时间

日期加0

// 获得当前时间 2019-02-02 14:06:08

function getNowTime() {

// 加0

function add_10(num) {

if (num < 10) {

num = '0' + num

}

return num;

}

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 nowTime = myDate.getFullYear() + '-' + add_10(myDate.getMonth()) + '-' + myDate.getDate() + ' ' + add_10(myDate.getHours()) + ':' + add_10(myDate.getMinutes()) + ':' + add_10(myDate.getSeconds());

return nowTime;

}

获取一周前的日期

//当天

var Date1 = new Date();

//前一天

var Date2 = new Date(Date1.getTime() - 24*60*60*1000);

//前两天

var Date3 = new Date(Date1.getTime() - 48*60*60*1000);

//前三天

var Date4 = new Date(Date1.getTime() - 72*60*60*1000);

//前四天

var Date5 = new Date(Date1.getTime() - 96*60*60*1000);

//前五天

var Date6 = new Date(Date1.getTime() - 120*60*60*1000);

//前六天

var Date7 = new Date(Date1.getTime() - 144*60*60*1000);

js 判断当前时间是否处于某个时间段内

js 判断当前时间(或者所选时间)是否在某一时间范围,js 日期比较大小,js判断日期是否在区间内,js判断时间段是否在另外一个时间段内

// 传入 beginDateStr (开始时间), endDateStr(结束时间)

/**

* [isDuringDate 比较当前时间是否在指定时间段内]

* @author alan

* @DateTime 2019-08-21

* @version 1.0

* @param date [beginDateStr] [开始时间]

* @param date [endDateStr] [结束时间]

* @return Boolean

*/

var date = {

isDuringDate: function (beginDateStr, endDateStr) {

var curDate = new Date(),

beginDate = new Date(beginDateStr),

endDate = new Date(endDateStr);

if (curDate >= beginDate && curDate <= endDate) {

return true;

}

return false;

}

}

示例

date.isDuringDate('2018/09/17', '2030/09/17');

// 当前时间是否在2018/09/17 - 2030/09/17 之间,输出 true

date.isDuringDate('2018/09/17 13:00', '2019/09/17 15:00');

// 当前时间是否在2018/09/17 13:00 - 2019/09/17 15:00 之间,输出 false

date.isDuringDate('2018-09-17 13:00', '2019-09-17 15:00');

// 当前时间是否在2018/09/17 13:00 - 2019-09-17 15:00 之间,输出 false

https://www.cnblogs.com/sirdong/archive/2019/09/18/11542153.html

相关推荐

什么是大数据——史上最全大数据理论解析
365商城官网下载

什么是大数据——史上最全大数据理论解析

🌍 07-08 👁️ 4883
uzi小狗的名怎么来的-为什么叫uzi狂小狗
完美365体育ios下载

uzi小狗的名怎么来的-为什么叫uzi狂小狗

🌍 07-29 👁️ 8571
物流公司会计做账流程 会计分录怎么做?
完美365体育ios下载

物流公司会计做账流程 会计分录怎么做?

🌍 07-29 👁️ 6936