4

我想得到以 yyyy-mm-dd 格式给出的两个日期之间的差异,差异应该是年份。

        var ds='2002-09-23';
        var today_date = new Date();
        alert(today_date);
        Date.prototype.yyyymmdd = function() {
        var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based
        var dd  = this.getDate().toString();
        var dt = yyyy +"-"+(mm[1]?mm:"0"+mm[0]) +"-"+ (dd[1]?dd:"0"+dd[0]);// padding
        var num_years = diff_date/31536000000;
        alert(num_years);
        if (num_years>18){
           alert (num_years);
        }else{
        alert ("i m not 18");
               }

请帮帮我。

4

8 回答 8

10

这要短得多:

var yearsApart = new Date(new Date - new Date('2002-09-23')).getFullYear()-1970

…但请注意通过提供正确的日期时间字符串来处理非 UTC 时区!

于 2014-04-19T14:45:31.577 回答
2

你不需要这个库,只需要纯 javascript:

function wholeYearsBetweenTwoDates(dateOneString, dateTwoString) {
    // assuming that dateTwo is later in time than dateOne
    var dateOne = getDateFromString(dateOneString);
    var dateTwo = getDateFromString(dateTwoString);

    var result = dateTwo.getFullYear() - dateOne.getFullYear();

    dateOne.setFullYear(dateTwo.getFullYear());
    if (dateOne > dateTwo) {
        // compensate for the case when last year is not full - e.g., when
        // provided with '2009-10-10' and '2010-10-09', this will return 0
        result -= 1;
    }

    return result;
}

function getDateFromString(stringDate) {
    var dateParts = stringDate.split('-');
    var result = new Date(dateParts[0], dateParts[1], dateParts[2]);
    return result;
}
于 2012-07-05T14:26:22.247 回答
1

首先,您必须选择一个 JavaScript 库来使用格式字符串解析日期(这样您就可以以您喜欢的格式提供日期)。试试这个很棒的库(至少你不必关心实现细节。构造函数和方法必须匹配,但它们可以解析该格式的简单日期并不是强制性的)。DateDate.parse

var date1 = getDateFromFormat("1999-10-10", "YYYY-MM-DD");
var date2 = getDateFromFormat("2012-10-10", "YYYY-MM-DD");

然后,当您必须计算差异时:

var millisecondsPerSecond = 1000;
var millisecondsPerMinute = millisecondsPerSecond * 60;
var millisecondsPerHour = millisecondsPerMinute * 60;
var millisecondsPerDay = millisecondsPerHour * 24;
var millisecondsPerYear = millisecondsPerDay * 365.26;

var years = Math.round((date2 - date1) / millisecondsPerYear);

如果您需要原始计算,您可以getFullYear()直接使用。

于 2012-07-05T14:22:42.623 回答
1

如果将日期转换为毫秒值,则可以更轻松地比较日期。

var birthday = new Date('2002-09-23');
var now = new Date();
var age = now.getTime() - birthday.getTime();

if (age < (1000 * 60 * 60 * 24 * 365 * 18)) { // number of milliseconds in 18 years
   document.write('not over 18');
} else {
  document.write('over 18');
}
于 2012-07-05T14:26:10.027 回答
1

尝试以下代码以获取年份差异...

function getDateDiffInYears(date1, date2) {
  var dateParts1 = date1.split('-')
    , dateParts2 = date2.split('-')
    , d1 = new Date(dateParts1[0], dateParts1[1]-1, dateParts1[2])
    , d2 = new Date(dateParts2[0], dateParts2[1]-1, dateParts2[2])

  return new Date(d2 - d1).getYear() - new Date(0).getYear() + 1;
}

var diff = getDateDiffInYears('2005-09-23', '2012-07-3');

console.log(diff); // => 7 years

祝你好运!

于 2012-07-05T14:29:40.723 回答
1

我一直在使用公式var yearsApart=milli/milliPerYear,但是当日期和月份相同时,四舍五入的值不正确。

在这里,您有我现在正在使用的脚本...

function yearDifferenceDates(firstDateDay, firstDateMonth, firstDateYear, secondDateDay, secondDateMonth, secondDateYear) {

    var fisrtDate   = new Date(firstDateYear, firstDateMonth - 1, firstDateDay);
    var secondDate  = new Date(secondDateYear, secondDateMonth - 1, secondDateDay);

    if(firstDateDay == secondDateDay && (firstDateMonth - 1) == (secondDateMonth - 1)) {
        return Math.round((secondDate-fisrtDate)/(1000*60*60*24*365.242199));
    }

    return Math.floor((secondDate-fisrtDate)/(1000*60*60*24*365.242199));
}
于 2014-03-18T11:59:29.933 回答
0

上面有一个小错误,但这项工作:)

NOT WORKING:   var millisecondsPerHour = millisecondsPerMinute = 60;
WORKING FINE:  var millisecondsPerHour = millisecondsPerMinute * 60;

但是谢谢阿德里亚诺·雷佩蒂

这里是完整的代码(带点格式)

var date1 = "01.01.2014";
var date2 = "31.12.2016";

var date1 = date1.split(".");
var date2 = date2.split(".");

date1 = String(date1[2] +"-"+ date1[1] +"-"+ date1[0]);
date2 = String(date2[2] +"-"+ date2[1] +"-"+ date2[0]);

var date1 = Date.parse(date1);
var date2 = Date.parse(date2);


//(Not for Europa :) )
//var date1 = Date.parse("2014-01-01");
//var date2 = Date.parse("2016-12-31");

var millisecondsPerSecond = 1000;
var millisecondsPerMinute = millisecondsPerSecond * 60;
var millisecondsPerHour = millisecondsPerMinute * 60;
var millisecondsPerDay = millisecondsPerHour * 24;
var millisecondsPerYear = millisecondsPerDay * 365.26;

// IN YEARS
var years = (date2 - date1) / millisecondsPerYear;

// IN MONTHS
var month = years * 12 // Very tricky, I know ;)
于 2014-10-13T21:38:35.303 回答
-2
var d1=new Date(2002, 9, 23);
var d2=new Date();

var milli=d2-d1;
var milliPerYear=1000*60*60*24*365.26;

var yearsApart=milli/milliPerYear;

console.log(yearsApart)
于 2012-07-05T14:16:32.883 回答