Javascript date difference

I looked: Get the difference between two dates in javascript?

And I still can't get this to work.

var difference = data.List[0].EndDate - Math.round(new Date().getTime()/1000.0) * 1000; var daysRemaining = Math.floor(difference / 1000 / 60 / 60 / 24); var hoursRemaining = Math.floor(difference / 1000 / 60 / 60 - (24 * daysRemaining)); var minutesRemaining = Math.floor(difference / 1000 / 60 - (24 * 60 * daysRemaining) - (60 * hoursRemaining)); var secondsRemaining = Math.floor(difference / 1000 - (24 * 60 * 60 * daysRemaining) - (60 * 60 * hoursRemaining) - (60 * minutesRemaining)); 

data.List [0] .EndDate is the UTC number (for example: 1291427809310 ( http://www.epochconverter.com/ )), which will always be later than the current date.

+8
javascript date
source share
5 answers
 function days_between(date1, date2) { // The number of milliseconds in one day var ONE_DAY = 1000 * 60 * 60 * 24 // Convert both dates to milliseconds var date1_ms = date1.getTime() var date2_ms = date2.getTime() // Calculate the difference in milliseconds var difference_ms = Math.abs(date1_ms - date2_ms) // Convert back to days and return return Math.round(difference_ms/ONE_DAY) } 

http://www.mcfedries.com/javascript/daysbetween.asp

+11
source share

Now I think this is the best solution:

http://momentjs.com/

+6
source share

Are you saying that the UTC timestamp is "2004-09-16T23: 59: 58.75"?

So you basically do

 var x = "2004-09-16T23:59:58.75" - 123456 

Now that you have clarified that what is stated above is not applicable. A new problem is the number of milliseconds in the past, so when you do a difference calculation, you get a negative number. You probably want to change the order.

 var difference = new Date().getTime()-data.List[0].EndDate; 
+5
source share

If EndDate is in milliseconds and getTime () returns milliseconds, why do you divide it by 1000 to multiply it by 1000 in one line? And if you only need a second precision for the rest of the code, why work in milliseconds? Start with a few seconds to simplify all your calculations:

 var difference = Math.round((data.List[0].EndDate - new Date().getTime()) / 1000); 
+3
source share

I wrote a component that does just that if you do not want to use the full library, for example moment.js. It can also produce the difference in any required unit of time (days, weeks, months, minutes, hours, etc.).

Here's how it works:

 var date1 = date.getTime(); var date2 = data.List[0].EndDate; var millisecondsRemaining = dateDiff(date1, date2, 'milliseconds'); var secondsRemaining = dateDiff(date1, date2, 'seconds'); var minutesRemaining = dateDiff(date1, date2, 'minutes'); var hoursRemaining = dateDiff(date1, date2, 'hours'); var daysRemaining = dateDiff(date1, date2, 'days'); var weeksRemaining = dateDiff(date1, date2, 'weeks'); var monthsRemaining = dateDiff(date1, date2, 'months'); var yearsRemaining = dateDiff(date1, date2, 'years'); 

To use the dateDiff () method, you can simply import the component.

0
source share

Source: https://habr.com/ru/post/650112/


All Articles