...

View Full Version : Javascript DateFormat ( 6 month window)



korssane
08-17-2011, 04:36 PM
Hi Guys ,

i am wondering how i can get the current 6 month windows like the following

always based onthe current month


Aug-11
Sep-11
Oct-11
Nov-11
Dec-11
Jan-12


thanks!

venegal
08-17-2011, 05:05 PM
var monthSpan = 6;
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

var date = new Date();
date.setDate(1);

var output = "";
for (var i = 0; i < monthSpan; i++) {
output += months[date.getMonth()] + '-' + (date.getFullYear() % 100) + '\n';
date.setMonth(date.getMonth() + 1);
}

alert(output);

korssane
08-17-2011, 05:23 PM
Thank you so much venegal for the help ,

i will figure out how to assign each month a specific variable...

thanks again

korssane
08-17-2011, 05:25 PM
Do you mind another question. i am stack on something else ..
iam unable to get the value of a dynamic generated dropdownlist

i can get it on firefox but not IE..

any idea ?

thanks

venegal
08-17-2011, 05:28 PM
For this one, you should probably post your code, so we can see why your method wouldn't work in IE.

korssane
08-17-2011, 06:05 PM
OK i ll do ,
thanks budd. i will create a new thread and hope you will see it..lol

Old Pedant
08-18-2011, 03:11 AM
i will figure out how to assign each month a specific variable...


Don't do it!

Use an array and assign the months to elements of the array.

Example, based on Venegal's code:


var monthSpan = 6;
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

var date = new Date();
date.setDate(1);

var namedMonths = []; // this is the array of names!
for (var i = 0; i < monthSpan; i++) {
namedMonts[i] = months[date.getMonth()] + '-' + (date.getFullYear() % 100);
date.setMonth(date.getMonth() + 1);
}

Now you have a nice array, where namedMonths[0] is the current month and (for example) namedMonths[5] is the sixth month.

MUCH better than trying to create and use 6 separate variables.

korssane
08-18-2011, 04:48 AM
already did it and it works great

thanks a bunch guys.

korssane
08-19-2011, 03:54 PM
Hi Guys,
i got another issue when trying to insert those values into oracle db ..
it seems it does not recognize mmm-yy format

and gives me ORA-01843: not valid month
my Oracle field is a date filed type.

Is there a way to leave the actual format and just create another one with date type ?
or just give it the 'mm/dd/yyyy' format with mask 'mmm-yy'

thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum