【问题标题】:Getting different outputs using getMonth()使用 getMonth() 获得不同的输出
【发布时间】:2018-08-31 01:11:45
【问题描述】:

我正在尝试使用此功能来验证用户是否有 18 年或以上,但我在使用 getMonth() 时出现了不稳定的行为。即如果我输入 1,则 ir 返回 0,但如果我输入 2,则返回 2:

function validacion() {
var anio = parseInt($('#year').val()); //year
var mes = parseInt($('#month').val()); //month
var dia = parseInt($('#day').val()); //day
var convertirMes = mes-1;
var ObjFechaFicticia = new Date();
var setearAnioFicticio = ObjFechaFicticia.setFullYear(anio); //year user input
var setearMesFicticio = ObjFechaFicticia.setMonth(convertirMes); //month user input
var setearDiasFicticio = ObjFechaFicticia.setDate(dia); //day user input

var ObjFechaActual = new Date();
var anioActual = ObjFechaActual.getFullYear();
var mesActual = ObjFechaActual.getMonth()+1;
var diaActual = ObjFechaActual.getDate();

console.log("Fecha real: "+diaActual+'/'+mesActual+'/'+anioActual)
console.log("Fecha ficticia :"+ObjFechaFicticia.getDate()+"/"+ObjFechaFicticia.getMonth()+"/"+ObjFechaFicticia.getFullYear())

}

【问题讨论】:

标签: javascript validation date object


【解决方案1】:

这是因为您更改日期对象的顺序...今天,2018 年 8 月 31 日

假设您想输入 2018 年 2 月 4 日的日期

const outputDate = d => {
  console.log(`${d.getFullYear()}, ${d.getMonth() + 1}, ${d.getDate()}`);
}
var d = new Date();
outputDate(d); // d is 2018, 8, 31
d.setFullYear(2001);
outputDate(d); // d is 2000, 8, 31 
d.setMonth(1); // set february
outputDate(d); // d is 2000, 3, 3 // 3 is March!!
d.setDate(4);
outputDate(d); // d is 2000, 3, 4 // 3 is March!!
console.log(d+'');

现在,如果您更改日期,然后更改月份,然后更改年份

const outputDate = d => {
  console.log(`${d.getFullYear()}, ${d.getMonth() + 1}, ${d.getDate()}`);
}
var d = new Date();
outputDate(d);
d.setDate(4);
outputDate(d);
d.setMonth(1); // set february
outputDate(d);
d.setFullYear(2001);
outputDate(d);
console.log(d+'');

当然,你应该这样做

 var ObjFechaFicticia = new Date(anio, convertirMes, dia);

一切都会很甜蜜

【讨论】:

    【解决方案2】:

    您的部分问题在这里:

    var ObjFechaFicticia = new Date();
    var setearAnioFicticio = ObjFechaFicticia.setFullYear(anio); //year user input
    var setearMesFicticio = ObjFechaFicticia.setMonth(convertirMes); //month user input
    var setearDiasFicticio = ObjFechaFicticia.setDate(dia); //day user input
    

    当您执行setYear时,如果日期设置的年份中不存在日期编号,则滚动到下个月,例如

    var d = new Date(2016, 1, 29); // 29 Feb 2016
    d.setFullYear(2017); 
    

    2017 年没有 2 月 29 日,因此日期滚动到 3 月 1 日。现在,当您设置月份时,您将获得该月的第一天,而不是您可能期待的 29 号。

    setMonth 也会发生同样的事情,所以即使您从 2018 年 8 月 31 日到 2017 年 8 月 31 日,如果您将月份设置为 6 月,它也会滚动到 7 月 1 日,并且当您将日期设置为 7 月时,而不是您可能预期的 6 月。

    解决方案是一次性设置所有部件。而且由于您不需要返回的值,所以不要存储它:

    ObjFechaFicticia.setFullYear(anio, convertirMes, dia);
    

    现在,只有在新月份不存在 dia 时,您才会遇到问题。希望您已经对此进行了排序。

    但无论如何你都不需要使用 set 方法,只需将值直接传递给 Date 构造函数即可:

    var ObjFechaFicticia = new Date(anio, convertirMes, dia);
    

    这避免了所有这些问题。您的代码可能是:

    function $(s) {
      return document.querySelector(s);
    }
    
    window.onload = function() {
      document.getElementById('calcButton').addEventListener('click', validacion, false);
    }
    
    function validacion() {
      var anio = parseInt($('#year').value); //year
      var mes = parseInt($('#month').value); //month
      var dia = parseInt($('#day').value); //day
      var convertirMes = mes-1;
      var ObjFechaFicticia = new Date(anio, convertirMes, dia); //day user input
    
      var ObjFechaActual = new Date();
      var anioActual = ObjFechaActual.getFullYear();
      var mesActual = ObjFechaActual.getMonth()+1;
      var diaActual = ObjFechaActual.getDate();
    
      console.log("Fecha real: "+diaActual+'/'+mesActual+'/'+anioActual);
      console.log("Fecha ficticia :"+ObjFechaFicticia.getDate()+"/"+ObjFechaFicticia.getMonth()+"/"+ObjFechaFicticia.getFullYear());
    }
    Year: <input value="2016" id="year"><br>
    Month: <input value="02" id="month"><br>
    Day: <input value="29" id="day"><br>
    <button id="calcButton">Do stuff</button>

    【讨论】:

      猜你喜欢
      • 2022-10-14
      • 1970-01-01
      • 2020-11-24
      • 1970-01-01
      • 2019-09-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-25
      • 2015-04-09
      相关资源
      最近更新 更多