【问题标题】:How can ı use loops in my datetime function?我如何在我的日期时间函数中使用循环?
【发布时间】:2020-02-06 08:02:01
【问题描述】:

我手头有一些。我需要显示此数据为当月前 11 个月的数据。

我是新js,我需要成为这个stution loop。

我的蒙特数组:

month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";

例如二月;

var mydata:[month[3],month[4],month[5],month[6],month[7],month[8],month[9],month[10],month[11],month[12],month[0],month[1]]

例如九月;

var mydata:[month[10],month[11],month[12],month[0],month[1],month[2],month[3],month[4],month[5],month[6],month[7],month[8]]

【问题讨论】:

  • 不完全确定您要达到的目标,但如果您尝试迭代几个月,您可以让一些初始值是 x(例如,如果您希望开始是二月,让x=2),然后使用 x=(x+1)%12(即当 x+1 = 12 时,12%12 会给你 0)

标签: javascript loops date datetime for-loop


【解决方案1】:

您可以对这个% 使用模数来找到提醒。所以你永远不会超出你的月份数组的范围。

这里有一个简单的例子:

const month = [];
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";

const getMonths = (fromMonth) => {
  const mydata = [];
  for(let i = 2; i <= 12; i++) { 
      mydata.push(month[(september + i) % 12]);
  }
  return mydata;
}
const september = 8;
console.log(getMonths(september));

【讨论】:

  • 是的,这段代码是正确的,谢谢,但是我改变一件事;常量 dte=新日期();常量 m1=dte.getMonth(); for(让 i = m1 ; i
  • 返回 12 个月,而不是 11 个月。
【解决方案2】:

我建议创建一个名为 newMonths 的新数组来重新排序月份。 这将是一个递归函数。 希望对你有帮助。

<script >

var d = new Date();
var n = d.getMonth();
var month = []; 

month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";

var newMonths = []; 

nextMonths =( current ) =>{
	if( newMonths.length===12  ){
	return null; 
	}else{
		let c = current < 12 ? current : 0 ;   
		let t = month[ c ] ;

	newMonths.push( t ); 
	return nextMonths( c + 1 ) ; 
	}

} 


	 nextMonths( n + 1 );
	 
	 console.log(  "current month  is"  , month[ n]  ); 
	 console.log(  "new Array "  , newMonths   );
	 
</script> 

【讨论】:

    【解决方案3】:

    循环不是必需的,您可以从当前月份 + 1 拼接到月底数组,然后将月份从开始添加到当前月份 - 1,例如

    /*
    ** @param {number} monthNumber - calendar month number in range 1 to 12
    ** @returns {Array} array of months from monthNumber + 1
    **                  to monthNumber - 1
    */
    function getMonthArray(monthNumber) {
      let month = [,"January", "February", "March", "April",
        "May", "June", "July", "August", "September",
        "October", "November", "December"];
      return month.splice(monthNumber + 1).concat(month.splice(1, monthNumber - 1));
    }
    
    console.log(getMonthArray(1));  // for January
    console.log(getMonthArray(2));  // for February
    console.log(getMonthArray(6));  // for June
    console.log(getMonthArray(12)); // for December      

    在月份数组的开头需要一个省略号,以防止在拼接到 monthNumber - 1 时尝试访问负索引。

    输入的月份数可以改为零索引,但我认为日历索引更合适。

    如果你想向它传递一个 Date 对象(并使用零索引月份),那么:

    /*
    ** @param {Date} date - date to use for month number
    ** @returns {Array} array of months from month + 1 to month - 1
    */
    function getMonthArray(date) {
      let m = date.getMonth();
      let month = [,"January", "February", "March", "April",
         "May", "June", "July", "August", "September",
         "October", "November", "December"];
      return month.splice(m+2).concat(month.splice(1, m));
    }
    
    console.log(getMonthArray(new Date(2020, 0)));  // for January
    console.log(getMonthArray(new Date(2020, 1)));  // for February
    console.log(getMonthArray(new Date(2020, 5)));  // for June
    console.log(getMonthArray(new Date(2020, 11))); // for December      
    console.log(getMonthArray(new Date())); // current month

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-18
      • 1970-01-01
      相关资源
      最近更新 更多