Javascript 将 BigInt 转换为日期时间和格式

Javascript Convert BigInt to Datetime and Format

我有一个方法 returns 时间戳是 bigint。

内置的日期方法只接受数字来创建日期时间。是否有其他函数或方法可以接受 bigint 并将其转换为日期。

本质上类似于 Java 的 DateTimeFormatter class。

此外,如果可能,是否有一种方法可以将日期格式化为格式为“yyyyMMdd”的字符串,寻找类似于 Java 的

DateTimeFormat.forPattern

时间戳是自纪元时间以来的毫秒数

let time = 1630017759934 

使用该时间戳,您可以只使用常规数字,它的大小为 15,而对于毫秒间隔,您只能使用大小 13。

下面是一些可能适合您的示例代码:

const time = 1630017759934;
const timeInSeconds = Math.floor(time/1000);
const d = new date(timeInSeconds);
const dateFormatted = `${date.getFullYear()}${(date.getMonth() + 1).toString().padStart(2, '0')}${date.getDate().toString().padStart(2, '0')}`;