javascript calculating difference between two dates gives me NaN - javascript

var startDate = new Date($('#startdate').val());
var endDate = new Date($('enddate').val());
var msPerDay = 1000*60*60*24;
var diff = Math.floor(startDate.getTime() - endDate.getTime());
$('#period').text(diff/msPerDay);
This is my code, it's pretty much based on other answers on SO. I can't seem to figure out what I'm doing wrong here ?

You have typo in your code:
Replace $('enddate').val() with $('#enddate').val()

var startDate = new Date($('#startdate').val());
var endDate = new Date($('#enddate').val()); //using id for enddate?

Are you sure you meant
var endDate = new Date($('enddate').val());
and not
var endDate = new Date($('#enddate').val());
?

Related

Calculate age in JS given the birth date in "dd/mm/yyyy" format

I am trying to write a function in JavaScript to calculate age.
I wrote a function but it does not give accurate age.
function calculateAge(date) {
var formattedDate = date.split("/")
var birthdateTimeStamp = new Date(formattedDate[2], [1], formattedDate[0])
var currentDate = new Date().getTime();
var difference = currentDate - birthdateTimeStamp;
var currentAge = Math.floor(difference / 31557600000)
// dividing by 1000*60*60*24*365.25
return currentAge
}
I try calculateAge("25/11/1993") and I don't get accurate age. Kindly guide me.
You missed the formattedDate for [1] in the new Date() function, so formattedDate[1] it should be.
function calculateAge(date) {
var formattedDate = date.split("/")
var birthdateTimeStamp = new Date(formattedDate[2], formattedDate[1], formattedDate[0])
var currentDate = new Date().getTime();
var difference = currentDate - birthdateTimeStamp;
var currentAge = Math.floor(difference / 31557600000)
// dividing by 1000*60*60*24*365.25
return currentAge
}
var age = calculateAge('25/11/1993');
console.log('My age is', age);

Get days difference in two dates format (YYYY/MM/DD hh:mm)?

I'm trying to get the diference between 2 dates that look like this:
2018/10/05 13:59
But my issue is with my code, it always retunr NaN.
This is my current code:
var start = '2018/10/05 13:59';
var end = '2018/10/05 17:59';
var converted_start = start.replace("/", "-");
var converted_end = end.replace("/", "-");
// end - start returns difference in milliseconds
var diff = new Date(converted_end - converted_start);
// get days
var days = diff/1000/60/60/24;
alert(days);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
I tried to replace the / with - in my code but that didn't fix the issue!
Could someone please advice on this issue?
Thanks in advance.
var start = '2018/10/05 13:59';
var end = '2018/10/05 17:59';
var startDate = Date.parse(start);
var endDate = Date.parse(end);
var diff = new Date(endDate - startDate);
var days = diff/1000/60/60/24;
this works for me
You can use the following to get days difference in two dates format (YYYY/MM/DD hh:mm):
var start = new Date('2018/10/05 13:59');
var end = new Date('2018/10/05 17:59');
// get hours
var hours= (end-start)/3600000;
alert(hours);

convert date to number in javascript

I'm using google app script for some application and in javascript m getting NaN error
var state = $("#state").text();
var now = new Date();
var startDate = $("#startDate").val();
var endDate = $("#endDate").val();
var sdate=Number(startDate);
var edate=Number(endDate);
var empName = $("#searchEmpName").val();
alert(edate+","+sdate);
if (edate < sdate){
// show error msg
}
You can use var sdate = parseInt(startDate); instead of var sdate = Number(startDate); and same for the end date.
var sdate=new Date(startDate);
var edate=new Date(endDate);
Here no need to convert sdate and edate in Number.
You can compare both the dates now as you have done before.

JavaScript calculate difference in seconds between to datetime

I am new to JavaScript and have done some research, however I cannot find a straightforward solution to relatively simple problem. I have a following var:
var recordDate = '2016-04-20 17:52:33';
I need to get the difference between now and recordDate in seconds. What would be the most efficient way to achieve it?
Try to convert recordDate to a Date Object first
var match = recordDate.match(/^(\d+)-(\d+)-(\d+) (\d+)\:(\d+)\:(\d+)$/);
var date = new Date(match[1], match[2] - 1, match[3], match[4], match[5], match[6]);
var currentDate = new Date();
var differenceInSeconds = Math.abs(date.getTime() - currentDate.getTime())/1000;
DEMO
var recordDate = '2016-04-20 17:52:33';
var match = recordDate.match(/^(\d+)-(\d+)-(\d+) (\d+)\:(\d+)\:(\d+)$/);
var date = new Date(match[1], match[2] - 1, match[3], match[4], match[5], match[6]);
alert(date.toString());
var currentDate = new Date();
var differenceInSeconds = Math.abs(date.getTime() - currentDate.getTime())/1000;
alert(differenceInSeconds);
Set the date record like this:
var recordDate = new Date('2016-04-20 17:52:33');
var currentTime = new Date();
And then calculate it like this:
(currentTime - recordDate) / 1000 = {Seconds in between}
Additional comment:
Additional explanation why I am not using abs is because it looks like the purpose for this it to display like where in stackoverflow - edited 5 seconds ago or similar, when the currentTime will never be less then the record date and therefore one calculation and dependency less.

Get Diffrence between two date string in momentjs?

I am using momentjs for date and I have one date string, ie,"2015-05-10",I want to get date difference from today
var today= moment().format('YYYY-MM-DD');
How it is possible here?
here is a example,
var now = moment(); // moment object of now
var day = moment("2015-05-13"); // moment object of other date
$scope.difference = now.diff(day, 'days'); // calculate the difference in days
$scope.difference = now.diff(day, 'hours'); // calculate the difference in hours
check more options here
here is a example
You can use diff
//Convert to date
var today = moment();
var date = moment("2015-05-13", "YYYY-MM-DD");
//Use diff
var duration = today.diff(date);
var hours = duration.asHours();
if you are talking about time difference in hours
var now = "04/09/2013 15:00:00";
var then = "02/09/2013 14:20:30";
var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
var s = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss");

Categories

Resources