Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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中将字符串转换为日期以返回JSON数组_Javascript_Json_Jsp_Date - Fatal编程技术网

在Javascript中将字符串转换为日期以返回JSON数组

在Javascript中将字符串转换为日期以返回JSON数组,javascript,json,jsp,date,Javascript,Json,Jsp,Date,我想将一个字符串转换为date对象,并添加从日期算起的最后三个月,以使用javascript形成季度报告 例如,我使用以下命令接收以下字符串“August-2010” var currDate = "August-2010"; 根据日期,我需要计算从当前日期算起的最后三个月,例如: 2010年7月至7月 2010年6月至6月 2010年5月-2010年 最后,我需要将结果格式化为以下格式的数组: [May,Jun,Jul,Aug]; 我应该如何用Javascript实现它?var Month

我想将一个字符串转换为date对象,并添加从日期算起的最后三个月,以使用javascript形成季度报告

例如,我使用以下命令接收以下字符串“August-2010”

var currDate = "August-2010";
根据日期,我需要计算从当前日期算起的最后三个月,例如: 2010年7月至7月 2010年6月至6月 2010年5月-2010年

最后,我需要将结果格式化为以下格式的数组:

[May,Jun,Jul,Aug];
我应该如何用Javascript实现它?

var Months=[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”];
var Months = ["Jan", "Feb", "Mar", "Apr", "May", "June", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

var d = new Date(Date.parse("August-2010"));
var CurrentMonth = d.getMonth();
var NumberOfMonthsBack = 4;
var Quarter = [];

for(x = 0; x < NumberOfMonthsBack; x++) {
    if(CurrentMonth == -1) {
        CurrentMonth = 11;
    }
    Quarter.unshift(Months[CurrentMonth--]);
}

alert(Quarter);
var d=新日期(日期解析(“2010年8月”); var CurrentMonth=d.getMonth(); var NumberOfMonthsBack=4; var季度=[]; 对于(x=0;x
如果currDate的格式是固定的,我想您必须处理字符串并输入一些If和else才能得到最终结果。比如说,

<script type="text/javascript">
    var monthArray = ["January","February","March","April","May","June","July","September","October","November","December"];
    var currDate = "August-2010";
    var month = currDate.substring(0,currDate.indexOf("-"));
    var year  = parseInt(currDate.substr(currDate.indexOf("-")+1));
    int i;
    for (i = 0 ; i < 12 ; i++) {
        if (month == monthArray[i]) break;
    }
    var resultArray = []; //This is the array that contain 4 months
    resultArray[3] = month.substring(0,3); //The last element of the array should be the month in currDate, I suppose the format is the 1st 3 letters of the month

    for (int j = 1 ; j < 4 ; j++) {
        var theYear = year;
        var k = i - j;
        if (k < 0) {
            k+=12;
            theYear--;
        }
        var theMonth = monthArray[k];
        resultArray[3-i] = theMonth.substring(0,3);
        alert(theMonth+"-"+theYear); // You get the 3 previous month here (e.g. July-2010, etc...)
    }

</script> 

var monthArray=[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“九月”、“十月”、“十一月”、“十二月”];
var currDate=“2010年8月”;
var month=currDate.substring(0,currDate.indexOf(“-”);
var year=parseInt(currDate.substr(currDate.indexOf(“-”)+1));
int i;
对于(i=0;i<12;i++){
如果(月=Monthharray[i])中断;
}
var resultArray=[]//这是包含4个月的数组
resultArray[3]=月子串(0,3)//数组的最后一个元素应该是currDate中的月份,我假设格式是该月份的前3个字母
对于(int j=1;j<4;j++){
年=年;
var k=i-j;
if(k<0){
k+=12;
年--;
}
var theMonth=Monthharray[k];
resultArray[3-i]=第个月的子串(0,3);
警报(月+“-”+年);//您在这里可以看到前三个月(例如2010年7月等)
}
var搜索、iFound、res、months=[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月];
var currDate=“2010年8月”;
//IE没有实现数组的indexOf,这里有一个解决方法:
if(!Array.prototype.indexOf){
Array.prototype.indexOf=函数(预期){
for(var i=0;i=0){//如果找到了月份
res=months.slice(iFound-3,iFound+1);//slice复制数组的一部分。这里,它从iFound-3复制到iFound+1(不包括)
控制台日志(res);
}

此函数返回最近X个月的月份+年份:

function getPastXMonths(theDate, X) {

    var d = new Date(Date.parse(theDate.replace('-', ' 1 ')));
    var out = [];
    var i = 0;

    while (i++ < X) {
        out.push([
        'January','February','March','April','May','June','July','August','September','October','November','December'
        ][d.getMonth()] + '-' + d.getFullYear());
        d.setMonth(d.getMonth() - 1);
    }

    return out;
}

var months = getPastXMonths('August-2010', 3);
alert(months.join('\n'));
函数getPasstxMonths(日期,X){
var d=新日期(Date.parse(Date.replace('-','1'));
var out=[];
var i=0;
而(i++

这是家庭作业吗?我在w3school代码测试页面中尝试了这一行:var d=new Date(Date.parse(“August-2010”)),但当我尝试“document.write(d.toString())”时,我得到了“invalid”,我刚刚在w3school上尝试过,效果很好。但实际上,在加载页面之后,不应该使用document.write()方法。只需使用alert()来回显简单的测试数据。
newdate(Date.parse(“August-2010”)
在所有浏览器中都不起作用。在firefox上试试吧。很好,J-P!这不是因为该方法不起作用,而是在某些浏览器中解析比其他浏览器更好。显然,firefox不如Chrome好。我仍然建议使用一个专用的解析库,以防日期的格式随时间发生变化。也许像@Gary这样的东西,它可能会改变吗?OP没有提到这种可能性。
var searched, iFound, res, months = [ 'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec' ];
var currDate = "August-2010";

// IE does not implement Arrays' indexOf, here is a workaround :
if(!Array.prototype.indexOf){
   Array.prototype.indexOf = function(expected){
     for(var i = 0; i < this.length; i++){
        if(this[i] === expected) return i;
     }
     return -1;
   }
}

searched = currDate.substring(0,3); // lets take the only 3 first letters of currDate
iFound = months.indexOf(searched); // we look for the index of the month in the array
if(iFound >= 0){ // if the month was found
   res = months.slice(iFound-3, iFound+1); // slice copies a portion of the array. Here, it copies from iFound-3 to iFound+1 (excluded)
   console.log(res);
}
function getPastXMonths(theDate, X) {

    var d = new Date(Date.parse(theDate.replace('-', ' 1 ')));
    var out = [];
    var i = 0;

    while (i++ < X) {
        out.push([
        'January','February','March','April','May','June','July','August','September','October','November','December'
        ][d.getMonth()] + '-' + d.getFullYear());
        d.setMonth(d.getMonth() - 1);
    }

    return out;
}

var months = getPastXMonths('August-2010', 3);
alert(months.join('\n'));