2015-08-27 201 views
2

我在表格中存儲了一些其他數據的公曆日子。 我得到通過以下二郎這幾天命令:如何將公曆日期轉換爲公曆日期

{Date, _} = calendar:now_to_datetime(now()). 
GDays = calendar:date_to_gregorian_days(Date). 

讓我們這個值作爲一個例子:GDays = 736202.

我打造牛仔一個網站,爲ErlyDTL我的意見。 現在我想以日期格式顯示這些格里曆日子。 (2015年8月28日)。

我用下面的代碼獲取自己的數據從列表中我的觀點:

{% for item in list %} 
    {{item.1}} <br/> 
{% endfor %} 

我嘗試下面的命令{{item.1|date:" D d M Y"}},但我得到一個錯誤:

Unexpected date parameter: 736202

現在哪能將這個公曆日期轉換爲erlyDTL或javascript中的日期時間?

在此先感謝

+0

這裏有關於計算公曆日期的信息:[*將公曆日期轉換爲公曆日期](http://mathforum.org/library/drmath/view/62338.html)。如果您需要實現該算法的幫助,只需詢問。一旦你有一個Date對象,這裏有很多關於如何格式化的問題。 – RobG

+0

我怎樣才能以相反的方式做到這一點,並在第0年(格里高利)開始計算天數。我有你參考中計算的天數。現在我想把這個數字計算回日期戳。 –

+0

當我做736202/365.25時,我得到2015.6112251882273。 現在我陷入困境,因爲我不知道如何從中獲得數月和數天。 –

回答

0

下面是我所需要的算法。我以一種非常簡單的方式在javascript中創建了它,現在已經足夠好了,所以不要緊張;)。 沒有支持閏年。

<html> 
    <head> 
    <script> 
     function date(n){ 
     // Calculate the year. 
     var year = Math.round(n/365.2425 - 0.5); 
     // Get the remainder of the year calculation. 
     var dec = Math.round(((n * 10000)/365.2425) - (year * 10000)); 
     // Multiply remainder with 365.2425. 
     var day = Math.round((dec * 365.25)/10000); 
     // Search which month and day it is. 
     if(day <= 30){ 
      return day.toString().concat(" januari ").concat(year); 
     }else if(day <= 58){ 
      return (day - 30).toString().concat(" februari ").concat(year.toString()); 
     }else if(day <= 89){ 
      return (day - 58).toString().concat(" maart ").concat(year.toString()); 
     }else if(day <= 119){ 
      return (day - 89).toString().concat(" april ").concat(year.toString()); 
     }else if(day <= 150){ 
      return (day - 119).toString().concat(" mei ").concat(year.toString()); 
     }else if(day <= 180){ 
      return (day - 150).toString().concat(" juni ").concat(year.toString()); 
     }else if(day <= 211){ 
      return (day - 180).toString().concat(" juli ").concat(year.toString()); 
     }else if(day <= 242){ 
      return (day - 211).toString().concat(" augustus ").concat(year.toString()); 
     }else if(day <= 272){ 
      return (day - 242).toString().concat(" september ").concat(year.toString()); 
     }else if(day <= 303){ 
      return (day - 272).toString().concat(" oktober ").concat(year.toString()); 
     }else if(day <= 333){ 
      return (day - 303).toString().concat(" november ").concat(year.toString()); 
     }else if(day <= 364){ 
      return (day - 333).toString().concat(" december ").concat(year.toString()); 
     }else{ 
      return "error"; 
     } 
     } 
    </script> 
    </head> 
    <body> 
    <script> 
     document.write(date(736205)); 
    </script> 
    </body> 
</html> 

謝謝大家的幫助。

2

根據該文件在這裏:http://www.erlang.org/doc/man/calendar.html

二郎基地公曆天0 1 1月,具有劃時代意義,並提供了一個例子:1970年1月1日是719528.因此劃時代的一天0.

以下函數將Date對象轉換爲公曆和公曆。他們根據文檔中的單個示例返回正確的值,但將736202轉換爲2015年8月27日,而不是8月28日。也許您使用UTC而不是本地時間。無論如何,我認爲這裏有足夠的空間來解決問題。

/* @param {Date} [date] - Date object to be converted 
 
** @returns {number} - whole days since 1 January 0 to d 
 
** 
 
** epoch date must set year separately as in many implementations 
 
** new Date(0,0,1) returns 1 Jan 1900, not 1 Jan 0000 
 
*/ 
 
function dateToGregorianDays(date) { 
 

 
    // Create a Date object for 0000-Jan-01 (months are zero based) 
 
    var epoch = new Date(0,0,1); 
 

 
    // Set the epoch to year 0 as in the above some browsers will 
 
    // create a date for 1900 not 0, even though 0 was passed in 
 
    epoch.setFullYear(0); 
 

 
    // Copy the passed in Date so it's not modified by next step 
 
    var e = new Date(+date); 
 

 
    // Set the time part of the copied date to 00:00:00, which 
 
    // helps to calculate whole days 
 
    e.setHours(0,0,0,0); 
 

 
    // In mathematic operations, dates are converted to their time value 
 
    // which is milliseconds, so get the difference in milliseconds between 
 
    // the two dates and divide by milliseconds per day. Round to remove 
 
    // fractional parts caused occasionally over daylight saving boundaries 
 
    // to get whole day count between the two dates and return it 
 
    return Math.round((e - epoch)/8.64e7); 
 
} 
 

 
/* @param {number} [days] - Gregorian day number 
 
** @returns {Date} - Based on whole days since 1 January 0 
 
** 
 
**  0 -> 1 Jan 0000 
 
** 719528 -> 1 Jan 1970 
 
*/ 
 
function gregorianDaysToDate(days) { 
 

 
    // Create a date for 0000-Jan-01 
 
    var epoch = new Date(0,0,1); 
 
    epoch.setFullYear(0); 
 

 
    // Add the number of days to the date 
 
    // epoch.getDate() could be replaced by 1 since that's what 
 
    // the date was set to just above 
 
    epoch.setDate(epoch.getDate() + days); 
 

 
    // Return the date 
 
    return epoch; 
 
} 
 

 
/* Simple function to return a date string as dd-MMM-yyyy 
 
** @param {Date} [date] - Date to format 
 
** @returns {string} - formatted string for date 
 
*/ 
 
function formatDateDMY(date) { 
 
    
 
    // Month names 
 
    var months = ['Jan','Feb','Mar','Apr','May','Jun', 
 
       'Jul','Aug','Sep','Oct','Nov','Dec']; 
 

 
    // Add leading zero to single digit days 
 
    // Get the month name for the month (zero indexed, 0 is Jan) 
 
    // Add leading zeros to years with less than 4 digits 
 
    // Use '-' as separator 
 
    return ('0' + date.getDate()).slice(-2) + '-' + 
 
     months[date.getMonth()] + '-' + 
 
     ('000' + date.getFullYear()).slice(-4); 
 
} 
 

 
// Create an alias for the above function to save typing 
 
var fd = formatDateDMY; 
 

 
// Gregorian days for 01-Jan-1970 
 
document.write(dateToGregorianDays(new Date(1970,0,1)) + '<br>'); // 719528 
 

 
// Gregorian calendar date for 719528 formatted as dd-MMM-yyyy 
 
document.write(fd(gregorianDaysToDate(719528)) + '<br>')   // 01 Jan 1970 
 

 
// Gregorian calendar date for 0 formatted as dd-MMM-yyyy 
 
document.write(fd(gregorianDaysToDate(0)) + '<br>');    // 01 Jan 0000 
 

 
// Gregorian calendar date for 736202 formatted as dd-MMM-yyyy 
 
document.write(fd(gregorianDaysToDate(736202)) + '<br>');   // 27 Aug 2015 
 

 
// Gregorian days for 28-Aug-2015 
 
document.write(dateToGregorianDays(new Date(2015,7,28)));   // 736203

+0

736202實際上是8月27日;) –

+0

我不明白代碼的作用以及它如何計算值。 –