Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript返回过去6个月,然后将每个月用作变量_Javascript - Fatal编程技术网

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

Javascript返回过去6个月,然后将每个月用作变量,javascript,Javascript,希望有人能帮忙 我有下面的函数来返回最后6个月的名称(即6月、7月、8月等),但是,我无法确定如何将返回的每个月的名称作为单独的变量使用 我需要这个,这样我就可以将每个月的名称输入到一个sql查询中,该查询填充过去6个月的表 非常感谢您的帮助 function getMonths() { var today = new Date(); var month = 1; var currMonth = month-3;

希望有人能帮忙

我有下面的函数来返回最后6个月的名称(即6月、7月、8月等),但是,我无法确定如何将返回的每个月的名称作为单独的变量使用

我需要这个,这样我就可以将每个月的名称输入到一个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());
函数getMonths()
{
var today=新日期();
var月=1;
var currMonth=第3个月;
var monthArray=新数组(“一月”、“二月”、“三月”、“四月”、“五月”、“六月”,
“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”);
var menuMonths=新数组();
var计数=6;
var缓冲区=10;
而(计数>0)
{           
如果(当前月<0)
当月+=12;
如果(当前月>=12)
月份-=12;
var月=蒙塔雷[当前月];
每月推送(月);
currMonth=currMonth-1;
计数=计数-1;
}       
return(menumonhs.reverse());
} 
console.log(getMonths());
var months=getMonths();
对于(var i=0,len=months.length;i
var months=getMonths();
对于(var i=0,len=months.length;i您可以使用

小提琴

代码

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))
}
希望对您有所帮助

您可以使用

小提琴

代码

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查询,您使用的是服务器代码。您应该在服务器端或数据库端执行此操作…@Bartdude他可以在SQLite DB上使用它客户端
将每个返回的月份名称用作分离变量
很简单,但很可能不是您需要的。不要这样做。如果您运行的是sql查询,您使用的是服务器代码。您应该在服务器端或数据库端执行此操作…@Bartdude他可以在SQLite DB上使用它客户端
tx.executeSql("SELECT * FROM tablename WHERE month = ? OR month = ? month = ? OR month = ? month = ? OR month = ?;", [slicedMonthArray], successCB, errorCB);