Get the difference between two dates using jquery

Is it possible to suggest a jQuery plugin for calculating the difference between two dates (dates can also contain time) and show it as “32 days”, “13 hours”, “20 minutes”, etc.?

+3
source share
5 answers

I highly recommend using the excellent datejs structure to easily do all the calculations of the time on a date

+4
source

You can add, subtract and do many other things with the built-in JavaScript Date . powerful enough for most of your needs. If you use it, you save kilobytes of page size and make the code understandable to everyone (perhaps 90% of JavaScript developers have never used any plugins to calculate dates).

What I hate about a Date object is the lack of inline formatted output.

For example, you cannot specify a localized week or month name without parsing strings. Then datejs comes to your aid.

What can a Date object do?

var msMinute = 60*1000, msDay = 60*60*24*1000, a = new Date(2012, 2, 12, /* days, hours*/ 23, 59, 59), b = new Date("2013 march 12"), /* string */ c = new Date(), /* now */ d = new Date(c.getTime() + msDay - msMinute); /* tomorrow - minute */ console.log(a.getUTCHours()); console.log(typeof (b - a + 1000)); console.log(Math.floor((b - a) / msDay) + ' full days between'); console.log(Math.floor(((b - a) % msDay) / msMinute) + ' full minutes between'); console.log('Today is ' + c.getDay() + ' day of week'); console.log('Tomorrow is ' + d.getDay() + ' day of week'); console.log('Your timezone offset is ' + c.getTimezoneOffset() + ' minutes'); 

Easy to calculate days before Christmas

And, sometimes the joke has more truth, then you might expect OMG, a scary dream

+11
source

Here is a pretty simple Javascript implementation that I just hacked. You can do the math to extend it to several months or years, or remove plurals for 1 value, if necessary.

 var dateDiff = function ( d1, d2 ) { var diff = Math.abs(d1 - d2); if (Math.floor(diff/86400000)) { return Math.floor(diff/86400000) + " days"; } else if (Math.floor(diff/3600000)) { return Math.floor(diff/3600000) + " hours"; } else if (Math.floor(diff/60000)) { return Math.floor(diff/60000) + " minutes"; } else { return "< 1 minute"; } }; dateDiff(new Date(1990, 1, 1), new Date(1990, 1, 13)) // -> 12 days 
+6
source

I think jQuery EasyDate is exactly what you are looking for.

+2
source
 day1= $("#tMTLCLsDay").val(); month1= $("#tMTLCLsMnth").val(); year1= $("#tMTLCLsYr").val(); hour1= $("#tMTLCLs_HOUR").val(); min1= $("#tMTLCLs_MIN").val(); var Date1=month1+ "/" + day1+ "/" + year1+ " " + hour1 + ":" + min1+ ": 00" ; day2= $("#tMTLCLsDay").val(); month2= $("#tMTLCLsMnth").val(); year2= $("#tMTLCLsYr").val(); hour3= $("#tMTLCLs_HOUR").val(); hour2= $("#tMTLCLs_MIN").val(); var Date2=month2+ "/" + day2+ "/" + year2+ " " + hour2 + ":" + hour2 + ": 00" ; if(Date.parse(Date1)<Date.parse(Date2)) { alert("Date 2 is large"); } 
+1
source

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


All Articles