How to find the difference between two dates? - javascript

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

Related

Get difference between dates and format with 'DD/MM/YYYY HH:MM:SS'

How do I transform the milliseconds from date.now() into the format DD/MM/YYYY HH:MM:SS or transform a string with a date into milliseconds.
I also have a string which returns the following:
appointment: "21/11/2019 18:00:30"
I need to get the difference in hours between the date.now and appointment date
To transform a date, you can use DatePipe:
let datePipe = new DatePipe();
datePipe.transform(date, 'dd/MM/yyyy hh:mm:ss');
To get the difference between Date.now() and a date, you can take a look at this answer, or you can use moment like that:
import * as moment from 'moment';
// ...
const difference = moment(moment(),"DD/MM/YYYY HH:mm:ss").diff(moment(appointment,"DD/MM/YYYY HH:mm:ss"))
You will need to install moment like that:
npm install --save moment
You need to convert date string to a valid format like this "2019-11-21T18:00:30"; and then get the difference using .getTime()
Try like this:
let difference = Math.abs(new Date().getTime() - new Date(this.appointment).getTime());
var seconds = difference / 1000;
var minutes = difference / 1000 / 60;
var hours = minutes / 60;
console.log("Difference in Hours",hours)
console.log("Difference in minutes",minutes)
console.log("Difference in seconds",seconds)
Working Demo
Here is working Example
var d = new Date,
dformat = [d.getDate(),
d.getMonth()+1 ,
d.getFullYear()].join('/')+' '+
[d.getHours(),
d.getMinutes(),
d.getSeconds()].join(':');
console.log(dformat)
OUT PUT
26/12/2019 15:48:17
There's is one way to do it without 3rd party
let date = new Date(Date.now());
date = `${date.getDate()}:${date.getMonth() + 1}:${date.getFullYear()}:${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
and there's a way using angular if you use Angular, you can use "DatePipe", with transform method, like that:
datePipe.transform(date, 'dd/MMM/yyyy hh:mm:ss');
and to get the difference btw dates there is only one way
const currentDate = new Date('26/12/2019');
const appoinemnt = new Date('15/1/2020');
const diffTime = Math.abs(appoinemnt - currentDate );
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
console.log(diffDays);
Hours difference:
const dateAppointment = new Date('1/11/2019 18:00:30');
const currentDate = new Date();
console.log(`currentDate is `, currentDate );
const diffTime = Math.abs(currentDate - dateAppointment);
const diffHours = Math.ceil(diffTime / (1000 * 60 * 60));
console.log(`diffHours is`, diffHours );
An example:
const dateAppointment = new Date('1/11/2019 18:00:30');
const currentDate = new Date();
console.log(`currentDate is `, currentDate );
const diffTime = Math.abs(currentDate - dateAppointment);
const diffHours = Math.ceil(diffTime / (1000 * 60 * 60));
console.log(`diffHours is`, diffHours );
UPDATE:
HTML:
<p>currentDate is {{ currentDate | date : 'dd.MM.y, HH:mm:ss' }} </p>
<p>dateAppointment is {{ dateAppointment | date : 'dd.MM.y, HH:mm:ss' }} </p>
<p>differenceHour is {{ differenceHour }} </p>
TypeScript:
name = 'Angular';
currentDate = new Date();
dateAppointment = new Date('1/11/2019 18:00:30');
differenceHour = 0;
constructor(){
const diffTime = Math.abs(this.currentDate - this.dateAppointment);
this.differenceHour = Math.ceil(diffTime / (1000 * 60 * 60));
}
An stackblitz example can be seen here

Calculating time difference between two dates picked with datapicker

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));

JavaScript day difference function outputs wrong days

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);

Comparing the difference between two dates in the US Date Standard of mm/dd/yyyy

I need your help,
How can one compare the difference between two given dates while using the US date standard of mm/dd/yyyy?
A few examples below:
11/20/2014 minus 11/25/2014 = -5
11/25/2014 minus 11/25/2014 = 0
11/27/2014 minus 11/25/2014 = 2
var date1 = new Date("11/20/2014");
var date2 = new Date("11/25/2014");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
try this

How to calculate the difference between date of birth and the system date in JavaScript

I want to calculate the difference between a date of birth and the system date in JavaScript.
in JSP and JavaScript:
'Date Of Birth
'
function currentAge(DateOfBirth){
//alert(DateOfBirth);
var date1 = DateOfBirth;
date1 = document.getElementById("dateOfBirth").value;
alert(date1);
var date2 = new Date();
alert(date2);
// _Diff=Math.ceil((date2.getTime()-date1.getTime())/(one_day));
/* var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
//var diffDays = date2.getDate() - date1.getDate(); */
//alert(_Diff);
}
function dateDiff(dateform) {
date1 = new Date();
date2 = new Date();
diff = new Date();
date1temp = new Date(dateform.firstdate.value);
date1.setTime(date1temp.getTime());
date2temp = new Date(dateform.seconddate.value);
date2.setTime(date2temp.getTime());
diff.setTime(Math.abs(date1.getTime() - date2.getTime()));
timediff = diff.getTime();
days = Math.floor(timediff / (1000 * 60 * 60 * 24));
dateform.difference.value = days;
return false;
}
Now its fine
function currentAge(DateOfBirth){
var date1 = document.getElementById("dateOfBirth").value;
alert(date1);
var date11=new Date(date1);
alert(date11);
var date2 = new Date();
alert(date2);
alert(3);
var timeDiff = Math.abs(date2.getYear() - date11.getYear());
alert(timeDiff);
}

Categories

Resources