简体   繁体   中英

hours difference between two years (With leap year) in javascript

How can i get the hours difference between two years (With leap year) in javascript

I have two year 2015 and 2014

var year1="2015";
var year="2016";

I want to get the total hours different between those above years by one line code(with leap year and without leap year)!.

I have tried this below code

// get hours from one year
    var date = new Date;        
    var Hours= date.getFullYear().getHours();


 // get hours between two years    

    var Hours= (date.getFullYear()-dat2.getFullYear()).getHours()

But It's something wrong for me.

You could use a function similar to this:

function getHoursBetweenYears(startYear, endYear) {
    var startDate = new Date(startYear, 0, 1),
        endDate = new Date(endYear, 0 ,1);

    return (+endDate - +startDate) / 3600000;
}

Usage like this:

getHoursBetweenYears(2012, 2013) // 8784

Date object is your saver. Get time differance. then multiply with min, s, ms. Gives you time diff total hour between years.

var year=2015,
year1=2016,
timeDiff =(new Date("01/01/"+year1)-new Date("01/01/"+year))/(1000*60*60);

The leap year should be specified by year and also month. So March d + 59 Add 1 if leap year ….up to December d + 334 Add 1 if leap year

您可以尝试这样。

hours = ((new Date()).setFullYear( 2016 ) - (new Date()).setFullYear( 2015 ))/(1000*3600);

Calculate the diffference in milliseconds from two dates (including the first day of the start year and the last day of the end year) and divide the result by 3600000 (1000 * 60 * 60 = milliseconds in one hour):

// difference in hours for two whole years (2015-2016)
var hourdiff = (new Date('2017/01/01') - new Date('2014/12/31'))/(1000*60*60);

You can create a Date extension to calculate hours in a certain year:

Date.prototype.hoursInYear = function() {
  return ( (new Date(this.getFullYear()+1, 0, 1)) - 
           (new Date(this.getFullYear()-1, 11, 31)) ) / 3600000; }
// usage
new Date(1997, 0, 1).hoursInYear(); // => 8784 
new Date(2008, 0, 1).hoursInYear(); // => 8808 (leap year)

Or even (the number of hours in a (leap)year is constant)

Date.prototype.hoursInYear = function() {
  return new Date(this.getFullYear(), 1, 29).getMonth() == 1 
          ? 8808 : 8784;
}

And finally, using the Date extension, this could be a method to calculate the number of hours in [n years] starting with [startyear]:

function calcHours(startyear, numyears) {
  return isNaN(new Date(startyear, 0, 1))
         ? null // invalid year value
         : Array.apply(null, {0: startyear, length: numyears})
           .map(function(v, i) {return v == this ? v : this + 1;}, startyear)
           .reduce( function(a, b) { 
                     return a +  new Date(b, 0, 1)
                                   .hoursInYear();}, 0);
}
// usage
calcHours(2000, 2); //=> 17592 (2000 is leap year)
calcHours(2001, 2); //=> 17568

View demo jsFiddle

var start = new Date(2015, 0, 0);
var end = new Date(2016, 0, 0);
var diff = end - start;
var oneDay = 1000 * 60 * 60;
var day = Math.floor(diff / oneDay);
alert("Hours: " + day);

Answer

Hours: 8760

Get the seconds of both years. setFullYear gives you the unix timestamp in millis. Divide by 1000 and you have seconds. Get the difference between the two years and divide this through 3600 (seconds per hour). Then you have your difference in hours.

function getDiffHours (year1, year2) {
    var d1 = new Date().setFullYear(year1) / 1000;
    var d2 = new Date().setFullYear(year2) / 1000;
    var diff = Math.abs(d2 - d1);
    return  Math.floor(diff / 3600);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM