2016-07-26 32 views

回答

6

的捷徑:

new Date(2016, 0, 208) 

將返回Date 2016-07-26

+0

比我預期的更容易。非常感謝您 –

+0

不客氣) –

7

試試這個:

function dateFromDay(year, day){ 
    var date = new Date(year, 0); // initialize a date in `year-01-01` 
    return new Date(date.setDate(day)); // add the number of days 
} 

dateFromDay(2016, 208); 
-1

function dayNumToDate(numDays) { 
 
    var thisYear = new Date().getFullYear(); 
 
    var initDate = new Date("JANUARY, 1," + thisYear); 
 
    var millis = numDays * 24 * 60 *60 * 10000; 
 
    var newDate = new Date(millis + initDate.getTime()); 
 
    return newDate; 
 
} 
 
var date = dayNumToDate(208); 
 
alert(date.toString());

-1

//初始化日期功能

var now = new Date(); 

//天數日期轉換

var curr_day_number = 208; 

//計算一天的總時間

var oneDay = 1000 * 60 * 60 * 24; 

//計算總時間所有日子所以

var total_number_of_time_of_day = curr_day_number * oneDay; 

// calcuclate開始一年中的哪一天

var start_date_of_year = new Date(now.getFullYear(), 0, 0); 

//計算一年時間標記的開始日期

var start_date_of_year_timestamp = new Date(start_date_of_year).getTime(); 

今年// calulate總時間戳開始用一年的總天數

var calculated_timestamp = start_date_of_year_timestamp + total_number_of_time_of_day; 

//使用日期函數

var calculated_date = new Date(calculated_timestamp); 

document.write(calculated_date); 
轉換時間戳爲日期

這將幫助你詳細。

+0

您可否詳細說明投票反饋? –

+0

只有一行的解決方案。它符合規範,簡潔易讀。那麼爲什麼會有人想要使用跨越多行的解決方案,可能會出現容易出錯的計算結果,而且很難閱讀和理解? – str

相關問題