I've got a option for users to select two dates, then it should display the difference between the two.
function calculateDate() {
var dateDom1 = document.getElementById("startDate").value;
var dateDom2 = document.getElementById("endDate").value;
var date1 = Date.parse(dateDom1);
var date2 = Date.parse(dateDom2);
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
console.log(diffDays);
}
Can anyone tell me why I get
InternshipDetails:880 Uncaught TypeError: date2.getTime is not a
function
in the inspector console
Date.parse return time in ms
so you should do something like this:
var timeDiff = Math.abs(date2 - date1);
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
instead this:
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
Related
Hi Guys i have two date
firstDate = "06/11/2019"
lastDate = "15/11/2019"
var date1 = new Date(firstDate);
var date2 = new Date(lastDate);
var diffTime = Math.abs(date2 - date1);
var diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
i get diffDays=NaN. how can i do this. Thanks.
Your lastDate is an invalid date.
You can check this with
Date.parse("15/11/2019 00:00:00")
which will return a NaN
Check the Warnings section
https://www.w3schools.com/js/js_date_formats.asp
How to calculate the time difference using jquery?
Example:
First 4 hours is equivalent to 0.5
Whole day is equivalent to 1.0
Example working hours
8am to 12pm is 0.5
1pm to 5pm will equivalent to 0.5 also.
and 24 hours is equivalent to 1.0
for example:
11/11/2014 08:00am 11/11/2014 12:00pm
result is 0.5..
Thanks
$('#get').click(function(){
var startDate = new Date($('#dpd1').val());
var startTime =(''+ $("#time1").val()).split(":");
var endDate = new Date($('#dpd2').val());
var endTime = (''+ $("#time2").val()).split(":");
startDate.setHours(parseInt(startTime[0]));
startDate.setMinutes(parseInt(startTime[1]));
endDate.setHours(parseInt(endTime[0]));
endDate.setMinutes(parseInt(endTime[1]));
var diff = endDate.getTime() - startDate.getTime();
var differenceDays = (diff) / (1000 * 60 * 60 * 24);
var differenceHours = ((diff) % (1000 * 60 * 60 * 24))/ (1000 * 60 * 60);
$('#totalleave').val(Math.round(differenceDays) + ' days ' +Math.round(differenceHours) + ' hours' );
});
var date1 = new Date(d1), date2 = new Date(date2);
var hourDiff = date1.getHours()-date2.getHours();
now manipulate this hourDiff the way you want.
I use the following JS function to calculate the difference of days between today and a day in the future:
var oneDay = 24 * 60 * 60 * 1000;
var today = new Date();
var futureDay = new Date(futureDate);
var diffDays = Math.round(Math.abs((today.getTime() - futureDay.getTime()) / (oneDay)));
My problem: When the futureDate is today, I get the result "1" and if it is tomorrow, I get "0".
What is wrong about this function?
Try this way, you didn't set future date
var oneDay=1000 * 3600 * 24;
var dateToday = new Date("02/24/2015"); //or just, new Date();
var dateFuture = new Date("02/25/2015"); // set here future date like this
var timeDiff = Math.abs(dateFuture.getTime() - dateToday.getTime());
var diffDays = Math.ceil(timeDiff /oneDay);
alert(diffDays);
I am looking for a way to convert a javascript Date to a number that represents the amount of days passed since , for example, 1.1.1900, for that date. As it is usually done with timestamps and miliseconds.
Can I do this?
You can achieve this by using the javascript function getTime().
Code:
var a = new Date();
alert(a.getTime());
Working Example
According to getTime() definition:
The getTime() method returns the numeric value corresponding to the
time for the specified date according to universal time.
More can be found in this link
UPDATE:
If you want to have it in no of days then you would need to calculate it or use library like moment.js
Working Fiddle for Days
For example something like:
var date1 = new Date("1/Jan/1900 00:00:00");
var date2 = new Date("20/Nov/2012 19:15:00");
var diff = date2.getTime() - date1.getTime();
var days = Math.floor(diff / (1000 * 60 * 60 * 24));
diff -= days * (1000 * 60 * 60 * 24);
var hours = Math.floor(diff / (1000 * 60 * 60));
diff -= hours * (1000 * 60 * 60);
var mins = Math.floor(diff / (1000 * 60));
diff -= mins * (1000 * 60);
var seconds = Math.floor(diff / (1000));
diff -= seconds * (1000);
document.write(days + " days, " + hours + " hours, " + mins + " minutes, " + seconds + " seconds");
I am building an Instagram feed with JQuery into my site and want to show how long has passed since the post was submitted in a short form like: 23H or 2D or 3M or 1Y depending on how long its been. I've got my two date objects but I can't figure our how to calculate the difference and display it how i want.
I am fairly new to JS/Jquery and as far as i could get was:
var pd = new Date(postDate);
var nd = new Date();
var nd = nd.getTime();
var difference = nd-pd;
How do I calculate the difference between two dates in hours, days, months and years?
Thanks.
Doing anything with dates is generally painful.
If you aren't committed to using that exact format, you can use a library for this instead.
moment.js has a .fromnow() function.
or timeago.js can be used to update the element on the page periodically, so if the user leaves the page open for a few minutes, the time stamps will count up.
You can do this to get the time elapsed since posted
var timeDiff = Math.abs(nd.getTime() - pd.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
you can try like this.
var pd = new Date(postDate);
var nd = new Date();
var Hours = nd.getHours() - pd.getHours();
var Days = nd.getDay() - pd.getDay();
var Months = nd.getMonth() - pd.getMonth();
var Years = nd.getYear() - pd.getYear();
or get millisecods diference
var miliseconds = (nd - pd).getTime(); //gets time in miliseconds since 1/1/1970
then use your logic to calculate hours, days, months and years
you can have a look at this Work with a time span in Javascript
moments
ar date1 = new Date("7/Nov/2012 20:30:00");
var date2 = new Date("20/Nov/2012 19:15:00");
var diff = date2.getTime() - date1.getTime();
var days = Math.floor(diff / (1000 * 60 * 60 * 24));
diff -= days * (1000 * 60 * 60 * 24);
var hours = Math.floor(diff / (1000 * 60 * 60));
diff -= hours * (1000 * 60 * 60);
var mins = Math.floor(diff / (1000 * 60));
diff -= mins * (1000 * 60);
var seconds = Math.floor(diff / (1000));
diff -= seconds * (1000);
console.log(days + " days : " + hours + " hours : " + mins + " minutes : " + seconds + " seconds");
My solution is dirty but direct: calculate them by myself.
Record start time:
var BEGIN_TIME=new Date();
var HOUR=BEGIN_TIME.getHours();
var MINUTE=BEGIN_TIME.getMinutes();
var SECOND=BEGIN_TIME.getSeconds();
Then do so some math
var today=new Date();
h=today.getHours();
m=today.getMinutes();
s=today.getSeconds();
s = s - SECOND;
if (s<0) { s=s+60; m=m-1; }
m = m - MINUTE;
if (m<0) { m=m+60; h=h-1; }
h = h - HOUR;