1. 類型總結(jié)
- 指定格式 YYYY-MM-DD HH:MM:SS
- 時(shí)間戳
- 中國(guó)標(biāo)準(zhǔn)時(shí)間 Sat Jan 30 2022 08:26:26 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)
new Date()
獲得系統(tǒng)當(dāng)前時(shí)間就會(huì)是這種形式
2. 類型之間的轉(zhuǎn)換
- 時(shí)間戳轉(zhuǎn)換為 yyyy-mm-dd或yyyy-MM-dd HH-mm-ss
function timestampToTime(timestamp) {
var date = new Date(timestamp * 1000);//時(shí)間戳為10位需*1000,時(shí)間戳為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)為時(shí)間戳
var stringTime = '2012-10-12 22:37:33';
//將獲取到的時(shí)間轉(zhuǎn)換成時(shí)間戳
var timestamp = Date.parse(new Date(stringTime));
- 中國(guó)標(biāo)準(zhǔn)時(shí)間轉(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)為中國(guó)標(biāo)準(zhǔn)時(shí)間
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:是需要?jiǎng)?chuàng)建的時(shí)間和 GMT時(shí)間1970年1月1日之間相差的毫秒數(shù);當(dāng)前時(shí)間與GMT1970.1.1之間的毫秒數(shù):var mills = new Date().getTime(); -
時(shí)間戳轉(zhuǎn)為中國(guó)標(biāo)準(zhǔn)時(shí)間
const time = 1531065600000;//時(shí)間戳(數(shù)字)
const youData = new Data(time);
- 中國(guó)標(biāo)準(zhǔn)時(shí)間轉(zhuǎn)為時(shí)間戳
Date.parse(Time)
3. Date類型
創(chuàng)建日期對(duì)象 let now = new Date();
在不給Date構(gòu)造函數(shù)傳參數(shù)的情況下,創(chuàng)建的對(duì)象將保存當(dāng)前日期和時(shí)間。要基于其他日期和時(shí)間創(chuàng)建日期對(duì)象,需要傳入毫秒表示。
方法:Date.parse() && Date.UTC() && Date.now() && Date.toLocaleString() && Date.toString()
Date.parse()
支持的參數(shù)類型:
1) 月/日/年 eg:’1/18/2023‘
2) 月名 日,年 eg: ‘May 23, 2019’
3) 周幾 月名 日 年 時(shí):分:秒 時(shí)區(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)前時(shí)間
Date.toLocaleString() && Date.toString()
4. 日期格式化
toDateString()
toTimeString()
toLocaleDateString()
toLocaleTimeString()文章來源:http://www.zghlxwxcb.cn/news/detail-813201.html
toUTCString()文章來源地址http://www.zghlxwxcb.cn/news/detail-813201.html
5. 如何判斷是否為當(dāng)天時(shí)間
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));
}
到了這里,關(guān)于Js各種時(shí)間轉(zhuǎn)換問題(YYYY-MM-DD 時(shí)間戳 中國(guó)標(biāo)準(zhǔn)時(shí)間)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!