欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript Date對象詳解及時間戳和時間的相互轉(zhuǎn)換問題

 更新時間:2024年01月20日 10:29:23   作者:~black-  
這篇文章主要介紹了JavaScript Date對象詳解及時間戳和時間的相互轉(zhuǎn)換問題,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧

一、Date對象詳解

1.Date對象

Date 對象用于處理日期與時間。

2.創(chuàng)建Date對象

let date = new Date();

3.Date對象屬性

屬性描述
constuctor返回對創(chuàng)建此對象的Date函數(shù)的引用
prototype使您有能力向?qū)ο筇砑訉傩院头椒?/td>

4.Date對象方法

 這個是我今天的日期

(1)getDate() 返回一個月中的某一天(1-31)

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

(2)getDay() 返回一周中的某一天(0-6)

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

 (3)getFullYear() 返回年份

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

(4)getHours() 返回Date對象的小時(0-23)

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

(5)getMillseconds() 返回Date對象的毫秒數(shù)(0-999)

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

 

 (6)getMinutes() 返回Date對象的月份(0-11)因為月份返回0-11,因此一般獲取月份都要  + 1

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

 (7)getSeconds() 返回Date對象的秒數(shù)(0-59)

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

 (8)getTime() 返回1970年1月一日至今的毫秒數(shù),即時間戳

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

這些就是我們經(jīng)常使用的Date對象的方法,其他方法可參考菜鳥教程

JavaScript Date 對象 | 菜鳥教程
JavaScript Date 對象 Date 對象 Date 對象用于處理日期與時間。 創(chuàng)建 Date 對象: new Date() 以下四種方法同樣可以創(chuàng)建 Date 對象: var d = new Date(); var d = new Date(milliseconds); // 參數(shù)為毫秒 var d = new Date(dateString); var d = new Date(year, month, day, h..

https://www.runoob.com/jsref/jsref-obj-date.html

5.Date對象的應(yīng)用(節(jié)流函數(shù)時間戳寫法)

Date.now()  相當(dāng)于 date.getTIme() 獲取時間戳

            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();
                    }
                };
            }

二、時間戳和時間的相互轉(zhuǎn)換

1.時間轉(zhuǎn)換為時間戳

            // Date.parse()不推薦這種寫法,毫秒級別的數(shù)值直接被轉(zhuǎn)化為000
            let date1 = Date.parse(new Date());
            // valueOf()函數(shù)返回指定對象的原始值獲得準確的時間數(shù)值
            let date2 = new Date().valueOf();
            // getTime()通過原型方法直接獲得當(dāng)前時間的毫秒數(shù)
            let date3 = new Date().getTime();
            // Number() 將時間轉(zhuǎn)化為一個number類型的數(shù)值
            let date4 = Number(new Date());
            console.log(date1);
            console.log(date2);
            console.log(date3);
            console.log(date4);

打印結(jié)果如下

2.時間戳轉(zhuǎn)換為時間

 (1)自己手寫轉(zhuǎn)換函數(shù)利用 Date對象的方法

            // 時間戳轉(zhuǎn)換成時間格式
            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));

另一種寫法,可以根據(jù) now.toTimeString().substr(0, 8) 獲取后面的時分秒格式,更加簡便

            // 時間戳轉(zhuǎn)換成時間格式
            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));

輸出結(jié)果如下

 (2)可以使用replace方法進行轉(zhuǎn)置

            function getLocalTime(nS) {
                // parseInt可解析一個字符串,并返回一個整數(shù)
                // toLocalString()方法可根據(jù)本地時間把date對象轉(zhuǎn)換為字符串,并返回結(jié)果
                // replace利用正則進行匹配替換
                return new Date(parseInt(nS)).toLocaleString().replace(/:\d{1,2}$/, ' ');
            }
            console.log(getLocalTime(1685671570448));

輸出結(jié)果如下

到此這篇關(guān)于JavaScript Date對象詳解及時間戳和時間的相互轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)JavaScript Date對象內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論