dayjs is not a valid operation

I've assigned two dayjs date values to global variables Vars.date.

var dayjs = require("dayjs") for import
   
Vars.date1 = dayjs(whateverdate1("add", 2)).format('MMM D, YYYY')
Vars.date2 = dayjs(whateverdate2("add", 2)).format('MMM D, YYYY')

const date1 = Vars.date1
const date2 = Vars.date2
let diff = date1.diff(date2, 'month')
console.log('diff' + diff)

Error: TypeError: date1.diff is not a function

I seem to be unable to access the diff and get methods, always showing 'not a function'

What have I overlooked?

Answer №1

Ensure that Day.js is compatible with the require() function. If it uses import, you may need to adjust the import statement accordingly.

To do this, simply try the following:

import dayjs from "dayjs";

Helpful Links

Answer №2

It seems like there were a couple of issues in your code:

  1. You should use dayjs().add(2, 'month') to call the add() method
  2. Instead of calling diff() on a string (the result of dayjs().format()), it should be called on a Dayjs object

Here is an example of how you can create two separate dayjs objects and calculate their month difference:

const dayjs = require("dayjs")
// or:
// import dayjs from "dayjs";

const firstDate = dayjs()   // Dayjs object representing current date
const secondDate = dayjs().add(2, "month") // Dayjs object representing date 2 months from now

secondDate.diff(firstDate, "month") // Result will be 2

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Experience the new Bootstrap 5 feature: Off-Canvas sliding from left to

I encountered the code snippet below on the official bootstrap 5 demo. Despite my efforts, I am struggling to figure out how to relocate the off-canvas menu from Left-to-Right. The divergence between the documentation code referencing offcanvas-start and t ...

The link.url in my code is consistently changing, while the pathName remains static

My code is experiencing an issue where link.url is changing but pathName is not. I am trying to implement an animation transition on my website, but it is not working correctly because the pathName and link.url do not match. "use client" import ...

What is the method for reaching a service in a different feature module?

Currently, I am utilizing Angular 2/4 and have organized my code into feature modules. For instance, I have a Building Module and a Client Module. https://i.stack.imgur.com/LvmkU.png The same structure applies to my Client Feature Module as well. Now, i ...

Tips on retrieving specific information from PHP through jQuery AJAX

I have encountered an issue with my JavaScript file where I am sending an array of data to my PHP file. The problem is, when trying to print the name in #NAME and password in #PASSWORD, both values end up in both fields. You can see how it currently displa ...

Updating a d3.js force-directed graph may retain previous JSON data during the reloading process

Having a d3.js force-directed graph that pulls data from a JSON feed, I encounter an issue when clicking on a node. Although the updated JSON is correct, the displayed graph does not reflect this new data. It seems like the graph is retaining previous info ...

Tips for isolating all the Javascript loaded via ajax or jquery.load within a specific child scope instead of a global scope

I am currently working on a solution to embed a third-party page into my site using ajax/jquery.load() to avoid using iframes (CORS requirements are already handled by the third party). My dilemma lies in the fact that the main host site loads jquery 1.x ...

Dealing with cascading menu in knockout viewmodel

I have incorporated knockoutjs into my current project. One particular requirement I encountered was the need to create a nested menu within my viewmodel. Here is how I implemented it: self.menu = [ { name: 'Services', su ...

Troubleshooting a misformatted JSON string that lacks proper double quotes in Java Script

{ DataError: { user_id: [ [Object] ] } } I want to transform this string into JSON structure like below: { "DataError": { "user_id": [ [Object] ] } } Is there a potential method to achieve this outcome from incorrectly formatted JSON string? ...

What is the best way to show the probability of users' bets in percentage form based on their wagered amounts?

I am currently working on creating a Jackpot Roulette game that features a main pot. Each round sees users joining and placing bets that contribute to the main pot, with the winner taking home the entire amount. My goal is to provide each user with real-t ...

The styling of the CSS is tailored to various breakpoints

source: Display helpers How can I dynamically change CSS styles based on the current breakpoint size? For example, can I set different sizes, positions, and colors for elements when the window is on xs compared to md or other breakpoints? ...

Modify an element upon clicking the mouse on an image

I am looking to dynamically change the paragraph element with className="details" to an editable input field when a user clicks on the image with className="edit-icon" within the same grid container. How can I achieve this functionality ...

What is the best method for creating a loop script within a widget using script?

I am trying to implement a loop within a widget, however, the widget contains an internal script: <!DOCTYPE html> <html><head></head><body> <script> list=["AMAR3","BBDC4", "BEEF3", "BPAN4", "BRFS3", "B3SA3", "CVCB3" ...

My website doesn't seem to support Javascript, although it works perfectly on jsfiddle

I was tinkering around on jsfiddle and managed to get my code working flawlessly. However, when I tried to copy and paste it elsewhere, the functionality broke down. It seems that for some inexplicable reason, the code doesn't seem to pass through the ...

The poster image functionality in videos is experiencing issues after updating to version 1.5.0 of the Azure Media Player

I've encountered a strange issue after updating my azure media player to version 1.5.0 - it's not displaying the poster image like it did in version 1.3.0. Below is the code I'm currently using: <div class="marginBlock" id="mediaPlayer" ...

Link Array with Google Charts

I've been struggling for a long time to connect this array to a Google chart with no success. I would really appreciate some assistance in identifying what mistake I've made. I have created a jsfiddle where the array appears to be correct, and wh ...

Struggling with resolving the error message "EEXIST: file already exists, mkdir 'C:UsersOKRDesktopMeetUp' after the apk generation failed in React Native? Learn how to troubleshoot this

Currently, I am in the process of testing my React Native apk app file. These are the steps I followed before generating the apk: react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.a ...

Even though my performance in a sandbox environment is excellent, I am unable to obtain a token in a production environment

After posting my question on the Evernote developer forum, I was disappointed to find that the forum had been closed before I received a response. Even after receiving a proposal from an Evernote employee named chanatx to verify if my key was activated co ...

React component making an Axios request only receives the initial state as a response

I'm struggling with creating an AJAX call using Axios in React. Despite my efforts, I can't seem to pinpoint where the issue lies. Below is what I currently have within my component: ComponentDidMount() { axios.get('https://jsonplacehol ...

Issue with Three JS canvas not adjusting to fit mobile device window width

I'm currently exploring the world of 3D animations using Three JS and I've set out to create a project involving multiple spinning cubes. However, I've encountered an issue where the canvas doesn't resize correctly when zooming in or ou ...

Is there a way to extract the data types of my model's properties from the Prisma Schema?

If we consider the following Prisma Schema example: model User { id String @id @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid email String @unique username String } Is there a way to extract t ...