如何在Javascript中找到两个DateTime字符串之间的区别

时间:2022-12-28 21:29:08

So I have two strings in javascript:

所以我在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...

JavaScript / jQuery专家可以帮助我吗?非常感激...

5 个解决方案

#1


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>

#2


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

更多细节查看此答案详细解答

#3


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>

#4


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.

如果你用日期调用getDates方法,那么你可以在控制台中看到不同的时间。

#5


-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/

#1


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>

#2


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

更多细节查看此答案详细解答

#3


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>

#4


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.

如果你用日期调用getDates方法,那么你可以在控制台中看到不同的时间。

#5


-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/