JavaScript中獲取當(dāng)前時間yyyymmddhhmmss的六種實(shí)現(xiàn)方式
介紹
在編寫JavaScript代碼時,我們經(jīng)常需要獲取當(dāng)前日期和時間。在本文中,我們將介紹幾種獲取當(dāng)前時間并將其格式化為 yyyymmddhhmmss 的字符串的方法。
方法一:使用Date對象
在JavaScript中,我們可以使用 Date 對象來獲取當(dāng)前日期和時間。下面是一個示例代碼:
const now = new Date();
const year = now.getFullYear();
const month = ('0' + (now.getMonth() + 1)).slice(-2);
const day = ('0' + now.getDate()).slice(-2);
const hours = ('0' + now.getHours()).slice(-2);
const minutes = ('0' + now.getMinutes()).slice(-2);
const seconds = ('0' + now.getSeconds()).slice(-2);
const formattedTime = year + month + day + hours + minutes + seconds;在上面的代碼中,我們使用 getFullYear、getMonth、getDate、getHours、getMinutes 和 getSeconds 函數(shù)來獲取年、月、日、小時、分鐘和秒。然后,我們使用 slice 函數(shù)將所有這些值轉(zhuǎn)換為兩位數(shù)字并將它們連接到一個字符串中。
方法二:使用moment.js
Moment.js是一個流行的JavaScript日期庫,它提供了許多日期和時間操作方法。下面是一個示例代碼:
const moment = require('moment');
const formattedTime = moment().format('YYYYMMDDHHmmss');在上面的代碼中,我們使用moment.js庫的format函數(shù)將當(dāng)前時間格式化為 yyyymmddhhmmss 的字符串。
方法三:使用Intl.DateTimeFormat
Intl.DateTimeFormat是一個內(nèi)置的JavaScript日期庫,它提供了本地化和格式化日期的方法。下面是一個示例代碼:
const options = { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false };
const formattedTime = new Intl.DateTimeFormat('en-US', options).format(new Date()).replace(/[^0-9]/g, '');在上面的代碼中,我們使用Intl.DateTimeFormat來格式化當(dāng)前時間,并使用正則表達(dá)式將所有非數(shù)字字符替換為空字符串,以生成 yyyymmddhhmmss 的字符串。
方法四:使用day.js
day.js是一個輕量級的JavaScript日期庫,它提供了許多日期和時間操作方法。下面是一個示例代碼:
const dayjs = require('dayjs');
const formattedTime = dayjs().format('YYYYMMDDHHmmss');在上面的代碼中,我們使用day.js庫的format函數(shù)將當(dāng)前時間格式化為 yyyymmddhhmmss 的字符串。
方法五:使用toLocaleString
在JavaScript中,我們可以使用 toLocaleString 函數(shù)來獲取本地化的日期和時間。下面是一個示例代碼:
const now = new Date();
const formattedTime = now.toLocaleString('en-US', {year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit'}).replace(/[^\\d]/g, '');在上面的代碼中,我們使用 toLocaleString 函數(shù)獲取本地化的日期和時間,并使用正則表達(dá)式將所有非數(shù)字字符替換為空字符串,以生成 yyyymmddhhmmss 的字符串。
方法六:使用String.prototype.padStart
在JavaScript中,我們可以使用 padStart 函數(shù)來將數(shù)字字符串填充到指定的長度。下面是一個示例代碼:
const now = new Date(); const year = now.getFullYear(); const month = String(now.getMonth() + 1).padStart(2, '0'); const day = String(now.getDate()).padStart(2, '0'); const hours = String(now.getHours()).padStart(2, '0'); const minutes = String(now.getMinutes()).padStart(2, '0'); const seconds = String(now.getSeconds()).padStart(2, '0'); const formattedTime = year + month + day + hours + minutes + seconds;
在上面的代碼中,我們使用 padStart 函數(shù)將所有數(shù)字字符串填充到兩位,并將它們連接到一個字符串中,以生成 yyyymmddhhmmss 的字符串。
結(jié)論
現(xiàn)在你已經(jīng)知道了六種在JavaScript中獲取當(dāng)前時間并將其格式化為 yyyymmddhhmmss 的字符串的方法。選擇適合你的代碼項(xiàng)目的最佳方法,并開始編寫更好的JavaScript代碼吧!
到此這篇關(guān)于JavaScript中獲取當(dāng)前時間yyyymmddhhmmss的六種實(shí)現(xiàn)方式的文章就介紹到這了,更多相關(guān)JS獲取當(dāng)前時間yyyymmddhhmmss內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
11個Javascript小技巧幫你提升代碼質(zhì)量(小結(jié))
這篇文章主要介紹了11個Javascript小技巧幫你提升代碼質(zhì)量(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
一文徹底理解js原生語法prototype,__proto__和constructor
作為一名前端工程師,必須搞懂JS中的prototype、__proto__與constructor屬性,相信很多初學(xué)者對這些屬性存在許多困惑,容易把它們混淆,下面這篇文章主要給大家介紹了關(guān)于js原生語法prototype,__proto__和constructor的相關(guān)資料,需要的朋友可以參考下2021-10-10
javascript實(shí)現(xiàn)點(diǎn)擊提交按鈕后顯示loading的方法
這篇文章主要介紹了javascript實(shí)現(xiàn)點(diǎn)擊提交按鈕后顯示loading的方法,涉及javascript動態(tài)設(shè)置頁面元素樣式的相關(guān)技巧,需要的朋友可以參考下2015-07-07

