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
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.From your provided document of "IsSameOrBefore", the sample script is as follows.
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:
By this,
true
is showing in the log.As another plugin, when "IsToday" is used, the sample script is as follows.
Note:
References:
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:
npm i dayjs