Reputation: 1403
I want to calculate the difference of days from the date & time 2021-02-23T08:31:37.1410141
(yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffff)
which comes from the server which is in string format, with the current date-time in angular.
I tried using
const daysDiff = Math.floor(Math.abs(<any>fromDate - <any>toDate) / (1000*60*60*24));
but it's not working for me. How can I calculate the day difference?
Upvotes: 0
Views: 1745
Reputation: 356
I had the same issue for some time with receiving data like 2020-11-12T12:18:58.019+03:30
but I handled it with this piece of code :
const date = createAt.substr(0, 10);
const time = createAt.substr(11, 19).split('.')[0];
if (date === '0001-01-01') {
return '';
} else {
return (
time +
' - ' +
moment(date, 'YYYY-M-D').locale('fa').format('YYYY/MM/DD')
);
}
since you are receiving a string of data as big as 27 you have to change the .substr(0, 10)
methods according to your needs
Upvotes: 0
Reputation: 356
const date1 = new Date("2021-02-23T08:31:37.1410141");
const date2 = new Date();
const diffTime = date2.getTime() - date1.getTime();
const diffDays = Math.floor(Math.abs(diffTime / (1000 * 3600 * 24)));
Upvotes: 2
Reputation:
Would this work for you?
Date.now() - <old date timestamp>
this gives the difference in miliseconds
So to calculate the difference in days we use:
Math.floor((Date.now() - <old date timestamp>) / (1000 * 60 * 60 * 24))
Upvotes: 0