GMT和欧洲格式的日期之间 [英] Days between dates with GMT and europe format

查看:152
本文介绍了GMT和欧洲格式的日期之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问了一千次,但是我试图让javascript来显示两个日期之间的日子。我看过这篇文章:如何获取JavaScript中两个日期之间的天数

I know this question has been asked a thousand times, but im trying to get javascript to display me the days between two dates. I have seen this post:How do i get the number of days between two dates in javascript

从我使用此代码的其中一个注释:

From one of the comments I have used this code:

<input type="text" name="sod" class="startd" value="10/02/2016" />
<input type="text" name="dos" class="endd"  value="12/02/2016" />

  <script>
  function treatAsUTC(date) {
      var result = new Date(date);
      result.setMinutes(result.getMinutes() - result.getTimezoneOffset());
      return result;
  }

  function daysBetween(startDate, endDate) {
      var millisecondsPerDay = 24 * 60 * 60 * 1000;
      return (treatAsUTC(endDate) - treatAsUTC(startDate)) / millisecondsPerDay;
  }

  alert(daysBetween($('.startd').val(), $('.endd').val()));
  </script>

从javascript得到61天,但是我想以dd / mm / yyyy的形式读取不是当前的mm / dd / yyyy,所以结果应该是2天。

The reulst from the javascript give 61 days, but I want it to read as dd/mm/yyyy not mm/dd/yyyy as it currently is, so the result should be 2 days.

我已经尝试删除treatAsUTC部分,但是它根本不给任何答案

I have tried removing the treatAsUTC parts but it then desont give any answer at all.

  function daysBetween(startDate, endDate) {
      var millisecondsPerDay = 24 * 60 * 60 * 1000;
      return (endDate - startDate) / millisecondsPerDay;
  }

  alert(daysBetween($('.startd').val(), $('.endd').val()));

可以帮助还是引导我正确的方向?

can any one help or guide me in the right direction?

提前感谢

Ian

推荐答案


来自javascript的reulst给了61天,但是我希望它像dd / mm / yyyy一样读不是目前的mm / dd / yyyy,所以结果应该是2天。 p>

The reulst from the javascript give 61 days, but I want it to read as dd/mm/yyyy not mm/dd/yyyy as it currently is, so the result should be 2 days.

所以你只需要正确解析日期。原来是:

So you just need to parse the date correctly. The original was:

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

以m / d / y格式解析日期,因此支持d / m / y,只需将最后一行更改为:

Which parses a date in m/d/y format, so to support d/m/y, just change the last line to:

    return new Date(mdy[2], mdy[1]-1, mdy[0]);

整理代码一点,你可能会得到如下结果:

Tidying up the code a bit, you might end up with something like:

// Parse a date in d/m/y format as UTC
function treatAsUTC(s) {
  var b = s.split(/\D/);
  return new Date(Date.UTC(b[2], b[1]-1, b[0]));
}

function daysBetween(startDate, endDate) {
  startDate = treatAsUTC(startDate);
  endDate = treatAsUTC(endDate);
  return (endDate - startDate) / 8.64e7;
}

function calcDiff() {
  document.querySelector('#result').value = 
     (daysBetween(document.querySelector('#sod').value,
      document.querySelector('#dos').value));
}

<input type="text" id="sod" class="startd" value="10/02/2016" />d/m/y
<input type="text" id="dos" class="endd"  value="12/02/2016" />d/m/y
<br>
<button onclick="calcDiff()">Calculate difference in days</button>
<input type="text" id="result" readonly>Days

但是你实际上并不需要UTC,你可以圆满结果。如果解析日期,毫秒将只会由于夏令时的变化而变化,四舍五入到最近一整天会修复。但是我猜这是使用UTC,因为它不需要四舍五入。

But you don't actually need UTC, you can just round the result. If parsing dates, the milliseconds will only ever be out by the daylight saving change, rounding to the nearest whole day fixes that. But I guess it's nice using UTC as it doesn't need rounding.

这篇关于GMT和欧洲格式的日期之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆