簡體   English   中英

在Javascript中將字符串轉換為日期以返回JSON數組

[英]Converting String to Date in Javascript to return a JSON Array

我想將字符串轉換為日期對象,並在該日期后的最后三個月添加,以使用JavaScript形成季度報告。

例如,我使用以下命令接收以下字符串“ 2010年8月”

var currDate = "August-2010";

根據日期,我需要計算從當前日期開始的最后三個月,例如:2010年7月-2010年6月-2010年5月

最后,我需要將結果格式化為以下格式的數組:

[May,Jun,Jul,Aug];

我應該如何用Javascript來做?

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);

如果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 getLastThreeMonths = (function(){

    var months = [
            'Jan','Feb','Mar','Apr','May','Jun',
            'Jul','Aug','Sep','Oct','Nov','Dec'
        ],
        monthsAsObj = (function() {
            var r = {}, l = 12;
            while (l--) r[months[l]] = l;
            return r;
        }());

    return function getLastThreeMonths(date){

        var month = date.split('-')[0],
            monthIndex = monthsAsObj[month.slice(0,3)];

        return months.slice(monthIndex - 3, monthIndex);

    };

}());

getLastThreeMonths('August-2010'); // => ["May", "Jun", "Jul"]
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);
}

此函數返回最近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'));

http://jsfiddle.net/SLcNJ/4/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM