how can I convert day of year to date in javascript?

后端 未结 9 1056
一整个雨季
一整个雨季 2020-11-30 08:48

I want to take a day of the year and convert to an actual date using the Date object. Example: day 257 of 1929, how can I go about doing this?

9条回答
  •  甜味超标
    2020-11-30 09:27

    // You might need both parts of it-

    Date.fromDayofYear= function(n, y){
        if(!y) y= new Date().getFullYear();
        var d= new Date(y, 0, 1);
        return new Date(d.setMonth(0, n));
    }
    Date.prototype.dayofYear= function(){
        var d= new Date(this.getFullYear(), 0, 0);
        return Math.floor((this-d)/8.64e+7);
    }
    
    var d=new Date().dayofYear();
    //
    alert('day#'+d+' is '+Date.fromDayofYear(d).toLocaleDateString())
    
    
    /*  returned value: (String)
    day#301 is Thursday, October 28, 2010
    */
    

提交回复
热议问题