【问题标题】:Javascript Converting IETF Date to ISO8601 FormatJavascript 将 IETF 日期转换为 ISO8601 格式
【发布时间】:2011-03-24 03:40:58
【问题描述】:

我正在使用这个很棒的 jQuery 日历插件

http://arshaw.com/fullcalendar/

点击日期的选项之一是返回点击日期的回调函数。

http://arshaw.com/fullcalendar/docs/mouse/dayClick/

我相信它只会以这种 IETF 格式返回日期 - Wed, 18 Oct 2009 13:00:00 EST

但是,我需要它以 ISO861 格式发布数据。我似乎在谷歌上找不到任何东西。我正在尝试将其转换为 Javascript。如果没有,那么转换可以在 Java 后端进行。感谢您的帮助

【问题讨论】:

    标签: javascript datetime date data-conversion


    【解决方案1】:

    来自fine manual:

    date 保存当天的 Date 对象。

    强调我的。

    我猜您看到的 IETF 格式只是 Date 对象的默认字符串化。 Date class 具有 getYear()、getMonth() 和 getDate() 方法,因此如果您注意零填充,您可以轻松生成 ISO-8601 日期字符串。

    【讨论】:

      【解决方案2】:

      由于 dayClick 已经为您提供了一个 Javascript Date 对象,您可以使用 this function from the Mozilla site. 将其格式化为 8601 格式的日期字符串

      【讨论】:

        【解决方案3】:

        它返回一个常规的 javascript 日期对象。你可以像这样使用 fullcalendar formatDate 实用函数:

        $.fullCalendar.formatDate(date, 'u');
        

        full docs for formatDate

        【讨论】:

          【解决方案4】:

          您可以使用属于您的 Date 对象 dayClick 的 formatDate 函数!

          function getGMTOffset(localDate) {
              var positive = (localDate.getTimezoneOffset() > 0);
              var aoff = Math.abs(localDate.getTimezoneOffset());
              var hours = Math.floor(aoff / 60);
              var mins = aoff % 60;
              var offsetTz = padzero_(hours) + ':' + padzero_(mins);
              // getTimezoneOffset() method returns difference between (GMT) and local time, in minutes.
              // example, If your time zone is GMT+2, -120 will be returned.
              // This is why we are inverting sign
              if (!positive) {
                return '+' + offsetTz;
              }
              return '-' + offsetTz;
          }
          
          function pad2zeros(n) {
            if (n < 100) {
                n = '0' + n;
            }
            if (n < 10) {
                n = '0' + n;
            }
            return n;
          }
          function padzero(n) {
              return n < 10 ? '0' + n : n.toString();
          }
          
          function formatDate(date)  {
            if (date) {
              return (date.getFullYear()) +
                     '-' + padzero((date.getMonth() + 1)) +
                     '-' + padzero(date.getDate()) +
                     'T' + padzero(date.getHours()) +
                     ':' + padzero(date.getMinutes()) +
                     ':' + padzero(date.getSeconds()) +
                     '.' + pad2zeros(date.getMilliseconds()) +
                     getGMTOffset(date);
            }
            return '';
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-11-23
            • 2023-03-16
            • 1970-01-01
            • 1970-01-01
            • 2014-01-25
            相关资源
            最近更新 更多