Google Apps Script dayjs Plugin Import

I am trying to use the dayjs library in my Google apps script and some of its plugins.

I first added the library to my project by searching for the following script ID:

1ShsRhHc8tgPy5wGOzUvgEhOedJUQD53m-gd8lG2MOgs-dXC_aCZn9lFB

Then I used it in my function like so:

const { dayjs } = Daygs;

function myFunction() {
  Logger.log(dayjs());
}

This works perfectly fine but I want to use a plugin library, like IsSameOrBefore, but I’m not sure how exactly I’m supposed to import and use it.

Any idea how to import this plugin and get this to work? Also open to suggestions on better date libraries to use.

I’ve tried all the following and nothing works:

const { dayjs } = Daygs;
// const { isSameOrBefore } = Daygs.plugin.isSameOrBefore;
// const { isSameOrBefore } = 'dayjs/plugin/isSameOrBefore';
// const isSameOrBefore = require("dayjs/plugin/isSameOrBefore");
// const isSameOrBefore = Daygs.plugin.isSameOrBefore;
// const isSameOrBefore = dayjs.plugin.isSameOrBefore;
// const { isSameOrBefore } = dayjs.plugin.isSameOrBefore;
// import { isSameOrBefore } from "dayjs/plugin/isSameOrBefore";

function myFunction() {
  dayjs.extend(isSameOrBefore);
  Logger.log(dayjs());
}

Although I’m not sure whether I could correctly understand your expected result, when I saw the repository of “dayjs”, I noticed that dayjs/src/plugin/isSameOrBefore/index.js. Ref In the case of “isSameOrBefore”, it seems that the script is as follows.

export default (o, c) => {
  c.prototype.isSameOrBefore = function (that, units) {
    return this.isSame(that, units) || this.isBefore(that, units)
  }
}

From your provided document of “IsSameOrBefore”, the sample script is as follows.

var isSameOrBefore = require("dayjs/plugin/isSameOrBefore");
// import isSameOrBefore from 'dayjs/plugin/isSameOrBefore' // ES 2015

dayjs.extend(isSameOrBefore);

dayjs("2010-10-20").isSameOrBefore("2010-10-19", "year");

When these are reflected in Google Apps Script under your showing situation, how about the following modification?

In this answer, it supposes that the library of Daygs you show has already been installed. Please be careful about this.

Modified script:

const { dayjs } = Daygs;

function myFunction() {

  // This is from https://github.com/iamkun/dayjs/blob/dev/src/plugin/isSameOrBefore/index.js
  const isSameOrBefore = (o, c) => {
    c.prototype.isSameOrBefore = function (that, units) {
      return this.isSame(that, units) || this.isBefore(that, units)
    }
  }

  dayjs.extend(isSameOrBefore);
  const res = dayjs("2010-10-20").isSameOrBefore("2010-10-19", "year");
  console.log(res); // true
}

By this, true is showing in the log.

As another plugin, when “IsToday” is used, the sample script is as follows.

const { dayjs } = Daygs;

function myFunction() {

  // This is from https://github.com/iamkun/dayjs/blob/dev/src/plugin/isToday/index.js
  const isToday = (o, c, d) => {
    const proto = c.prototype
    proto.isToday = function () {
      const comparisonTemplate = 'YYYY-MM-DD'
      const now = d()

      return this.format(comparisonTemplate) === now.format(comparisonTemplate)
    }
  }

  dayjs.extend(isToday);
  const res = dayjs().isToday();
  console.log(res); // true
}

Note:

  • This modified script is for “IsSameOrBefore”. Unfortunately, I cannot test all plugins. So, I’m not sure whether all plugins can be used with this method. Please be careful about this.

References:

  • dayjs at GitHub
  • IsSameOrBefore

If you are up for it, feel free to bundle the library from NPM. You can check out my article on this topic.

Here’s the gist:

  1. Work locally and make sure you have clasp installed
  2. Install and configure Vite as per the article
  3. Install the simple Vite plugin from the article
  4. Install DayJS from npm with npm i dayjs
  5. Import DayJS in your code with ES6 syntax as you would in normal JavaScript
  6. Build your code with Vite and push it with clasp

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật