【问题标题】:Difference (in days) between two days in yyyy-mm-dd format?yyyy-mm-dd 格式的两天之间的差异(以天为单位)?
【发布时间】:2016-03-14 12:13:27
【问题描述】:

很抱歉,如果以前有人问过这个问题,但我找不到任何东西。这基本上是我想要做的:

new Date(response.departureDate).getTime() - new Date(response.arrivalDate).getTime()

我需要计算到达日期和离开日期之间的总天数(始终是整数)。这些日期是字符串,结构为'YYYY-MM-DD'

我该怎么做?

【问题讨论】:

  • 你必须使用moment js更好的方式来解决你的问题
  • 我有 jsfiddle 演示检查它非常有用

标签: javascript date datetime


【解决方案1】:

您可以使用正则表达式或更简单的方法是熟悉MomentJS API,它可以让您非常顺利地在 JS 中处理日期(适用于 Node 和浏览器)

http://momentjs.com/

它确实在您的工具箱中添加了另一个工具,但是一旦您操纵日期,恕我直言,这绝对值得。

使用 MomentJS 的方法:

var depDate = moment(response.departureDate);
var arrDate = moment(response.arrivalDate);
var nbDays = depDate.diff(arrDate, 'days');

【讨论】:

  • 最好加一些代码。现在它只是一个链接的答案,链接可能会腐烂。
  • 现在好点了吗?
  • 现在更好...:)
【解决方案2】:

看看Miles的回答here 只需将其更改为:

function parseDate(str) {
var mdy = str.split('-')
return new Date(mdy[2], mdy[0]-1, mdy[1]);
}

function daydiff(first, second) {
    return Math.round((second-first)/(1000*60*60*24));
}

并使用:

daydiff(parseDate(response.departureDate), parseDate(response.arrivalDate));

【讨论】:

    【解决方案3】:

    您可以使用RegEx 更改它们。

    new Date(response.departureDate.replace(/-/g, "/")).getTime()
        - new Date(response.arrivalDate.replace(/-/g, "/")).getTime()
    

    因此,RegEx .replace(/-/g, "/") 会将所有 - 替换为 /,JavaScript 将能够正确读取它。

    【讨论】:

    • 好的,但区别类似于86400000。如何将这样的日期参考转换为天,Math.ceil?
    • @JohnDoe 86400000 毫秒 = /1000 = 86400 秒,然后 /(60 * 60 * 24) 天...将是 1 天。最终你需要做:date/(60 * 60 * 24 * 1000)
    • NO,不要使用 Date 构造函数(或 Date.parse)解析字符串,它非常不可靠。手动解析日期字符串(2行代码或使用库)。
    • @RobG 你有更好的建议吗? :)
    • 是的,可以使用 2 行函数安全一致地解析字符串,例如答案here.
    【解决方案4】:

    希望这个例子对你有所帮助

    除了 .diff(),您还可以使用持续时间:http://momentjs.com/docs/#/durations/

    小提琴示例https://jsfiddle.net/abhitalks/md4jte5d/

    示例片段:

    $("#btn").on('click', function(e) {
        var fromDate = $('#fromDate').val(), 
            toDate = $('#toDate').val(), 
            from, to, druation;
    
        from = moment(fromDate, 'YYYY-MM-DD'); // format in which you have the date
        to = moment(toDate, 'YYYY-MM-DD');     // format in which you have the date
    
        /* using duration */
        duration = moment.duration(to.diff(from)).days(); // you may use duration
    
        /* using diff */
        //duration = to.diff(from, 'days')     // alternatively you may use diff
    
        /* show the result */
        $('#result').text(duration + ' days');
    
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.1/moment.min.js"></script>
    From: <input id="fromDate" type='date' />
    To: <input id="toDate" type='date' />&nbsp;&nbsp;
    <button id="btn">Submit</button><hr />
    <p id="result"></p>
    

    【讨论】:

      【解决方案5】:

      你可以使用这样的东西

      function countDays(date1, date2)
      {
          var one_day=1000*60*60*24;
          return Math.ceil((date1.getTime()- date2.getTime()) /one_day);
      }
      
      countDays(new Date(response.departureDate), new Date(response.arrivalDate));
      

      【讨论】:

      • NO,不要使用 Date 构造函数(或 Date.parse)解析字符串,它非常不可靠。手动解析日期字符串(2行代码或使用库)。
      • 我认为这取决于您是否可以信任响应:看起来 John Doe 可以,所以我不介意)
      • OP 正在征求意见。格式 'YYYY-MM-DD'(没有时区的 ISO 8601 日期)将被兼容 ECMAScript 2015 的浏览器解析为 UTC,这会让那些期望它被解析为本地的人感到惊讶。它不会被 IE 8 解析,它仍然有大约 8% 的使用率(这比桌面 Safari 的所有版本加起来还要多)。某些浏览器的某些版本会将其解析为本地。因此,即使格式得到保证,也无法进行适当的解析。
      猜你喜欢
      • 2019-03-11
      • 1970-01-01
      • 1970-01-01
      • 2011-03-12
      • 1970-01-01
      • 1970-01-01
      • 2012-09-18
      • 1970-01-01
      • 2019-01-07
      相关资源
      最近更新 更多