通过JavaScript获取年,月,日的两个日期之间的差异 [英] Obtain difference between two dates in years, months, days in JavaScript

查看:111
本文介绍了通过JavaScript获取年,月,日的两个日期之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我发现很多这样的问题,在这里试图获得两个日期在年,月和日之间的差异...但没有答案完成我的要求。



所以我写了一些计算,似乎有效,但也许这里的一些专家可以进行更正或帮助,使这更简单。

解决方案

所以这是我的功能,这收到两个日期,做所有的工作,并返回一个json与3个值,年,月和日。

  var DifFechas = {}; 

//两个日期之间的年份,月份和日期之间的差异
DifFechas.AMD = function(dIni,dFin){
var dAux,nAnos,nMeses,nDias,cRetorno
//最终日期始终大于初始
if(dIni> dFin){
dAux = dIni
dIni = dFin
dFin = dAux
}
//计算年
nAnos = dFin.getFullYear() - dIni.getFullYear()
//将初始日期翻译为最终
dAux = new的同一年日期(dIni.getFullYear()+ nAnos,dIni.getMonth(),dIni.getDate())
//检查我们必须休息一年,因为它没有满
如果(dAux> ; dFin){
--nAnos
}
//计算月份
nMeses = dFin.getMonth() - dIni.getMonth()
//我们添加月份不完整年份的一部分
如果(nMeses <0){
nMeses = nMeses + 12
}
//计算天数
nDias = dFin.getDate () - dIni.get Date()
//在日子里添加不完整月份的一部分
if(nDias< 0){
nDias = nDias + this.DiasDelMes(dIni)
}
//如果一天更大,我们退出月
if(dFin.getDate()< ; dIni.getDate()){
if(nMeses == 0){
nMeses = 11
}
else {
--nMeses
}
}
cRetorno = {años:nAnos,meses:nMeses,dias:nDias}
return cRetorno
}

DifFechas .DiasDelMes = function(date){
date = new Date(date);
return 32 - new Date(date.getFullYear(),date.getMonth(),32).getDate();
}

希望这有助于人们寻找解决方案。



这是另一个人做的新版本,似乎没有错误,希望这个更好地工作。


well, I found a lot of questions like that here trying to obtain the difference between 2 dates in years, month and days... But no answers that complete my requirement.

So i wrote something to calculate, it seems to work, but maybe some expert's here can make corrections or help to make this more simple.

解决方案

so this is my function, this receive two dates, do all the work and return a json with 3 values, years, months and days.

var DifFechas = {};

// difference in years, months, and days between 2 dates
DifFechas.AMD = function(dIni, dFin) {
    var dAux, nAnos, nMeses, nDias, cRetorno
    // final date always greater than the initial
    if (dIni > dFin) {
        dAux = dIni
        dIni = dFin
        dFin = dAux
    }
    // calculate years
    nAnos = dFin.getFullYear() - dIni.getFullYear()
    // translate the initial date to the same year that the final
    dAux = new Date(dIni.getFullYear() + nAnos, dIni.getMonth(), dIni.getDate())
    // Check if we have to take a year off because it is not full
    if (dAux > dFin) {
        --nAnos
    }
    // calculate months
    nMeses = dFin.getMonth() - dIni.getMonth()
    // We add in months the part of the incomplete Year
    if (nMeses < 0) {
        nMeses = nMeses + 12
        }
    // Calculate days
    nDias = dFin.getDate() - dIni.getDate()
    // We add in days the part of the incomplete month
    if (nDias < 0) {
        nDias = nDias + this.DiasDelMes(dIni)
    }
    // if the day is greater, we quit the month
    if (dFin.getDate() < dIni.getDate()) {
        if (nMeses == 0) {
            nMeses = 11
        }
        else {
            --nMeses
        }
    }
    cRetorno = {"años":nAnos,"meses":nMeses,"dias":nDias}
    return cRetorno
}

DifFechas.DiasDelMes = function (date) {
    date = new Date(date);
    return 32 - new Date(date.getFullYear(), date.getMonth(), 32).getDate();
}

hope this help people looking for a solution.

this is a new version that other guy did, seems to have no erros, hope this works better

这篇关于通过JavaScript获取年,月,日的两个日期之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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