JS中國標準時間轉化為年月日時分秒'yyyy-MM-dd hh:mm:ss'的示例詳解
JS中國標準時間轉化為年月日時分秒‘yyyy-MM-dd hh:mm:ss‘
新建一個formatDate.js文件,如下:
function padLeftZero(str) { return ('00' + str).substr(str.length) } export function formatDate(date, fmt) { if (!date) { return '' } if (/(y+)/.test(fmt)) { fmt = fmt.replace( RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length) ) } const o = { 'M+': date.getMonth() + 1, 'd+': date.getDate(), 'h+': date.getHours(), 'm+': date.getMinutes(), 's+': date.getSeconds() } for (const k in o) { if (new RegExp(`(${k})`).test(fmt)) { const str = o[k] + '' fmt = fmt.replace( RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str) ) } } return fmt } export default formatDate
如果是在vue項目中運用,在main.js中全局引用
import formatDate from '@/utils/formatDate.js' Vue.prototype.$formatDate = formatDate
再到頁面上直接調用$formatDate方法
// 獲取當前時間年月日時分秒,即 2022-10-14 16:11:58 this.$formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss') // 如果時分秒不需要精確到,即 2022-10-14 00:00:00 this.$formatDate(new Date(), 'yyyy-MM-dd 00:00:00') // 意思就是說時分秒你需要什么時間就寫什么時間;需要是下午4點11分58秒,即 2022-10-14 16:11:58 this.$formatDate(new Date(), 'yyyy-MM-dd 16:11:58')
補充:
Js各種時間轉換問題(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; }
2.yyyy-mm-dd或yyyy-MM-dd HH-mm-ss 轉為時間戳
var stringTime = '2012-10-12 22:37:33'; //將獲取到的時間轉換成時間戳 var timestamp = Date.parse(new Date(stringTime));
3.中國標準時間轉為 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();
5.時間戳轉為中國標準時間
const time = 1531065600000;//時間戳(數(shù)字) const youData = new Data(time);
6.中國標準時間轉為時間戳
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)); }
到此這篇關于JS中國標準時間轉化為年月日時分秒‘yyyy-MM-dd hh:mm:ss‘的文章就介紹到這了,更多相關js中國標準時間轉換年月日時分秒內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
js 動態(tài)生成html 觸發(fā)事件傳參字符轉義的實例
下面小編就為大家?guī)硪黄猨s 動態(tài)生成html 觸發(fā)事件傳參字符轉義的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02