2015-08-27 151 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

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