Detailed explanation of JavaScript Date object and conversion between timestamp and time

Table of contents

1. Detailed Explanation of Date Object

1.Date object

2. Create a Date object

3.Date object properties

4. Date object method

5. Application of Date object (throttling function timestamp writing method)

2. Mutual conversion between timestamp and time

1. Convert time to timestamp

2. Convert timestamp to time


1. Detailed Explanation of Date Object

1.Date object

Date objects are used to work with dates and times.

2. Create a Date object

 let date = new Date();

3.Date object properties

Attributes describe
constuctor Returns a reference to the Date function that created this object
prototype Gives you the ability to add properties and methods to objects

4. Date object method

 this is my date today

(1) getDate() returns a day of the month (1-31)

            let date = new Date();
            console.log(date.getDate());

(2) getDay() returns a day of the week (0-6)

            let date = new Date();
            console.log(date.getDay());

 (3) getFullYear() returns the year

            let date = new Date();
            console.log(date.getFullYear());

(4) getHours() returns the hour (0-23) of the Date object

            let date = new Date();
            console.log(date.getHours());

(5) getMillseconds() returns the milliseconds of the Date object (0-999)

            let date = new Date();
            console.log(date.getMilliseconds());

 

 (6) getMinutes() returns the month (0-11) of the Date object, because the month returns 0-11, so it is generally necessary to obtain the month + 1

            let date = new Date();
            console.log(date.getMonth());

 

 (7) getSeconds() returns the seconds of the Date object (0-59)

            let date = new Date();
            console.log(date.getSeconds());

 (8) getTime() returns the number of milliseconds from January 1, 1970 to the present, that is, the timestamp

            let date = new Date();
            console.log(date.getTime());

 

These are the methods of the Date object that we often use. For other methods, please refer to the rookie tutorial

JavaScript Date Object | Novice Tutorial JavaScript Date Object Date Object The Date object is used to work with dates and times. Create a Date object: new Date() The following four methods can also create a Date object: var d = new Date(); var d = new Date(milliseconds); // The parameter is milliseconds var d = new Date(dateString); var d = new Date(year, month, day, h.. https://www.runoob.com/jsref/jsref-obj-date.html

5. Application of Date object (throttling function timestamp writing method)

Date.now() is equivalent to date.getTIme() to get the timestamp

            function throttled(fn, delay) {
                let oldTime = Date.now();
                return function (...args) {
                    let newTIme = Date.now();
                    if (newTIme - oldTime >= delay) {
                        fn.apply(this, args);
                        oldTime = Date.now();
                    }
                };
            }

2. Mutual conversion between timestamp and time

1. Convert time to timestamp

            // Date.parse()不推荐这种写法,毫秒级别的数值直接被转化为000
            let date1 = Date.parse(new Date());
            // valueOf()函数返回指定对象的原始值获得准确的时间数值
            let date2 = new Date().valueOf();
            // getTime()通过原型方法直接获得当前时间的毫秒数
            let date3 = new Date().getTime();
            // Number() 将时间转化为一个number类型的数值
            let date4 = Number(new Date());
            console.log(date1);
            console.log(date2);
            console.log(date3);
            console.log(date4);

The print result is as follows

2. Convert timestamp to time

 (1) Handwritten conversion function using Date object method

            // 时间戳转换成时间格式
            var formatDate = function (date) {
                // 如果传入的时间戳为10位需*1000,时间戳为13位的话不需乘1000
                date = new Date(date);
                var y = date.getFullYear();
                var m = date.getMonth() + 1;
                var d = date.getDate();
                var h = date.getHours();
                var m1 = date.getMinutes();
                var s = date.getSeconds();
                // 过滤格式
                m = m < 10 ? '0' + m : m;
                d = d < 10 ? '0' + d : d;
                h = h < 10 ? '0' + h : h;
                m1 = m1 < 10 ? '0' + m1 : m1;
                s = s < 10 ? '0' + s : s;
                // 拼接时间格式
                return y + '-' + m + '-' + d + ' ' + h + ':' + m1 + ':' + s;
            };
            console.log(formatDate(1685671570448));

Another way of writing, you can get the following hour, minute and second format according to now.toTimeString().substr(0, 8) , which is more convenient

            // 时间戳转换成时间格式
            var formatDate = function (date) {
                // 如果传入的时间戳为10位需*1000,时间戳为13位的话不需乘1000
                date = new Date(date);
                var y = date.getFullYear();
                var m = date.getMonth() + 1;
                var d = date.getDate();

                // 过滤格式
                m = m < 10 ? '0' + m : m;
                d = d < 10 ? '0' + d : d;

                return y + '-' + m + '-' + d + ' ' + date.toTimeString().substr(0, 8);
            };
            console.log(formatDate(1685671570448));

The output is as follows

 (2) You can use the replace method to transpose

            function getLocalTime(nS) {
                // parseInt可解析一个字符串,并返回一个整数
                // toLocalString()方法可根据本地时间把date对象转换为字符串,并返回结果
                // replace利用正则进行匹配替换
                return new Date(parseInt(nS)).toLocaleString().replace(/:\d{1,2}$/, ' ');
            }
            console.log(getLocalTime(1685671570448));

The output is as follows

Guess you like

Origin blog.csdn.net/qq_63299825/article/details/130999891