js获取当前时间戳()+时间大小比较

1.js获取当前时间戳 

js 中日期转换成时间戳 - mmzz3322 - 博客园js字符串转化时间戳可以使用自带函数 Date(要转化的时间字符串)先转化为Date类型,之后再将Date类型转化为时间戳类型,其中时间字符串有要求,形式必须是 yyyy-MM-dd HH:mm:sshttps://www.cnblogs.com/mmzz3322/p/11218017.html上面这个博客已经写的很清楚了,试了一下new Date()里面的参数是不是一下几种日期格式都可以:

let nowDate = new Date()
let nowTime = new Date(nowDate).getTime()//精确到毫秒
let startTime = new Date('2021-01-01').getTime()//用“-”连接日期才是【标准格式】
let endTime = new Date('2021.01.01 17:10:25').getTime()
let Time = new Date('2021/01/01 17:10:25:12').getTime()
console.log('nowDate时间:',nowDate,'\nnowDate时间戳:',nowTime);
console.log('2021-01-01','\t',startTime);
console.log('2021.01.01 17:10:25','\t',endTime);
console.log('2021/01/01 17:10:25:12','\t',Time);

2.比较时间大小以及时间的加减

先指个路:
js时间比较大小,时间加减 - xue11hua - 博客园第一种: 第二种: //时间戳比较 startTime=Date.parse(starttime); endTime=Date.parse(endTime); //进行比较 startTime>https://www.cnblogs.com/aSnow/p/9144473.html

需要的日期是年月日的哪一个不同就用获取日期方法中哪一个的方法(比如:getFullYear)做加减即可,时间也是。

试验一下:

1.年份加减:

let nowDate = new Date()
let today = nowDate.getFullYear() + '-' + (nowDate.getMonth()+1) + '-' + nowDate.getDate()
let lastYear = (nowDate.getFullYear()-1) + '-' + (nowDate.getMonth()+1) + '-' + nowDate.getDate()
let nextYear = (nowDate.getFullYear()+1) + '-' + (nowDate.getMonth()+1) + '-' + nowDate.getDate()
console.log('今天:',today);
console.log('去年:',lastYear);
console.log('明年:',nextYear);
console.log('去年<明年?:',lastYear < nextYear);//比较时间大小,直接用标准日期格式比较

 

 2.月份加减:

let nowDate = new Date()
let today = nowDate.getFullYear() + '-' + (nowDate.getMonth()+1) + '-' + nowDate.getDate()
let lastMonth = nowDate.getFullYear() + '-' + (nowDate.getMonth()+1-1) + '-' + nowDate.getDate()
let nextMonth = nowDate.getFullYear() + '-' + (nowDate.getMonth()+1+1) + '-' + nowDate.getDate()
console.log('今天:',today);
console.log('上个月:',lastMonth);
console.log('下个月:',nextMonth);
console.log('上个月<下个月?:',lastMonth < nextMonth);//标准格式比较,结果错误
console.log('上个月<下个月?:', new Date(lastMonth).getTime() < new Date(nextMonth).getTime());//时间戳比较,结果正确

 

3.前后两天:

let nowDate = new Date()
let today = nowDate.getFullYear() + '-' + (nowDate.getMonth()+1) + '-' + nowDate.getDate()
let lastday = nowDate.getFullYear() + '-' + (nowDate.getMonth()+1) + '-' + (nowDate.getDate()-1)
let nextday = nowDate.getFullYear() + '-' + (nowDate.getMonth()+1) + '-' + (nowDate.getDate()+1)
console.log('今天:',today);
console.log('昨天:',lastday);
console.log('明天:',nextday);
console.log('昨天<明天?:',lastday < nextday);//标准格式比较,结果正确
console.log('昨天<明天?:', new Date(lastday).getTime() < new Date(nextday).getTime());//时间戳比较,结果正确

 

 4.前后几小时:

let nowDate = new Date()
let today = nowDate.getFullYear() + '-' + (nowDate.getMonth()+1) + '-' + nowDate.getDate() + ' ' + nowDate.getHours() + ':' + nowDate.getMinutes() + ':' + nowDate.getSeconds()
let lastday = nowDate.getFullYear() + '-' + (nowDate.getMonth()+1) + '-' + nowDate.getDate() + ' ' + (nowDate.getHours()-2) + ':' + nowDate.getMinutes() + ':' + nowDate.getSeconds()
let nextday = nowDate.getFullYear() + '-' + (nowDate.getMonth()+1) + '-' + nowDate.getDate() + ' ' + (nowDate.getHours()+2) + ':' + nowDate.getMinutes() + ':' + nowDate.getSeconds()
console.log('现在:',today);
console.log('两小时前:',lastday);
console.log('两小时后:',nextday);
console.log('两小时前<两小时后?:',lastday < nextday);//标准格式比较,结果正确
console.log('两小时前<两小时后?:', new Date(lastday).getTime() < new Date(nextday).getTime());//时间戳比较,结果正确

 

 试验结论:本来想试试可不可以直接用标准格式的时间直接比较大小,结果显示,月份不同时标准格式比较出错了,所以还是用时间戳进行大小比较吧。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值