【问题标题】:How to convert gregorian days to gregorian date如何将公历日期转换为公历日期
【发布时间】:2015-11-22 12:09:47
【问题描述】:

我在我的表中存储了公历日和一些其他数据。 这些天我通过以下 Erlang 命令获得:

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

我们以这个值为例:GDays = 736202。

我已经用 ErlyDTL 在 Cowboy 中建立了一个网站,供我参考。 现在我想以日期格式呈现这些公历。 (2015 年 8 月 28 日)。

我使用以下代码从我的视图中的列表中获取我的数据:

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

我尝试了以下命令{{item.1|date:" D d M Y"}},但出现错误:

意外的日期参数:736202

现在如何在 erlyDTL 或 javascript 中将此公历日期转换为日期时间?

提前致谢

【问题讨论】:

  • 这里有计算公历日期的信息:Converting Gregorian Date to Gregorian Days。如果您在实施算法时需要帮助,请询问。一旦你有了一个 Date 对象,这里有很多关于如何格式化它的问题。
  • 如何以相反的方式执行此操作,并且天数从第 0 年开始计算(公历)。我有您参考中正在计算的天数。现在我想将该数字计算回日期戳。
  • 当我执行 736202 / 365.25 时,我得到 2015.6112251882273。现在我被卡住了,因为我不知道如何度过几个月和几天。
  • 公历的历元应该是 1582 年 10 月 15 日。加上 736,202 天得到 3598 年 6 月 10 日。如果你使用 0001 年 1 月 1 日的历元,那么你会得到 2016 年 8 月 27 日。减去 a如果从第 0 年开始,则为年(这实际上没有意义)。大多数地方都警告不要在公历开始之前计算公历日期,尽管我猜你可以使用任何你喜欢的纪元。
  • 我不明白这个数学,因为公历从 0000 年 1 月 1 日开始。Erlang 给我(见上面的命令)一个数字,它是自 0000 年 1 月 1 日以来的天数。我想你'是在儒略历中(我正确吗?)。你能给我这个反向计算的算法,以便我更容易理解吗?

标签: javascript datetime django-templates erlang


【解决方案1】:

这是我需要的算法。我以一种非常简单的方式在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

    Erlang 以 1 月 1 日 0 的纪元为公历日,并提供了一个示例: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

    【讨论】:

    • 数字 736202 实际上是 8 月 27 日 ;)
    • 我不明白代码的作用以及它如何计算值。
    猜你喜欢
    • 2023-03-30
    • 1970-01-01
    • 2023-03-20
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    相关资源
    最近更新 更多