-1

Hello i want to find the difference between these 2 dates. So as i understand, this is the code for angular getting current date.

let currentDate = new Date();

and in the console log, the format is this. Fri Sep 03 2021 10:49:33 GMT+0800 but the other date that i retrieved from the database is in laravel carbon format i think which is like 2021-08-13T07:17:57.000000Z. Is there a way to find the difference between these 2 dates?

andyiscoding
  • 43
  • 1
  • 8

1 Answers1

0

If you want to stick with dates you can refer Get difference between 2 dates in JavaScript? or use moment js like this:

const today = moment();
const someday = moment('Fri Sep 03 2021 10:49:33 GMT+0800');

const diffDays = today.diff(someday, 'day');
const diffMin = today.diff(someday, 'minute');
const diffSec = today.diff(someday, 'second');

But be aware that moment js adds a bit of an overhead to your application. Only use it if you have significant amount of date/time related calculations.

Akhil
  • 685
  • 1
  • 4
  • 12
  • Thanks for the reply but the user3532758 comment was what i needed. – andyiscoding Sep 03 '21 at 04:22
  • from Moment.js website: `Considering using Moment in your project? There may be better modern alternatives. For more details and recommendations, please see Project Status in the docs.` You may consider other libraries such as luxon – LoLo Sep 03 '21 at 04:37