## 介紹
在編寫(xiě)JavaScript代碼時(shí),我們經(jīng)常需要獲取當(dāng)前日期和時(shí)間。在本文中,我們將介紹幾種獲取當(dāng)前時(shí)間并將其格式化為 yyyymmddhhmmss
的字符串的方法。
方法一:使用Date對(duì)象
在JavaScript中,我們可以使用 Date
對(duì)象來(lái)獲取當(dāng)前日期和時(shí)間。下面是一個(gè)示例代碼:
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ù)來(lái)獲取年、月、日、小時(shí)、分鐘和秒。然后,我們使用 slice
函數(shù)將所有這些值轉(zhuǎn)換為兩位數(shù)字并將它們連接到一個(gè)字符串中。
方法二:使用moment.js
Moment.js是一個(gè)流行的JavaScript日期庫(kù),它提供了許多日期和時(shí)間操作方法。下面是一個(gè)示例代碼:
const moment = require('moment');
const formattedTime = moment().format('YYYYMMDDHHmmss');
在上面的代碼中,我們使用moment.js庫(kù)的format
函數(shù)將當(dāng)前時(shí)間格式化為 yyyymmddhhmmss
的字符串。
方法三:使用Intl.DateTimeFormat
Intl.DateTimeFormat是一個(gè)內(nèi)置的JavaScript日期庫(kù),它提供了本地化和格式化日期的方法。下面是一個(gè)示例代碼:
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來(lái)格式化當(dāng)前時(shí)間,并使用正則表達(dá)式將所有非數(shù)字字符替換為空字符串,以生成 yyyymmddhhmmss
的字符串。
方法四:使用day.js
day.js是一個(gè)輕量級(jí)的JavaScript日期庫(kù),它提供了許多日期和時(shí)間操作方法。下面是一個(gè)示例代碼:
const dayjs = require('dayjs');
const formattedTime = dayjs().format('YYYYMMDDHHmmss');
在上面的代碼中,我們使用day.js庫(kù)的format
函數(shù)將當(dāng)前時(shí)間格式化為 yyyymmddhhmmss
的字符串。
方法五:使用toLocaleString
在JavaScript中,我們可以使用 toLocaleString
函數(shù)來(lái)獲取本地化的日期和時(shí)間。下面是一個(gè)示例代碼:
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ù)獲取本地化的日期和時(shí)間,并使用正則表達(dá)式將所有非數(shù)字字符替換為空字符串,以生成 yyyymmddhhmmss
的字符串。
方法六:使用String.prototype.padStart
在JavaScript中,我們可以使用 padStart
函數(shù)來(lái)將數(shù)字字符串填充到指定的長(zhǎng)度。下面是一個(gè)示例代碼:
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ù)字字符串填充到兩位,并將它們連接到一個(gè)字符串中,以生成 yyyymmddhhmmss
的字符串。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-665870.html
結(jié)論
現(xiàn)在你已經(jīng)知道了六種在JavaScript中獲取當(dāng)前時(shí)間并將其格式化為 yyyymmddhhmmss
的字符串的方法。選擇適合你的代碼項(xiàng)目的最佳方法,并開(kāi)始編寫(xiě)更好的JavaScript代碼吧!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-665870.html
到了這里,關(guān)于如何在JavaScript中獲取當(dāng)前時(shí)間yyyymmddhhmmss? (六種實(shí)現(xiàn)方式)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!