通过使用默认的Date()
const now = new Date()
console.log(now)
对于不同的日期和时间格式,您可以使用toLocaleString()
:
短日期格式:
MM/DD/YYYY
const now = new Date();
const formattedDate = now.toLocaleString('en-US', { dateStyle: 'short' });
console.log(formattedDate);
长日期格式:
Weekday, Month Day, Year
const now = new Date();
const formattedDate = now.toLocaleString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' });
console.log(formattedDate);
短时间格式:
HH:MM AM/PM
const now = new Date();
const formattedDate = now.toLocaleString('en-US', { timeStyle: 'short' });
console.log(formattedDate);
长时间格式:
HH:MM:SS AM/PM Timezone
const now = new Date();
const formattedDate = now.toLocaleString('en-US', { hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: true, timeZoneName: 'short' });
console.log(formattedDate);
ISO 8601 格式:
YYYY-MM-DDTHH:MM:SSZ
const now = new Date();
const isoDate = now.toISOString();
console.log(isoDate);