2日期之间的JavaScript日期返回错误 [英] Javascript days between 2 dates return wrong

查看:124
本文介绍了2日期之间的JavaScript日期返回错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试解决这个问题一段时间没有成功,我在这里找到了几个类似的答案,但格式在这里很重要。我需要返回X年,X个月,X天。



你可以看看看看,我在这里做错什么...天数不是相当正确。



这是一个 bin



(),(),(),,,,,,,,,,,,,,var var var var var var var var var var var var var var var var var var var var var var var var var var var var var var var var var var today_date = new Date(); var diff_date =(user_date - today_date); var num_years = diff_date / 31536000000; var num_months =(diff_date%31536000000)/ 2628000000; var num_days =((diff_date%31536000000)%2628000000)/ 86400000; var years = Math.floor(Math.abs(num_years)); var months = Math.floor(Math.abs(num_months)); var days = Math.floor(Math.abs(num_days)); if(years> = 1){console.log(years +years+ months +months+ days +days); } else if(years< = 0&& month> = 0){console.log(months +months+ days +days); } else {console.log(days +days); }} inBetweenDays(2015,03,04); inBetweenDays(2016,03,04); inBetweenDays(2016,02,04); inBetweenDays(2018,02,04);

解决方案

计算两个日期之间的日历日期差异的算法,几个月,天数如下:


  1. 验证日期为全年(4位数),范围为1-12个月,以及日期范围内的1个月的月日。


  2. 从较晚的日期减去较早的日期。如果需要借款,请在较早日期的年份之前的较早日期的日期添加日期,然后添加1至数月以减去。


  3. 从较晚的日期月份减去较早的日期月份。如果需要借款,请添加12到较晚的几个月,并加1到几年才能减去。


  4. 从较晚的日期年度减去较早的日期年份。


这可能是在一系列重复问题的一个现有答案中实现的,但尝试发现没有文档的算法。借款日数的计算可以计算如下:

  function monthDays(y,m)// full year and month in范围1-12 
{var leap = 0;
if(m == 2)
{if(y%4 == 0)leap = 1;
if(y%100 == 0)leap = 0;
if(y%400 == 0)leap = 1;
}
return [0,31,28,31,30,31,30,31,31,30,31,30,31] [m] + leap;
}

(编辑),或者根据评论,使用Date对象调整的行为超出预期的绑定行为:

  function monthDays(y,m)
{return new Date(y,m ,0).getDate();
}


Been trying to resolve this one for a while with no success, I found couple of similar answers here, but the format is what is important here. I need to return X years, X months, X days.

Can you take a look to see, what I am doing wrong here... Number of days are not quite right.

Here is a bin

function inBetweenDays(y,m,d){

var user_date = new Date(y,m + 1,d);
var today_date = new Date();

  
var diff_date = (user_date - today_date);

var num_years = diff_date/31536000000;
var num_months = (diff_date % 31536000000)/2628000000;
var num_days =  ((diff_date % 31536000000) % 2628000000)/86400000;
 
var years = Math.floor(Math.abs(num_years));
var months = Math.floor(Math.abs(num_months));  
var days =  Math.floor(Math.abs(num_days));
  
  if (years >= 1) {
    console.log(years + " years " + months + " months " + days + " days");
  } else if (years <= 0 && months >= 0){
     console.log(months + " months " + days + " days");
   } else {
     console.log(days + " days ");
   }
  
  }
  

inBetweenDays(2015,03,04);
inBetweenDays(2016,03,04);
inBetweenDays(2016,02,04);
inBetweenDays(2018,02,04);

解决方案

An algorithm to calculate the calendar date difference between two date in terms of years, months, days is as follows:

  1. validate dates to have full years (4 digits), months in range 1-12, and month-day in range 1 - days in month of year of date.

  2. subtract earlier date days from later date days. If borrow required, add days in the month of earlier date, in the year of the earlier date, to later date days and add 1 to months to be subtracted.

  3. subtract earlier date month from later date months. If borrow required, add 12 to later day months and add 1 to years to be subtracted.

  4. subtract earlier date year from later date year.

This may have been implemented in one existing answer to this question in a chain of duplicate questions, but try and spot it without documentation of the algorithm. The calculation of days to borrow can be calculated as follows:

function monthDays(y, m)    // full year and month in range 1-12
{   var leap = 0;
    if( m == 2)
    {   if( y % 4 == 0) leap = 1;
        if( y % 100 == 0) leap = 0;
        if( y % 400 == 0) leap = 1;
    }
    return [0, 31,28,31,30,31,30,31,31,30,31,30,31][ m] + leap;
}

(Edit) or alternatively as per comment, using the behavior of Date objects adjusting to out of expected bound behavior:

function monthDays( y, m)
{ return new Date(y, m, 0).getDate();
}

这篇关于2日期之间的JavaScript日期返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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