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

如何在Google Apps Script中导入并使用dayjs的IsSameOrBefore插件?

在Google Apps Script中使用Dayjs的IsSameOrBefore插件

正确的导入与使用方法

你使用的Daygs是适配Google Apps Script的封装版Dayjs,插件的获取逻辑和原生Dayjs不同,正确操作步骤如下:

  1. 直接从Daygs.plugin对象中提取目标插件,再通过dayjs.extend()加载:
const { dayjs } = Daygs;
// 从封装库的plugin对象中获取isSameOrBefore插件
const isSameOrBefore = Daygs.plugin.isSameOrBefore;

function myFunction() {
  // 加载插件
  dayjs.extend(isSameOrBefore);
  
  // 测试插件功能
  const targetDate = dayjs('2024-05-01');
  Logger.log(targetDate.isSameOrBefore(dayjs('2024-05-02'))); // 输出 true
  Logger.log(targetDate.isSameOrBefore(dayjs('2024-04-30'))); // 输出 false
}

如果上述方式无效,可尝试直接传入插件名称字符串加载(部分封装库支持此简化方式):

const { dayjs } = Daygs;

function myFunction() {
  dayjs.extend('isSameOrBefore');
  
  Logger.log(dayjs('2024-05-01').isSameOrBefore(dayjs()));
}

替代日期库推荐

如果Daygs的插件使用体验不佳,推荐以下适配Google Apps Script的方案:

  • Moment.js:GAS原生支持,无需额外添加库,语法成熟、文档齐全,适合快速开发(注意:Moment已进入维护模式,不再新增功能)
  • Luxon:Moment团队推出的继任者,支持时区处理、ISO标准解析,功能更现代,可通过脚本库添加到GAS中使用
  • 原生Date对象:若仅需基础日期处理(如格式化、简单比较),直接使用GAS原生Date对象即可,无第三方依赖,性能更优

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 21:04:58