JavaScript计算两个日期之间的差异,分别为年,月,日,小时,分钟

时间:2015-07-22 10:08:10

标签: javascript date difference days

我从很多网站收集这个JavaScript, 这个脚本可以提供我到目前为止所需要的,即选择日期和葡萄干日期之间的差异, 我复制了很多相同结果的代码,但我认为如果我使用一个函数[我不知道如何使用它]会在更少的代码中得到相同的结果,

这是我的剧本,

<script type="text/javascript">
var s = '2015-05-21T10:17:28.593Z';
var a = s.split(/[^0-9]/);
var d=new Date (a[0],a[1]-1,a[2],a[3],a[4],a[5] );

var fromDate = new Date(d);
var toDate = new Date();
// current date format as mm-dd-yyyy 
    var Current_Day = new Date();
    var sec = Current_Day.getSeconds();
    var min = Current_Day.getMinutes();
    var hr = Current_Day.getHours();
    var dd = Current_Day.getDate();
    var mm = Current_Day.getMonth()+1; //January is 0!
    var yyyy = Current_Day.getFullYear();
// 
if(dd<10){
        dd='0'+dd
    } 
if(mm<10){
        mm='0'+mm
    }
if(hr<10){
        hr='0'+hr
    }
if(min<10){
        min='0'+min
    }
//
var Current_Day = dd+'-'+mm+'-'+yyyy+'&nbsp<span style="color:#008000;font-weight:800;">(&nbsp'+hr+ ":" +min+ ":" +sec+'&nbsp)</span>';
// current date format as mm-dd-yyyy 
    var Start_Date = new Date(d);
    var sec = Start_Date.getSeconds();
    var min = Start_Date.getMinutes();
    var hr = Start_Date.getHours();
    var dd = Start_Date.getDate();
    var mm = Start_Date.getMonth()+1; //January is 0!
    var yyyy = Start_Date.getFullYear();
if(dd<10){
        dd='0'+dd
    } 
if(mm<10){
        mm='0'+mm
    }
if(hr<10){
        hr='0'+hr
    }
if(min<10){
        min='0'+min
    }
var Start_Date = dd+'-'+mm+'-'+yyyy+'&nbsp<span style="color:#008000;font-weight:800;">(&nbsp'+hr+ ":" +min+ ":" +sec+'&nbsp)</span>';
// End date format as mm-dd-yyyy   
// Start_Date = str;
var differenceTravel = toDate.getTime() - fromDate.getTime();
YearDifferenceTravel = Math.floor(differenceTravel / 1000 / 60 / 60 / 24 / 365);
var month = toDate.getMonth() - fromDate.getMonth() + (12 * (toDate.getFullYear() - fromDate.getFullYear()));
var diffDays = Math.floor((differenceTravel) / (1000 * 60 * 60 * 24));
var hours = Math.floor((differenceTravel) / (1000 * 60 * 60));
var minutes = Math.floor((differenceTravel) / (1000 * 60));
var seconds = Math.floor((differenceTravel) / (1000));
var difference_ms = differenceTravel;


// Deference Years, Months, Days
var Tyears = Math.round(differenceTravel / (365*60*60*24*1000));
var Tmonths = Math.round((differenceTravel - (Tyears * 365*60*60*24*1000)) / (1000*60*60*24*30));
var Tdays = Math.round((differenceTravel - Tyears - Tmonths) / (1000*60*60*24*30));

//
document.writeln('<div>');
document.writeln('<ul>');
document.writeln('<span style="color:#008000">Information:</span>');
document.writeln('<li><span style="color:#008000">Start date is&nbsp;:</span> ' + Start_Date+'</li>');
document.writeln('<li><span style="color:#8080C0">Today date is&nbsp;:</span> '+ Current_Day +'</li>');


document.writeln('<br />');
document.write('Total in between selected dates are: &nbsp; <br />'+ Tyears +'<span style="color:#008000">&nbsp;year(s)&nbsp; and&nbsp;</span> '+ Tmonths +'<span style="color:#008000"> &nbsp; month(s)&nbsp; and &nbsp;</span>'+ Tdays +'<span style="color:#008000"> &nbsp; day(s)</span></li>');
document.writeln('<br /><br />');
document.writeln('<span style="color:#8080C0">From beginning, Date & Times equal to:</span>');
document.write('<li>Total Year in between&nbsp;: '+ YearDifferenceTravel +'</li>');
document.write('<li>Total Month in between&nbsp;:<span style="color:#400000;font-weight:800;"> '+ month +'</span></li>');
document.write('<li>Total Days in between&nbsp;: '+ diffDays +'</li>');
document.write('<li>Total Hours in between&nbsp;: '+ hours +'</li>');
document.write('<li>Total Minutes in between&nbsp;: '+ minutes +'</li>');
document.write('<li>Total Seconds in between&nbsp;: '+ seconds +'</li>');
document.write('<li>Total Millisecond in between&nbsp;: '+ difference_ms +'</li>');
document.writeln('</ul>');
// -----
document.writeln('</div>');
</script>

如果你看到我重复这段代码以获得结果,

    if(dd<10){
        dd='0'+dd
    } 
if(mm<10){
        mm='0'+mm
    }
if(hr<10){
        hr='0'+hr
    }
if(min<10){
        min='0'+min
    }

我希望有人帮助我纠正这个脚本,使其比它的行更少。

1 个答案:

答案 0 :(得分:0)

也许不是最好的,但我认为这很清楚

var FromDate = new Date(2001, 9, 11, 1, 23, 45);
var ToDate = new Date();

function compareDates(beginDate, endDate) {
    var tDate = beginDate;
    var neg = false;
    if (beginDate > endDate) {
        beginDate = end;
        endDate = tDate;
        neg = true;
    }
    tDate = new Date(beginDate);

    function doCompare(from, end, what) {
        var ret = -1;
        while (from <= end) {
            ret++;
            from['set' + what](from['get' + what]() + 1);
        }
        from['set' + what](from['get' + what]() - 1);
        return ret;
    }
    return {
        negative: neg,
        years: doCompare(tDate, endDate, 'FullYear'),
        months: doCompare(tDate, endDate, 'Month'),
        days: doCompare(tDate, endDate, 'Date'),
        hours: doCompare(tDate, endDate, 'Hours'),
        minutes: doCompare(tDate, endDate, 'Minutes'),
        seconds: doCompare(tDate, endDate, 'Seconds')
    };
}
console.log(compareDates(FromDate, ToDate));