satya
satya

Reputation: 3560

Could not calculate the time difference using JavaScript

I need to calculate time difference in hrs in between current date time and user input date time using JavaScript. Here is my code:

var user_date = '31-03-2019';
        var dep_time='12:30PM';
        var datePieces = user_date.split("-");
        var mydate=[datePieces[1] , datePieces[0] , datePieces[2]].join("-");
        var todayDate = new Date();
        var todayMonth = todayDate.getMonth() + 1;
        var todayDay = todayDate.getDate();
        var todayYear = todayDate.getFullYear();
        if (todayDay < 10) {
          todayDay = '0' + todayDay;
        }
        if (todayMonth < 10) {
          todayMonth = '0' + todayMonth;
        }
        var todayDateText = todayMonth + "-" + todayDay + "-" + todayYear;
        var inputToDate = Date.parse(mydate);
        var todayToDate = Date.parse(todayDateText);
        //console.log(inputToDate, todayToDate);
        //console.log(user_date, todayDateText);
        if (inputToDate > todayToDate) {
            var date=new Date;
            var hours = date.getHours();
            var minutes = date.getMinutes();
            var ampm = hours >= 12 ? 'pm' : 'am';
            hours = hours % 12;
            hours = hours ? hours : 12; // the hour '0' should be '12'
            minutes = minutes < 10 ? '0'+minutes : minutes;
            var strTime = hours + ':' + minutes + ' ' + ampm;
            var timeStart = new Date(todayToDate + strTime);
            var timeEnd = new Date(mydate + dep_time);
            console.log(timeStart);
            console.log(timeEnd);
            var diff = (timeEnd - timeStart) / 60000; //dividing by seconds and milliseconds
            var minutes = diff % 60;
            var hours = (diff - minutes) / 60;
            alert(hours);
        } else {

        }

Here I getting the output NAN . I have both user input and current date time and I need the time difference in HRS.

Upvotes: 0

Views: 71

Answers (1)

hiew1
hiew1

Reputation: 1434

1) The Date.parse method turns a date into milliseconds since January 1st, 1970. See https://www.w3schools.com/Jsref/jsref_parse.asp, therefore turning your user input date into milliseconds since January 1st, 1970.

2) In Javascript, the getTime() method on the new Date() object gets the number of milliseconds that have passed since January 1, 1970 until the current time.

3) Therefore, finding the difference of these milliseconds gives you the difference in milliseconds.

4) Since 1 hour = 3600000 ms, to find the difference in hours, divide your answer by 3600000, and get the difference in hours.

  • You also seem to forget to include the dep_time in parsing your date.

And the solution is below:

<script>
"use strict";
var user_date = '31-03-2019 12:30 PM';
        var datePieces = user_date.split("-");
        var mydate=[datePieces[1] , datePieces[0] , datePieces[2]].join("-");

        var todayDate = new Date();
        var todayToDate = todayDate.getTime();
        // In JavaScript, getTime() gets the number of milliseconds that have passed since January 1, 1970.

        var inputToDate = Date.parse(mydate);

        if (inputToDate > todayToDate) {
            var diff = (inputToDate - todayToDate) / 3600000; //Since 1 h = 3600000 ms
            alert(diff);
        } else {
            var diff = (todayToDate - inputToDate) / 3600000; //Since 1 h = 3600000 ms
            alert(diff);
        }
</script>

Upvotes: 1

Related Questions