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

后端 未结 9 1055
一整个雨季
一整个雨季 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 09:16

    this also works ..

    function to2(x) { return ("0"+x).slice(-2); }
    function formatDate(d){
        return d.getFullYear()+"-"+to2(d.getMonth()+1)+"-"+to2(d.getDate());
        }
    document.write(formatDate(new Date(2016,0,257)));
    

    prints "2016-09-13"

    which is correct as 2016 is a leaap year. (see calendars here: http://disc.sci.gsfc.nasa.gov/julian_calendar.html )

提交回复
热议问题