如何在yyyy-mm-dd的日期中获得差异? [英] how to get difference in year where dates are in yyyy-mm-dd?

查看:131
本文介绍了如何在yyyy-mm-dd的日期中获得差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i want to get the difference between two dates which are give in yyyy-mm-dd format difference should be in year.

        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");
               }

请帮帮我。

推荐答案

这要短得多:

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

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

… but be careful to take care of non UTC time zones by providing the correct datetime string!

这篇关于如何在yyyy-mm-dd的日期中获得差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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