You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Luxon DateTime对象中获取对应时间戳?

从Luxon DateTime对象获取毫秒级时间戳的方法

你可以直接调用Luxon DateTime实例的toMillis()方法,返回结果和原生Date.getTime()完全一致,都是13位毫秒级时间戳。

注意:Luxon的所有实例操作方法都是不可变的,你示例中直接调用date.endOf('day')不会修改原date对象,需要用变量接收返回的新实例才能得到预期的当天结束时间对应的时间戳。

修正后的完整可运行代码如下:

const { DateTime } = require("luxon");

let date = DateTime.now().setZone('utc');
// 接收endOf方法返回的新DateTime实例
date = date.endOf('day');

// 输出毫秒级时间戳
console.log(date.toMillis()) 

如果你更习惯用原生Date的API,也可以先转成原生Date对象再调用getTime(),效果和直接调用toMillis()完全一致:

console.log(date.toJSDate().getTime())

内容的提问来源于stack exchange,提问作者Andreas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 13:24:03