0

So I have two strings in javascript:

old_date = "2010-11-10 07:30:40";
new_date = "2010-11-15 08:03:22";

I want to find the difference between these two dates, but I am totally at a loss :(

I tried to do the following:

old_date_obj = new Date(Date.parse(old_date));
new_date_obj = new Date(Date.parse(new_date));

But it is giving me error. However that is only start of my woes...I need to show the difference as:

Difference is X Days, Y hours and Z minutes

Can JavaScript/jQuery gurus help me please? Much appreciated...

8

5 Answers 5

1
<script>  
    function Calculate() {
       old_date = "2010-11-10 07:30:40";
        new_date = "2010-11-15 08:03:22";

        old_date_obj = new Date(Date.parse(old_date, "dd/mm/yyyy HH:mm:ss"));
        new_date_obj = new Date(Date.parse(new_date, "dd/mm/yyyy HH:mm:ss"));

        var utc1 = Date.UTC(new_date_obj.getFullYear(), new_date_obj.getMonth(), new_date_obj.getDate());
        var utc2 = Date.UTC(old_date_obj.getFullYear(), old_date_obj.getMonth(), old_date_obj.getDate());
        alert(Math.floor((utc2 - utc1) / (1000 * 60 * 60 * 24)));
    }
</script>
0
0

simply add in you date and it will work for you.

"2010-11-10T07:30:40+01:00"

for more detail check this answer Answer in detail

0
<script type="text/javascript">
    // The number of milliseconds in one day, hour, and minute
    var ONE_DAY = 1000 * 60 * 60 * 24;
    var ONE_HOUR = 1000 * 60 * 60;
    var ONE_MINUTE = 1000 * 60;

    var old_date = "2010-11-10T07:30:40";
    var new_date = "2010-11-15T08:03:22";

    // Convert both dates to milliseconds
    var old_date_obj = new Date(old_date).getTime();
    var new_date_obj = new Date(new_date).getTime();

    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(new_date_obj - old_date_obj)

    // Convert back to days, hours, and minutes
    var days = Math.round(difference_ms / ONE_DAY);
    var hours = Math.round(difference_ms / ONE_HOUR) - (days * 24) - 1;
    var minutes = Math.round(difference_ms / ONE_MINUTE) - (days * 24 * 60) - (hours * 60);

    alert('Difference is ' + days + ' days, ' + hours + ' hours and ' + minutes + ' minutes.' );
</script>
0
<script type="text/javascript">
    function getDates(strDate1, strDate2) {
    /*Now strDate1 and strDate2 string. So we should convert them to javascript datetime value.*/
     var tempDate1  = strDate1.split(/\-|\s/)
     var date1 = new Date(tempDate1.slice(0,3).reverse().join('/')+' '+tempDate1[3]);
     var tempDate2  = strDate2.split(/\-|\s/)
     var date2 = new Date(tempDate2.slice(0,3).reverse().join('/')+' '+tempDate2[3]);
     var obj1 = $.datepicker.parseDate('dd.mm.yy', $("#date1").val());
     var obj2 = $.datepicker.parseDate('dd.mm.yy', $("#date2").val());
     console.log(findDifferentDate(obj1, obj2));
    }

    function findDifferentDate(obj1, obj2){
      var date1 = getFormattedDate(obj1);
      var date2 = getFormattedDate(obj2);
      var year = date1.getFullYear() - date2.getFullYear();
      var day = date1.getDate() - date2.getDate();
      var month = date1.getMonth() - date2.getMonth();
      var seconds = date1.getSeconds() - date2.getSeconds();
      var minutes = date1.getMinutes() - date2.getMinutes();
      var hour = date1.getHours() - date2.getHours();
      return 'Difference is' + day + 'Days' + month + 'Months' + year + 'Years' + seconds + 'Seconds' + minutes + 'Minutes' + hour + 'Hours';
    }

    function getFormattedDate(date) {
      var year = date.getFullYear();
      var month = (1 + date.getMonth()).toString();
      month = month.length > 1 ? month : '0' + month;
      var day = date.getDate().toString();
      day = day.length > 1 ? day : '0' + day;
      return day + '.' + month + '.' + year;
     }
</script>

If you call getDates method with your dates and then u can see difference time in the console.

-1
var old_date = "2010-11-15 07:30:40";
var new_date = "2010-11-15 08:03:22";

var old_date_obj = new Date(Date.parse(old_date));
var new_date_obj = new Date(Date.parse(new_date));

var diffMs = Math.abs(new_date_obj - old_date_obj);
var diffDays = Math.round(diffMs / 86400000); // days
var diffHrs = Math.round((diffMs % 86400000) / 3600000); // hours
var diffMins = Math.round(((diffMs % 86400000) % 3600000) / 60000); // minutes

https://jsfiddle.net/yps2wb58/1/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.