JS常见用法(3)-- 日期/时间操作

1、获取当前时间,日期格式为:年/月/日/时/分/秒

var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var seconds = date.getSeconds();
if (month < 10) {
  month = "0" + month;
}
if (day < 10) {
 day = "0" + day;
}
if (hour < 10) {
   hour = "0" + hour;
}
if (minute < 10) {
  minute = "0" + minute;
}
if (seconds < 10) {
  seconds = "0" + seconds;
}
this.date = year + '/' + month + '/' + day + '/' + hour + '/' + minute + '/' + seconds

https://www.cnblogs.com/mmcm/p/5868176.html

2、时间戳与日期格式的相互转换

  • 时间戳转为日期格式
// 时间戳转日期
        formatDate (time) {
            let now = new Date(time*1000) // 时间戳为10位时,需要乘1000
            let year = now.getFullYear();
            let month = (now.getMonth()+1)>9?now.getMonth()+1:'0'+(now.getMonth()+1);
            let date = now.getDate()>9?now.getDate():'0'+now.getDate();
            let hour = now.getHours()>9?now.getHours():'0'+now.getHours();
            let minute = now.getMinutes()>9?now.getMinutes():'0'+now.getMinutes();
            let second = now.getSeconds()>9?now.getSeconds():'0'+now.getSeconds();
            return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
        }

https://www.cnblogs.com/crf-Aaron/archive/2017/11/16/7844462.htm

3、定时器 (this.frame_length满24为1秒)

{{hours}}: {{minutes}}: {{seconds}}: {{frame_length}}
// 开始计时

参考:https://blog.csdn.net/qwe111qsx/article/details/80859221

5、获取昨天,今天,明天

        let date = new Date()
        let today = date.getFullYear() + '-' + (date.getMonth() + 1 > 9 ? date.getMonth() + 1 : '0' + (date.getMonth() + 1)) + '-' + (date.getDate() > 9 ? date.getDate() : '0' + date.getDate()) + ' 10:00:00';
        let date2 = new Date()
        date2.setDate(date.getDate() + 1);  // 计算昨天,减1
        let tomorrow = date2.getFullYear() + '-' + (date2.getMonth() + 1 > 9 ? date2.getMonth() + 1 : '0' + (date2.getMonth() + 1)) + '-' + (date2.getDate() > 9 ? date2.getDate() : '0' + date2.getDate()) + ' 10:00:00';
        this.form.time_frame = [today, tomorrow] // 默认时间为今天早上十点到明天早上十点

https://www.cnblogs.com/sxxjyj/p/6093326.html

时间格式 2016-08-15T16:00:00.000Z

https://blog.csdn.net/m0_37983376/article/details/78202770

JS中Date.parse方法返回NaN解决方案

https://www.cnblogs.com/treerain/p/Javascrip_Date.html

js 根据日期判断周几

var weekDay = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];  
var myDate = new Date(Date.parse("2018/5/19"));  
console.log(weekDay[myDate.getDay()]);    // 星期六

https://blog.csdn.net/m0_37793545/article/details/80395351

当前时间,实时刷新(定时器 setInterval)

  • 注意:
beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer); // 在Vue实例销毁前,清除我们的定时器
    }
  }

https://www.cnblogs.com/lidonglin/p/11362384.html

倒计时

https://www.jianshu.com/p/c1b1d8df4a35

获取第二天的时间

https://www.xuebuyuan.com/1038866.html

你可能感兴趣的:(JS常见用法(3)-- 日期/时间操作)