1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > js获取当前时间戳()+时间大小比较

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

时间:2020-10-11 07:50:20

相关推荐

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

1.js获取当前时间戳

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

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

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

先指个路:

js时间比较大小,时间加减 - xue11hua - 博客园第一种: 第二种: //时间戳比较 startTime=Date.parse(starttime); endTime=Date.parse(endTime); //进行比较 startTime>/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());//时间戳比较,结果正确

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。