加法运算:
var date = new Date(2023, 5, 17, 12, 34, 56); var nextSecond = new Date(date.getTime() + 1000); console.log('输出结果:', nextSecond) //输出结果: Sat Jun 17 2023 12:34:57 GMT+0800 (中国标准时间)
减法运算:
var date1 = new Date(2023, 5, 17, 12, 34, 56); var date2 = new Date(2023, 5, 18, 12, 34, 56); var diff = date2.getTime() - date1.getTime(); console.log('输出结果:', diff) //输出结果: 86400000
时间间隔计算:
var date = new Date(2023, 5, 17, 12, 34, 56); var now = new Date(); var yearDiff = now.getFullYear() - date.getFullYear(); var monthDiff = now.getMonth() - date.getMonth(); var dayDiff = now.getDate() - date.getDate(); var hourDiff = now.getHours() - date.getHours(); var minuteDiff = now.getMinutes() - date.getMinutes(); var secondDiff = now.getSeconds() - date.getSeconds(); var millisecondDiff = now.getMilliseconds() - date.getMilliseconds(); console.log('输出yearDiff结果:', yearDiff) // 0 console.log('输出monthDiff结果:', monthDiff) // -1 console.log('输出dayDiff结果:', dayDiff) // 0 console.log('输出hourDiff结果:', hourDiff) // 11 console.log('输出minuteDiff结果:', minuteDiff) // -33 console.log('输出secondDiff结果:', secondDiff) // -25 console.log('输出millisecondDiff结果:', millisecondDiff) // 946
上面是“如何利用js语法计算时间”的全面内容,想了解更多关于 js 内容,请继续关注web建站教程。
当前网址:https://m.ipkd.cn/webs_4520.html
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!