JavaScript中各種時間轉換問題詳解(YYYY-MM-DD、時間戳、中國標準時間)
1. 類型總結
- 指定格式 YYYY-MM-DD HH:MM:SS
- 時間戳
- 中國標準時間 Sat Jan 30 2022 08:26:26 GMT+0800 (中國標準時間)
new Date()
獲得系統(tǒng)當前時間就會是這種形式
2. 類型之間的轉換
- 時間戳轉換為 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 轉為時間戳
var stringTime = '2012-10-12 22:37:33'; //將獲取到的時間轉換成時間戳 var timestamp = Date.parse(new Date(stringTime));
- 中國標準時間轉為 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 轉為中國標準時間
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ù);當前時間與GMT1970.1.1之間的毫秒數(shù):var mills = new Date().getTime();時間戳轉為中國標準時間
const time = 1531065600000;//時間戳(數(shù)字) const youData = new Data(time);
- 中國標準時間轉為時間戳
Date.parse(Time)
3. Date類型
創(chuàng)建日期對象 let now = new Date();
在不給Date構造函數(shù)傳參數(shù)的情況下,創(chuà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日零點
2005年5月5日下午5點55分55秒(注意月份是0為起點的)
Date.now() 當前時間
Date.toLocaleString() && Date.toString()
4. 日期格式化
toDateString()
toTimeString()
toLocaleDateString()
toLocaleTimeString()
toUTCString()
5. 如何判斷是否為當天時間
if (new Date(str).toDateString() === new Date().toDateString()) { //今天 console.log("當天"); } else if (new Date(str) < new Date()){ //之前 console.log(new Date(str).toISOString().slice(0,10)); }
附獲取當前日期:
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
總結
到此這篇關于JavaScript中各種時間轉換問題的文章就介紹到這了,更多相關JS時間轉換問題內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
JS中Json數(shù)據(jù)的處理和解析JSON數(shù)據(jù)的方法詳解
JSON (JavaScript Object Notation)一種簡單的數(shù)據(jù)格式,比xml更輕巧,這篇文章主要介紹了JS中Json數(shù)據(jù)的處理和解析JSON數(shù)據(jù)的方法詳解的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-06-06uniapp微信小程序無法獲取Vue.prototype值的解決方法
在uniapp開發(fā)過程中,各端的一些高度會有區(qū)別,為了方便就要統(tǒng)一放到全局變量中,下面這篇文章主要給大家介紹了關于uniapp微信小程序無法獲取Vue.prototype值的解決方法,需要的朋友可以參考下2022-10-10動態(tài)創(chuàng)建的表格單元格中的事件實現(xiàn)代碼
好久沒有搞網頁了,今天重新弄了一個 ,做個動態(tài)表格,具體的實現(xiàn)代碼,大家可以自己寫吧2008-12-12JS在Chrome瀏覽器中showModalDialog函數(shù)返回值為undefined的解決方法
這篇文章主要介紹了JS在Chrome瀏覽器中showModalDialog函數(shù)返回值為undefined的解決方法,涉及javascript針對谷歌瀏覽器事件判定相關操作技巧,需要的朋友可以參考下2016-08-08淺析JavaScript定時器setTimeout的時延問題
這篇文章主要為大家詳細介紹了JavaScript中定時器setTimeout有最小時延的相關知識,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起學習一下2023-11-11重置Redux的狀態(tài)數(shù)據(jù)的方法實現(xiàn)
這篇文章主要介紹了重置Redux的狀態(tài)數(shù)據(jù)的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11