Momentjs initially indicates that the date is valid, logs the correct date, and displays the accurate duration using .fromNow(). However, this suddenly switches

I am currently working on converting the createdAt date in my Nuxtjs application that is fetched from MongoDB using an express app, with the help of moment.js. Initially, when I check if the date is valid, it shows as valid but then it switches to an incorrect fromNow format and turns into an invalid date...

<li class="bookchapterlists__list--items" v-for="(chapter, index) in chap" :key="index">
    <nuxt-link
      v-if="chapter.index === 0"
      class="bookchapterlists__list--items__links"
    >Prologue {{chapter.index}}: {{chapter.title}}</nuxt-link>

    <nuxt-link
      v-else
      class="bookchapterlists__list--items__links"
      :to="{path: `${ $route.params.id}/${chapter._id}`}"
    >
      <p
        class="bookchapterlists__list--items__links--title"
      >Chapter {{chapter.index}}: {{chapter.title}}</p>
      <p class="chapter-createdAt">{{chapter.createdAt}}</p>
    </nuxt-link>
  </li>




created() {
    this.$store.commit("book/FORMAT_DATE");
    this.chapters.forEach(chapter => {
      if (chapter.index % 2 === 1) {
        this.rowCount++;
      }
    });
    // console.log(this.rowCount);
  },
  computed: {
    chap: function() {
      return this.$store.state.book.book.chapters;
    }
  }

FORMAT_DATE(state) {
    state.book.chapters.forEach((chapter) => {
      let createdAt = new Date(chapter.createdAt);
      chapter.createdAt = moment(createdAt, 'YYYY-MM-DDYYYY-MM-DDTHH:mm:ss.SSSZ', true).fromNow();
      console.log(moment(createdAt, 'YYYY-MM-DDYYYY-MM-DDTHH:mm:ss.SSSZ').isValid())
      // console.log(chapter.createdAt);
    })
  }

The console log statement returns true.

Answer №1

After some exploration, I discovered the solution

<p>{{$moment(chapter.createdAt).fromNow()}}</p>

I recently added nuxt/moment and utilized it directly within the template!

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

Using v-model with the input type files in Vue.js allows for easy two

Is there a way to retrieve data from v-model array in an input type of "file" that allows multiple selections? <input type="file" multiple v-model="modFiles[index]"> <input type="file" multiple v-model="modFiles[index]"> <input type="file" ...

Creating Live Updates with Laravel Sanctum (SPA) - Implementing Private Channel Broadcasting in Real-Time Notifications

In my project, I am using Laravel -v7.x and Vue CLI in two separate folders for Frontend and Backend. I have successfully configured Laravel Sanctum (SPA) Authentication. Now, the challenge I am facing is creating real-time notifications when certain event ...

Required attributes not found for data type in TypeScript

When the following code snippet is executed: @Mutation remove_bought_products(productsToBeRemoved: Array<I.Product>) { const tmpProductsInVendingMachine: Array<I.Product> = Object.values(this.productsInVendingMachine); const reducedPro ...

Is there a way to prevent tinymce from automatically inserting <!DOCTYPE html><html><head></head><body> before all my content?

I have integrated TinyMCE as the editor for one of my database fields. The issue I am encountering is that when I input the text "abc" into the editor, it gets saved in the database surrounded by unnecessary HTML elements. This is the structure currently s ...

Utilizing the NODE_ENV variable in a Windows 10 npm script

I have integrated webpack into a Typescript project. Following a helpful tutorial, I created 3 separate webpack configuration files: webpack.common.js webpack.production.js webpack.development.js In the tutorial's package.json, the "scripts" sectio ...

Obtain information from local server in a React Native application

I am attempting to retrieve data from my localhost using React Native. My database, named "test," was created using MySQL. The table within the database is named "users" and contains the following rows: picture The connections are established in the ...

ExtJS web displays appear differently when viewed on Chrome's toggle device mode versus a mobile browser

Greetings, I have been working on developing a web application that can be accessed through a mobile browser. Initially, I created the web application and tested it in mobile mode using Chrome's simulation feature. https://i.sstatic.net/aAvVW.jpg H ...

Using Codeception's selenium module to wait for JavaScript and Ajax requests to

I am currently facing an issue where I need to wait for an ajax call to finish loading before moving on to the next step. I have tried using the waitForJS function, but I am struggling with building the JavaScript condition. I have experimented with diffe ...

Activating Ionic6 Stack Modal through JavaScript or TypeScript

Is it possible to trigger the modal using script code instead of a button? I have searched through various examples in the tutorial, but all of them rely on the modal trigger mechanism. <ion-button id="open-modal" expand="block">O ...

Troubleshooting problems with sending data in Jquery Ajax POST Request

I've spent a considerable amount of time searching for a solution to why my post request isn't sending its data to the server. Oddly enough, I can successfully send the request without any data and receive results from the server, but when attemp ...

Bug in toFixed causing incorrect results

function calculateTaxAndTotalRent(rent) { var phoneCharges = parseFloat($('#phone_charges').val()); phoneCharges = phoneCharges.toFixed(2); rent = parseFloat(rent); rent = rent.toFixed(2); var tax = parseFloat((rent * 15) / 1 ...

Combining the power of Vuforia with the capabilities of Three

I'm having difficulty with the integration of three.js and Vuforia, as I have limited knowledge about OpenGl which makes it challenging to troubleshoot. Our team is working on a small AR app where we aim to utilize Vuforia for tracking and recognitio ...

Display and conceal div with Jquery as you scroll to specific points on a mobile device

I'm looking to create a dynamic div that appears and disappears based on the user's scroll position. Here is what I have so far: $(document).scroll(function() { var y = $(this).scrollTop(); if (y > 200) { $('.float-contai ...

JavaScript and CSS tabs with smooth transition effect

I want to create tabs that smoothly fade between each other when switching. The code I have works, but there's a slight issue. When transitioning from one tab to the previous one, there is a momentary glitch where the last tab briefly changes state be ...

Simultaneous Loading: Marvelous Vue Preloader Spinner and Content Display (Vuejs)

Is there a way to ensure that the preloader loads before displaying the content? I tried using the library from , but both the preloader and content load simultaneously. How can I make sure that the preloader loads first, followed by the content? Thank you ...

Transform a nested array of objects into a distinct set of objects based on the data in JavaScript or TypeScript

I have a unique situation where I am dealing with a double nested array of objects, and I need to restructure it into a specific array format to better align with my table structure. Here are the current objects I'm working with and the desired resul ...

Angular JS has the capability to toggle the visibility of elements on a per-item basis as well as

I have created a repeater that displays a headline and description for each item. I implemented a checkbox to hide all descriptions at once, which worked perfectly. However, I also wanted to allow users to hide or show each description individually. I almo ...

Changing the key name for each element in an array using ng-repeat: a guide

In my current project, I have an array of objects that I am displaying in a table using the ng-repeat directive. <table> <thead> <tr> <th ng-repeat="col in columnHeaders">{{col}}</th> //['Name&apo ...

Issue with Pebble SDK/SimplyJS failing to recognize the tab character

Currently, I am facing an interesting challenge while attempting to make my Pebble watch recognize the escape sequence character \t when sending data to my watch using SimplyJS. Here is the code snippet I have been working with: simply.scrollable(tr ...

How to Retrieve a Targeted Element from a Multidimensional Array

I've got some data that looks like this: [[12, 23],[27,-6],[52, -32],[82, 11]] How can I access specific elements within these arrays? With a standard array like this: [a, b, c, d] It's easy to reference 'b' as arrayName[1]. But ho ...