【问题标题】:Parse 'Date & Time' string in Javascript which are of custom format解析自定义格式的 Javascript 中的“日期和时间”字符串
【发布时间】:2015-03-16 03:18:59
【问题描述】:

我必须解析格式为“2015-01-16 22:15:00”的日期和时间字符串。我想将其解析为 JavaScript 日期对象。有什么帮助吗?

我尝试了一些 jquery 插件,moment.js、date.js、xdate.js。还是没有运气。

【问题讨论】:

标签: javascript jquery momentjs datejs datetime-parsing


【解决方案1】:

有点烦人,但不难自己编写一个没有任何依赖关系的解析方法。正则表达式非常适合根据一种或多种日期格式检查输入。不过,现在提供的格式中的日期“正常工作”。 IE。 new Date('2015-01-16 22:15:00') 在 Firefox 中为我工作。我这样做是为了一个看起来像“08.10.2020 10:40:32”的日期,这不起作用,也许提供的日期在某些浏览器中不起作用。但是这样你就可以在不依赖内置解析方法的情况下解析它。

function getAsDate(input) {
    if (!input) return null;
    if (input instanceof Date) return input;

    // match '2015-01-16 22:15:00'
    const regexDateMatch = '^[0-9]{4}\-[0-9]{1,2}\-[0-9]{1,2}\ [0-9]{2}\:[0-9]{2}\:[0-9]{2}$';
    if(input.match(regexDateMatch)) {
        const [year, month, day, hours, minutes, seconds] = input.split(/[-: ]/).map(x => parseInt(x));
        const date = new Date(year, month, day, hours, minutes, seconds);
        return date;
    }

    // Date formats supported by JS
    return new Date(input);
}

【讨论】:

    【解决方案2】:

    如果您确定它是所需的格式并且不需要错误检查,您可以使用 split 手动解析它(并且可以选择替换)。我需要在我的项目(MM/DD/YYYY HH:mm:ss:sss)中做类似的事情并修改我的解决方案以适应您的格式。 注意月减1。

    var str = "2015-01-16 22:15:00"; 
    //Replace dashes and spaces with : and then split on :
    var strDate = str.replace(/-/g,":").replace(/ /g,":").split(":");
    var aDate = new Date(strDate[0], strDate[1]-1, strDate[2], strDate[3], strDate[4], strDate[5]) ; 
    

    【讨论】:

      【解决方案3】:

      更好的解决方案,我现在使用 date.js - https://code.google.com/p/datejs/

      我在我的 html 页面中包含了这个脚本 -

      <script type="text/javascript" src="path/to/date.js"></script>
      

      然后我简单地解析了日期字符串“2015-01-16 22:15:00”,并将格式指定为,

      var dateString = "2015-01-16 22:15:00";
      var date = Date.parse(dateString, "yyyy-MM-dd HH:mm:ss");
      

      【讨论】:

      • 这对我来说失败了。这个 date.js 解析函数似乎只适用于单一格式。第二个参数被完全忽略。
      【解决方案4】:

      我正在尝试使用 moment.js 家伙。但是由于我遇到了这个错误,“ReferenceError: moment is not defined”,我现在不得不跳过它。我目前正在使用临时解决方法。

      function parseDate(dateString) {
          var dateTime = dateString.split(" ");
          var dateOnly = dateTime[0];
          var timeOnly = dateTime[1];
      
          var temp = dateOnly + "T" + timeOnly;
          return new Date(temp);
      }
      

      【讨论】:

        【解决方案5】:

        使用moment.js,您可以使用String+Format constructor 创建一个moment 对象:

        var momentDate = moment('2015-01-16 22:15:00', 'YYYY-MM-DD HH:mm:ss');
        

        然后,您可以使用 toDate() method 将其转换为 JavaScript 日期对象:

        var jsDate = momentDate.toDate();
        

        【讨论】:

        • 我尝试使用moment.js,但出现错误“ReferenceError: moment is not defined”。我也在我的html中设置了js路径。
        • 正如马特约翰逊所说,您必须遵循 instructions 才能在浏览器应用程序中使用 moment.js。
        【解决方案6】:
        new Date("2015-01-16T22:15:00")
        

        Date.parse()

        字符串必须采用 ISO-8601 格式。如果要解析其他格式,请使用moment.js

        moment("2015-01-16 22:15:00").toDate();
        

        【讨论】:

        • 这对我有用。我所做的是对您提供的建议进行了一些改进,我已在评论中发布了该解决方案。谢谢:)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-12
        • 1970-01-01
        • 1970-01-01
        • 2022-12-14
        • 1970-01-01
        • 2019-09-14
        相关资源
        最近更新 更多