skip to Main Content

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());
}

2

Answers


  1. 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:

    Login or Signup to reply.
  2. 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
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search