欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS獲取當(dāng)前日期 YYYY-MM-DD hh-mm-ss的示例代碼

 更新時間:2024年02月03日 14:45:24   作者:花樹最愛貓  
在編寫JavaScript代碼時,遇到需要獲取當(dāng)前日期和時間并將其格式化為 yyyymmddhhmmss 字符串的情況,可以使用本文中介紹的幾種實(shí)現(xiàn)方式中的任意一種,

JS獲取當(dāng)前日期 YYYY-MM-DD hh-mm-ss

//獲取當(dāng)前時間(中國標(biāo)準(zhǔn)時間)
var myDate = new Date();
//日期時間截取
myDate.getYear();        //獲取當(dāng)前年份(2位)
myDate.getFullYear();    //獲取完整的年份(4位)
myDate.getMonth();       //獲取當(dāng)前月份()
myDate.getDate();        //獲取當(dāng)前日()
myDate.getDay();         //獲取當(dāng)前星期X()
myDate.getTime();        //獲取當(dāng)前時間()
myDate.getHours();       //獲取當(dāng)前小時數(shù)()
myDate.getMinutes();     //獲取當(dāng)前分鐘數(shù)()
myDate.getSeconds();     //獲取當(dāng)前秒數(shù)()
myDate.getMilliseconds();    //獲取當(dāng)前毫秒數(shù)(0-999)
myDate.toLocaleDateString();     //獲取當(dāng)前日期
myDate.toLocaleString( );        //獲取日期與時間
myDate.toLocaleTimeString();     //獲取當(dāng)前時間
myDate.toDateString ()           //轉(zhuǎn)字符串
myDate.getTime()                 //轉(zhuǎn)時間戳

補(bǔ)充:

在JavaScript中獲取當(dāng)前時間yyyymmddhhmmss的六種實(shí)現(xiàn)方式

在編寫JavaScript代碼時,遇到需要獲取當(dāng)前日期和時間并將其格式化為 yyyymmddhhmmss 字符串的情況??梢允褂帽疚闹薪榻B的幾種實(shí)現(xiàn)方式中的任意一種。

方法一:使用Date對象

使用 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;
console.log(formattedTime);

結(jié)果截圖:

代碼說明:

在上面的代碼中,使用了 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');
console.log(formattedTime);

結(jié)果截圖:

無,筆者使用時沒有運(yùn)行成功

代碼說明:

在上面的代碼中,使用了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, '');
console.log(formattedTime);

結(jié)果截圖:

代碼說明:

在上面的代碼中,使用了Intl.DateTimeFormat來格式化當(dāng)前時間,并使用正則表達(dá)式將所有非數(shù)字字符替換為空字符串,以生成 yyyymmddhhmmss 的字符串。

方法四:使用day.js

day.js是一個輕量級的JavaScript日期庫,它提供了許多日期和時間操作方法。

示例代碼:

const dayjs = require('dayjs');
const formattedTime = dayjs().format('YYYYMMDDHHmmss');
console.log(formattedTime);

結(jié)果截圖:

無,筆者使用時沒有運(yùn)行成功

代碼說明:

在上面的代碼中,使用了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, '');
console.log(formattedTime);

結(jié)果截圖:

代碼說明:

在上面的代碼中,使用了 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;
console.log(formattedTime);

結(jié)果截圖:

代碼說明:

在上面的代碼中,使用 padStart 函數(shù)將所有數(shù)字字符串填充到兩位,并將它們連接到一個字符串中,以生成 yyyymmddhhmmss 的字符串。

到此這篇關(guān)于JS獲取當(dāng)前日期 YYYY-MM-DD hh-mm-ss的文章就介紹到這了,更多相關(guān)js獲取當(dāng)前日期內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論