【发布时间】:2022-08-03 23:48:42
【问题描述】:
我需要计算小数天:自午夜以来经过的一天的小数部分。我还需要任意时间的年月。这些需要在 UTC 时间的上下文中。我的应用程序使用 Luxon,因此我使用以下方法来计算它们,从 DateTime.now() 开始作为任意示例:
const luxonNow = DateTime.now();
const gt = luxonNow.setZone(\'utc\');
const luxonY = gt.year;
const luxonM = gt.month;
const luxonMidnight = gt.startOf(\'day\');
// Create an Interval beginning at midnight, ending now
// Find the decimal hours that have passed. Divide by 24 to find the fractional day passed
const luxonFrac = Interval.fromDateTimes(luxonMidnight, gt).length(\'hours\') / 24;
luxonT = gt.day + luxonFrac;
这是在性能很重要的代码领域。代码速度很快:基准测试表明它最多需要 0.3 毫秒,平均 0.1 毫秒。
我可以让它更快吗?
标签: luxon