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

后端 未结 9 1057
一整个雨季
一整个雨季 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:14

    The shortest possible way is to create a new date object with the given year, January as month and your day of the year as date:

    const date = new Date(2017, 0, 365);
    
    console.log(date.toLocaleDateString());

    As for setDate the correct month gets calculated if the given date is larger than the month's length.

提交回复
热议问题