【问题标题】:javascript date to stringjavascript日期到字符串
【发布时间】:2011-08-20 07:38:30
【问题描述】:

这是我需要做的。

获取日期,转换为字符串并将其传递给第三方实用程序。 当我传递它时,来自库的响应将具有字符串格式的日期。所以,我需要将日期转换为字符串,如 20110506105524 (YYYYMMDDHHMMSS)

function printDate() {
    var temp = new Date();
    var dateStr = temp.getFullYear().toString() + 
                  temp.getMonth().toString() + 
                  temp.getDate().toString() +
                  temp.getHours().toString() + 
                  temp.getMinutes().toString() + 
                  temp.getSeconds().toString();

    debug (dateStr );
}

上面的问题是在 1-9 个月,它打印一个数字。如何将其更改为准确打印 2 位月份、日期...

【问题讨论】:

标签: javascript string date


【解决方案1】:

如果是单个数字,则需要用“0”填充并注意 getMonth 返回 0..11 而不是 1..12

function printDate() {
    var temp = new Date();
    var dateStr = padStr(temp.getFullYear()) +
                  padStr(1 + temp.getMonth()) +
                  padStr(temp.getDate()) +
                  padStr(temp.getHours()) +
                  padStr(temp.getMinutes()) +
                  padStr(temp.getSeconds());
    debug (dateStr );
}

function padStr(i) {
    return (i < 10) ? "0" + i : "" + i;
}

【讨论】:

    【解决方案2】:

    依赖JQuery Datepicker,不过也可以轻松搞定:

    var mydate = new Date();
    $.datepicker.formatDate('yy-mm-dd', mydate);
    

    【讨论】:

    【解决方案3】:

    使用这个 polyfill https://github.com/UziTech/js-date-format

    var d = new Date("1/1/2014 10:00 am");
    d.format("DDDD 'the' DS 'of' MMMM YYYY h:mm TT");
    //output: Wednesday the 1st of January 2014 10:00 AM
    

    【讨论】:

      【解决方案4】:

      使用正则表达式和toJSON() 稍微简单一些。

      var now = new Date();
      var timeRegex = /^.*T(\d{2}):(\d{2}):(\d{2}).*$/
      var dateRegex = /^(\d{4})-(\d{2})-(\d{2})T.*$/
      var dateData = dateRegex.exec(now.toJSON());
      var timeData = timeRegex.exec(now.toJSON());
      var myFormat = dateData[1]+dateData[2]+dateData[3]+timeData[1]+timeData[2]+timeData[3]
      

      在撰写本文时给您"20151111180924"

      使用toJSON() 的好处是所有内容都已填充。

      【讨论】:

        【解决方案5】:

        也许将 Date 转换为实际整数 20110506105524 然后将其转换为字符串会更容易:

        function printDate() {
            var temp = new Date();
            var dateInt =
                ((((temp.getFullYear() * 100 + 
                    temp.getMonth() + 1) * 100 + 
                   temp.getDate()) * 100 +
                  temp.getHours()) * 100 + 
                 temp.getMinutes()) * 100 + 
                temp.getSeconds();
        
            debug ( '' + dateInt );  // convert to String
        }
        

        temp.getFullYear() &lt; 1000 时,结果将缩短一位(或多位)数字。

        注意:这不适用于毫秒精度(即 17 位),因为 Number.MAX_SAFE_INTEGER 是 9007199254740991,它只有 16 位。

        【讨论】:

          【解决方案6】:

          我喜欢 Daniel Cerecedo 使用 toJSON() 和正则表达式的回答。更简单的形式是:

          var now = new Date();
          var regex = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}).*$/;
          var token_array = regex.exec(now.toJSON());
          // [ "2017-10-31T02:24:45.868Z", "2017", "10", "31", "02", "24", "45" ]
          var myFormat = token_array.slice(1).join('');
          // "20171031022445"
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-10-06
            • 1970-01-01
            • 2012-10-08
            • 2023-03-16
            • 1970-01-01
            • 1970-01-01
            • 2016-04-02
            • 1970-01-01
            相关资源
            最近更新 更多