Javascript - checkDate

时间:2017-03-10 10:03:46

标签: javascript

如何将第一个函数的参数(yearSlipt)用于i的第二个函数,可以使用户输入的年份与当前年份之间存在差异。

function checkDate(date,anneSplit){
    var date = new Array();
    for(i = 0; i < 3 ; i++){
        date[i] = prompt("entrer un date dd/mm/yyyy : ");
        console.log(date[i]);
    }

    for(var i = 0; i < date.length ; i++)
    {           
        daySplit = parseInt(date[i].split("/")[0]);
        monthSplit = parseInt(date[i].split("/")[1]);
        yearSplit = parseInt(date[i].split("/")[2]);

    }
    return (daySplit,monthSplit,yearSplit);
    console.log(daySplit,monthSplit,yearSplit);
}

function age(ageToday,yearSplit){

    checkDate();
    var ageToday = new Array();
    var d = new Date();
    var year = d.getFullYear();
    console.log(year);

    ageToday = year - yearSplit;
    document.write(ageToday);

}

2 个答案:

答案 0 :(得分:1)

对于多返回值,可以使用array或json对象。

&#13;
&#13;
function checkDate(date, anneSplit) {
  var date = new Array();
  for (i = 0; i < 3; i++) {
    date[i] = prompt("entrer un date dd/mm/yyyy : ");
    console.log(date[i]);
  }

  for (var i = 0; i < date.length; i++) {
    daySplit = parseInt(date[i].split("/")[0]);
    monthSplit = parseInt(date[i].split("/")[1]);
    yearSplit = parseInt(date[i].split("/")[2]);

  }

  return {
    daySplit: daySplit,
    monthSplit: monthSplit,
    yearSplit: yearSplit
  };
}


function age(ageToday, yearSplit) {

  var checkResult = checkDate();

  console.log(checkResult.yearSplit);

  // var ageToday = new Array();
  // var d = new Date();
  // var year = d.getFullYear();

  // ageToday = year - yearSplit;
  //document.write(ageToday);

}

age();
&#13;
&#13;
&#13;

答案 1 :(得分:0)

问题:该函数只返回一个日期,但它是由用户提供的3个日期

 function checkDate(date) {
 var date = new Array();
  for (i = 0; i < 3; i++) {
  date[i] = prompt("entrer un date dd/mm/yyyy : ");
  console.log(date[i]);
 }

 for (var i = 0; i < date.length; i++) {
 daySplit = parseInt(date[i].split("/")[0]);
 monthSplit = parseInt(date[i].split("/")[1]);
 yearSplit = parseInt(date[i].split("/")[2]);
 }
 return {
 daySplit: daySplit,
 monthSplit: monthSplit,
 yearSplit: yearSplit
 };     
 }


function age(yearSplit) {

var checkResult = checkDate();
var ageToday = new Array();
var d = new Date();
var year = d.getFullYear();

for(var i = 0; i < 3 ;i++)
{
    ageToday[i] = year - checkResult.yearSplit; 
    console.log(ageToday);
}
}
age();