当前位置: 移动技术网 > IT编程>开发语言>JavaScript > js获取任意一天的0点和23:59:59时间

js获取任意一天的0点和23:59:59时间

2019年07月23日  | 移动技术网IT编程  | 我要评论

最近写代码时,需要获取任意一天的起始和结束时间,0点和23:59:59这两个时间的时间戳

使用了sethours() 方法

sethours() 方法用于设置指定的时间的小时字段

1. 获取当天开始时间

new date(new date(new date().tolocaledatestring()).gettime()))

2. 获取当天结束时间

new date(new date(new date().tolocaledatestring()).gettime() + 24 * 60 * 60 * 1000 - 1))

3. 获取任意一天的开始时间

// time为某一天的时间戳
starttime(time) {
    const nowtimedate = new date(time)
    return nowtimedate.sethours(0, 0, 0, 0)
}

4. 获取任意一天的结束时间

endtime(time) {
    const nowtimedate = new date(time)
    return nowtimedate.sethours(23, 59, 59, 999)
}

 

sethours方法地址:https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/date/sethours

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网