javascript - 如何在浏览器中加载day.js插件

标签 javascript dayjs

我正在尝试在客户端加载两个 day.js 插件。但它似乎不起作用......我做错了什么?

哈巴狗/Jade 文件

script(defer, src='javascript/external/day.js')
script(defer, src='javascript/external/day_minmax.js')
script(defer, src='javascript/external/day_isbetween.js')

script(defer, dayjs.extend(window.dayjs_plugin_minmax))
script(defer, dayjs.extend(window.dayjs_plugin_isbetween))

控制台输出

dayjs.max()
Uncaught TypeError: dayjs.max is not a function

加载的js插件文件来自:

https://unpkg.com/[email protected]/plugin/isBetween.js

https://unpkg.com/[email protected]/plugin/minMax.js

最佳答案

这是一个在index.html 文件中的工作示例,您可以在根Pug/Jade 文件中执行相同的操作。另外,我使用的是 cdn 版本,但您也可以从下载它们的文件夹中导入它们。

window.dayjs_plugin_minmaxwindow.dayjs_plugin_minMax 可能是问题

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>Test page</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <script src="https://unpkg.com/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a7c3c6decdd4e796899f899596" rel="noreferrer noopener nofollow">[email protected]</a>/dayjs.min.js"></script>
    <script src="https://unpkg.com/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5733362e3d241766796f796566" rel="noreferrer noopener nofollow">[email protected]</a>/plugin/isBetween.js"></script>
    <script src="https://unpkg.com/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="187c7961726b58293620362a29" rel="noreferrer noopener nofollow">[email protected]</a>/plugin/minMax.js"></script>
  </head>
  <body></body>
  <script>
    const minMax = window.dayjs_plugin_minMax;
    const isBetween = window.dayjs_plugin_isBetween;
    dayjs.extend(minMax);
    dayjs.extend(isBetween);
    console.log(
      "MAX: ",
      dayjs.max(dayjs(), dayjs("2018-01-01"), dayjs("2019-01-01"))
    );
    console.log(
      "MIN: ",
      dayjs.min(dayjs(), dayjs("2018-01-01"), dayjs("2019-01-01"))
    );
    console.log(
      "BETWEEN: ",
      dayjs("2016-10-30").isBetween("2016-01-01", "2016-10-30", null, "[)")
    );
  </script>
</html>

这就是结果:

Result image

关于javascript - 如何在浏览器中加载day.js插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64319169/

相关文章:

javascript - 如何使用访问 token 获取电子邮件地址和 Facebook 用户的一些基本信息

vite - 如何在 Vite for Vue 或 React 中使用 dayjs?

javascript - 简化 JavaScript

javascript - Dayjs:无法将自定义 24 小时时间格式化为 12 小时

javascript - unwrap() 删除 jsFiddle 测试中的链接,但不删除 Tampermonkey 脚本中的链接?

javascript - 将 Canvas 对象另存为图像 - 黑色背景

javascript - 如何防止node.js中的多个并发连接覆盖全局变量值?

javascript - Knockout - 如何从一组复选框绑定(bind)外部容器 CSS?

javascript - 模拟日扩展