亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JavaScript中各種時間轉(zhuǎn)換問題詳解(YYYY-MM-DD、時間戳、中國標(biāo)準(zhǔn)時間)

 更新時間:2024年02月03日 09:53:07   作者:yggjdle  
在某些場景下,需要將時間轉(zhuǎn)換為字符串進(jìn)行展示或傳遞,下面這篇文章主要給大家介紹了關(guān)于JavaScript中各種時間轉(zhuǎn)換問題(YYYY-MM-DD、時間戳、中國標(biāo)準(zhǔn)時間)的相關(guān)資料,需要的朋友可以參考下

1. 類型總結(jié)

  • 指定格式 YYYY-MM-DD HH:MM:SS
  • 時間戳
  • 中國標(biāo)準(zhǔn)時間 Sat Jan 30 2022 08:26:26 GMT+0800 (中國標(biāo)準(zhǔn)時間) new Date()獲得系統(tǒng)當(dāng)前時間就會是這種形式

2. 類型之間的轉(zhuǎn)換

  • 時間戳轉(zhuǎn)換為 yyyy-mm-dd或yyyy-MM-dd HH-mm-ss
function timestampToTime(timestamp) {
        var date = new Date(timestamp * 1000);//時間戳為10位需*1000,時間戳為13位的話不需乘1000
        var Y = date.getFullYear() + '-';
        var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1):date.getMonth()+1) + '-';
        var D = (date.getDate()< 10 ? '0'+date.getDate():date.getDate())+ ' ';
        var h = (date.getHours() < 10 ? '0'+date.getHours():date.getHours())+ ':';
        var m = (date.getMinutes() < 10 ? '0'+date.getMinutes():date.getMinutes()) + ':';
        var s = date.getSeconds() < 10 ? '0'+date.getSeconds():date.getSeconds();
        return Y+M+D+h+m+s;
    }
  • yyyy-mm-dd或yyyy-MM-dd HH-mm-ss 轉(zhuǎn)為時間戳
var stringTime = '2012-10-12 22:37:33';
//將獲取到的時間轉(zhuǎn)換成時間戳
var timestamp = Date.parse(new Date(stringTime));
  • 中國標(biāo)準(zhǔn)時間轉(zhuǎn)為 yyyy-mm-dd hh-mm-ss
 let y = date.getFullYear()
 let m = date.getMonth() + 1
 m = m < 10 ? ('0' + m) : m
 let d = date.getDate()
 d = d < 10 ? ('0' + d) : d
 let h =date.getHours()
 h = h < 10 ? ('0' + h) : h
 let M =date.getMinutes()
 M = M < 10 ? ('0' + M) : M
 let s =date.getSeconds()
 s = s < 10 ? ('0' + s) : s
 let dateTime= y + '-' + m + '-' + d + ' ' + h + ':' + M + ':' + s;
  • yyyy-mm-dd hh-mm-ss 轉(zhuǎn)為中國標(biāo)準(zhǔn)時間
    1、new Date(“month dd,yyyy hh:mm:ss”);
    2、new Date(“month dd,yyyy”);
    3、new Date(yyyy,mth,dd,hh,mm,ss); 注意:這種方式下,必須傳遞整型;
    4、new Date(yyyy,mth,dd);
    5、new Date(ms); 注意:ms:是需要創(chuàng)建的時間和 GMT時間1970年1月1日之間相差的毫秒數(shù);當(dāng)前時間與GMT1970.1.1之間的毫秒數(shù):var mills = new Date().getTime();

  • 時間戳轉(zhuǎn)為中國標(biāo)準(zhǔn)時間

const time = 1531065600000;//時間戳(數(shù)字)
const youData = new Data(time);
  • 中國標(biāo)準(zhǔn)時間轉(zhuǎn)為時間戳
Date.parse(Time)

3. Date類型

創(chuàng)建日期對象 let now = new Date();

在不給Date構(gòu)造函數(shù)傳參數(shù)的情況下,創(chuàng)建的對象將保存當(dāng)前日期和時間。要基于其他日期和時間創(chuàng)建日期對象,需要傳入毫秒表示。

方法:Date.parse() && Date.UTC() && Date.now() && Date.toLocaleString() && Date.toString()

Date.parse()支持的參數(shù)類型:

1) 月/日/年 eg:’1/18/2023‘

2) 月名 日,年 eg: ‘May 23, 2019’

3) 周幾 月名 日 年 時:分:秒 時區(qū) eg:’Wed Jan 18 2023 16:21:53 GMT+0800‘

4) YYYY-MM-DDTHH:mm:ss.sssZ eg: 2019-05-23T00:00:00

如果傳入的參數(shù)不表示日期,則返回NaN

用法

Date.UTC()2000年1月1日零點(diǎn)

2005年5月5日下午5點(diǎn)55分55秒(注意月份是0為起點(diǎn)的)

Date.now() 當(dāng)前時間

Date.toLocaleString() && Date.toString()

4. 日期格式化

toDateString()

toTimeString()

toLocaleDateString()

toLocaleTimeString()

toUTCString()

5. 如何判斷是否為當(dāng)天時間

if (new Date(str).toDateString() === new Date().toDateString()) {
    //今天
    console.log("當(dāng)天");
} else if (new Date(str) < new Date()){
    //之前
    console.log(new Date(str).toISOString().slice(0,10));
}

附獲取當(dāng)前日期:

 getCurrentDate() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
          month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
          strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
    },
 
 
//2018-12-1

總結(jié) 

到此這篇關(guān)于JavaScript中各種時間轉(zhuǎn)換問題的文章就介紹到這了,更多相關(guān)JS時間轉(zhuǎn)換問題內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論