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

如何在Day.js中获取版本号?从Moment.js迁移后的技术疑问

解决Day.js ES模块版本获取version的问题

核心Day.js版本获取

Day.js的ES模块构建(比如你使用的2.0.0-alpha.4)和Moment.js的设计逻辑不同,核心版本号并非挂载在默认导出的dayjs对象上,而是作为独立的导出成员存在。你需要单独导入version:

修改你的代码如下:

<div id="moh">waiting</div>
<script type="module">
import dayjs, { version as dayjsVersion } from "https://cdn.jsdelivr.net/npm/dayjs@2.0.0-alpha.4/dist/esm/index.mjs";
document.getElementById("moh").textContent = dayjsVersion ?? "undefined";
</script>

Timezone插件版本获取

同理,tz插件的版本号也是独立导出的,需要从插件模块中单独导入:

import dayjs from "https://cdn.jsdelivr.net/npm/dayjs@2.0.0-alpha.4/dist/esm/index.mjs";
import tz, { version as tzVersion } from "https://cdn.jsdelivr.net/npm/dayjs@2.0.0-alpha.4/dist/esm/plugin/timezone.mjs";

dayjs.extend(tz);
console.log('Day.js版本:', dayjsVersion);
console.log('TZ插件版本:', tzVersion);

原因说明

Day.js的ES模块遵循ES6模块导出规范,将版本号作为独立导出项,而非附加在主函数对象上。这和Moment.js把版本挂载在全局moment对象上的实现逻辑不同,属于两者设计层面的差异。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 14:46:01