js time object

Table of contents

1. What is a time object

Second, the creation syntax of time objects

3. Function method of time object

1. Time object.getFullYear()

2. Time object.getMonth()

3. Time object get.Date()

4. Time object.getDay()

5. Time object.getHours()

6. Time object.getMinutes()

7. Time object.getSeconds()

8. Case: Get the current time

 4. Timestamp

1. Understanding of timestamp:

2. The purpose of the timestamp:

3. Get the timestamp

4. Calculate the current time from the Spring Festival: countdown to 00:00 on February 1, 2022

5. Timer delayer

1. Timer

2. Delay


1. What is a time object

Store record time data

Second, the creation syntax of time objects

Create a time object through the constructor

There is no parameter to create the time object of the current time by default
var variable = new Date();

Set parameters to create a time object at the specified time
var variable = new Date(parameter);

//当前时间的时间对象
var time = new Date();
console.log(time);

//指定时间的时间对象
//数字必须符合 时间数值范围

//参数语法形式1
//参数语法  年-月-日 时:分:秒
var time2 = new Date('2022-1-9 19:53:34');
console.log(time2);

//参数语法  年 月 日 时:分:秒
var time3 = new Date('2022 1 9 19:53:34');
console.log(time3);

//参数语法  年/月/日 时:分:秒
var time3 = new Date('2022/1/9 19:53:34');
console.log(time3);

//参数语法  年-月-日 时:分:秒
var time4 = new Date('2022,1,9 19:53:34');
console.log(time4);

//参数语法形式2 6个数字
//月份设定的数字是 0 - 11 对应 1 - 12 月
//数字格式 可以超出正常值范围 
//会自动向前一个时间单位进位

var time5 = new Date(2022,0,9,19,53,34);
console.log(time5);

//设定月份是15 对应的是 1年4个月
//向 年份单位进1 月份显示 4月
var time6 = new Date(2022,15,9,19,53,34);
console.log(time6);

Results of the:

3. Function method of time object

Get time object.get...()

Obtain the specific time data stored in the time object through the function method provided by the JavaScript program

1. Time object.getFullYear()

     Get the four-digit year

2. Time object.getMonth()

     get the month

         The obtained result is a number from 0 to 11

         Corresponding to the month from 1 to 12

3. Time object get.Date()

     get date

4. Time object.getDay()

     get week

         The obtained result is a number from 0 to 6

         Corresponding to Sunday ~ Saturday

5. Time object.getHours()

     get hours

6. Time object.getMinutes()

     get minutes

7. Time object.getSeconds()

     get seconds

8. Case: Get the current time

//创建时间对象
var currentTime = new Date();

//向控制台输出 数据
console.log(currentTime);
//向控制台输出 属性属性值
console.dir(currentTime);

//获取 时间对象中的具体的时间数据

//获取4 位年份
var year = currentTime.getFullYear();
console.log(year);

//获取月份
//获取的是0 ~ 11,进行+1操作,输出1~12
var month = (currentTime.getMonth() + 1) < 10 ? '0' + (currentTime.getMonth() + 1) : currentTime.getMonth() + 1;
console.log(month);

//获取日期
var day = currentTime.getDate() < 10 ? '0' + currentTime.getDate() : currentTime.getDate();

//获取星期
//获取结果0~6
//可以设定一个星期的数组
var weekArr = [ '星期日' , '星期一' , ' 星期二' , '星期三' , '星期四' , '星期五' , '星期六' ] ;
var week = weekArr[currentTime.getDay()];
console.log(week);

//获取小时
//获取的是对应小时 本地时间
var hour = currentTime.getHours() < 10 ? '0' + currentTime.getHours() : currentTime.getHours();
console.log(hour);

//获取的是对应的小时 世界标准时间
//var hour = currentTime.getUTCHours();

//获取分钟
var minute = currentTime.getMinutes() < 10 ? '0' + currentTime.getMinutes() : currentTime.getMinutes();
console.log(minute);

//获取秒
var seconds = currentTime.getSeconds() < 10 ? '0' + currentTime.getSeconds() : currentTime.getSeconds();
console.log(seconds);

       Results of the:

 4. Timestamp

1. Understanding of timestamp:

The time difference from January 1, 1970 at 0:00:00

The unit of timestamp in JavaScript is milliseconds

1 second = 1000 milliseconds

2. The purpose of the timestamp:

       Generally use timestamp to calculate time difference

       Then convert the time difference into the corresponding days, hours, minutes and seconds

3. Get the timestamp

//创建时间对象
var time = newDate();

//获取时间戳
var td = time.getTime();

4. Calculate the current time from the Spring Festival: countdown to 00:00 on February 1, 2022

4.1 Create a time object

              Start time --- current time

              End time --- set time

4.2 Calculate time difference

              Timestamp of end time - Timestamp of start time

              convert to seconds

 4.3 Convert Time Difference (Seconds) to Days Hours Minutes Seconds

//创建当前时间的时间对象
var startTime = new Date();
var endTime = new Date('2022/2/1 00:00:00');

//计算时间差
var td = parseInt((endTime.getTime() - startTime.getTime()) / 1000);

//换算为 天 小时 分钟 秒 前导补零

// 天
var day = parseInt( td / (24*60*60) );

// 小时 
var hour = parseInt( td % (24*60*60) / (60*60) );

// 分钟
var minute = parseInt( td % (60*60) / 60 );

// 秒
var seconds = td % 60 ;

//页面输出
document.write(`距离春节还剩下${day < 10 ? '0' + day : day}天${hour < 10 ? '0' + hour : hour}小时${minute < 10 ? '0' + minute : minute}分钟${seconds < 10 ? '0' + seconds : seconds}秒`);

Results of the:

5. Timer delayer

1. Timer

      Execute the program every set time interval according to the set time interval

      The unit of time interval is milliseconds     

      That is, the program is repeatedly executed according to the interval

 grammar

      setInterval(parameter 1, parameter 2);

           Parameter 1 The function program to execute

           Parameter 2 time interval

//定时器
//每间隔3000毫秒也就是3秒 执行一次 匿名函数中定义的程序

//语法形式1
setInterval(function() {console.log('正在努力写博客')},3000);

//语法形式2
setInterval(fn,3000);
function fn() {
    console.log('正在努力写博客')
}

2. Delay

      Delay execution of a program according to the interval time

      That is, delay the execution of the program according to the set time interval

grammar

      setTimeout(parameter 1, parameter 2);

           Parameter 1 The function program to execute

           Parameter 2 time interval

//延时器
//间隔3秒,只会触发一次

//语法形式1
setTimeout(function() {console.log('正在努力写博客')},3000);

//语法形式2
setTimeout(fn,3000);
function fn() {
   console.log('正在努力写博客');  
}

6. Clear the timer delayer

grammar

     clearInterval(parameter)

     clearTimeout(parameter)

These two functions can both clear the timer and clear the delayer

The parameter is the serial number of the timer or delayer

// 定时器 延时器的执行效果 是 按照间隔的时间 触发 参数1 设定的函数程序
// 它们执行结果返回值 是 定时器 延时器函数内部定义的return的数值
// 也就意味着 变量中 存储的是 定时器延时器函数中 return 返回的数据数值 
// 也就是 定时器延时器的编号序号
var num1 = setTimeout( function(){console.log(111)} , 1000 );
var num2 = setTimeout( function(){console.log(222)} , 2000 );
var num3 = setTimeout( function(){console.log(333)} , 3000 );
var num4 = setTimeout( function(){console.log(444)} , 4000 );

var num5 = setInterval( function(){console.log(555)} , 1000 );

console.log(num1); // 执行结果 1
console.log(num2); // 执行结果 2
console.log(num3); // 执行结果 3
console.log(num4); // 执行结果 4
console.log(num5); // 执行结果 5

// 清除的参数是 定时器 延时器 的 序号编号 
// 可以直接定义数字,但是我们一般不这样做
//因为在实际项目中,有很多行代码,我们手动计算序号,很难操作 
clearInterval(1);
// 可以 使用变量 存储 定时器延时器的执行结果返回值
clearInterval(num2);

Guess you like

Origin blog.csdn.net/weixin_58448088/article/details/122396294