我想将字符串转换为日期对象,并在日期的最后三个月内添加,以使用javascript形成季度报告。
例如,我使用以下
收到以下字符串“August-2010”var currDate = "August-2010";
根据日期,我需要计算当前日期的最后三个月,例如: 七月2010 六月2010 可以-2010
最后,我需要将该结果格式化为以下格式的数组:
[May,Jun,Jul,Aug];
我应该如何在Javascript中完成?
答案 0 :(得分:0)
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);
答案 1 :(得分:0)
如果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>
答案 2 :(得分:0)
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"]
答案 3 :(得分:0)
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);
}
答案 4 :(得分:0)
此函数返回最后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'));