gpt4 book ai didi

Javascript 返回过去 6 个月,然后使用每个月作为变量

转载 作者:行者123 更新时间:2023-11-28 08:44:53 25 4
gpt4 key购买 nike

希望有人能帮忙。

我有以下函数来返回最后 6 个月份名称(即六月、七月、八月等),但是,我无法弄清楚如何将每个返回的月份名称用作单独的变量。

我需要这个,这样我就可以将每个月份的名称输入到 SQL 查询中,以填充过去 6 个月的表。

非常感谢任何帮助。

function getMonths()
{
var today = new Date();
var month = 1;
var currMonth = month-3;

var monthArray = new Array("January","February","March","April","May","June",
"July","August","September","October","November","December");

var menuMonths = new Array();
var count = 6;
var buffer = 10;

while(count >0)
{
if (currMonth < 0)
currMonth += 12;
if (currMonth >=12 )
currMonth -= 12;

var month = monthArray[currMonth];
menuMonths.push(month);

currMonth = currMonth -1;
count = count -1;
}
return (menuMonths.reverse());
}


console.log (getMonths());

最佳答案

您可以使用Array.slice()

FIDDLE http://jsfiddle.net/BeNdErR/NF5Qm/1/

代码

var monthArray = new Array("January","February","March","April","May","June",
"July","August","September","October","November","December");

var currMonth = 3; // the current month index, from 1 to 12
var firstMonth = currMonth - 6;

if(firstMonth < 0){ //for example if currMonth is January - 0
var months = [];
months.push(monthArray.slice(12 - Math.abs(firstMonth), 12));
months.push(monthArray.slice(0, currMonth));
alert(months);
}else{
alert(monthArray.slice(firstMonth, currMonth))
}

作为输出,您仍然有一个数组,因此您可以将其按原样传递给 SQL 查询,例如 (SQLite):

tx.executeSql("SELECT * FROM tablename WHERE month = ? OR month = ? month = ? OR month = ? month = ? OR month = ?;", [slicedMonthArray], successCB, errorCB);

希望对你有帮助

关于Javascript 返回过去 6 个月,然后使用每个月作为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19976523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com