/    Sign up×
Community /Pin to ProfileBookmark

Returning the name of the month

I have a snippet of code for creating a “Date Picker” that has an image of a calendar and you just choose the appropriate date and it fills in the form accordingly. The code works fine except that it returns a number for the month field and I need a month name (i.e. JAN, FEB, MAR, etc.) Here is the code:

<script language=”javascript”>

var thisPage = “ship_info_calendar.html”;
var formName = “ship_info”;
var field_name = “begin_date”;

monthsNames = Array( “”, “January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December” );

var tempString = location.search.substring( 1 );
if ( tempString.indexOf( “&” ) != -1 ) {
var temp1 = tempString.split( “&” );
var strField = temp1[ 0 ];
var temp2 = temp1[ 1 ];
var strData = temp2.split( “,” );
} else {
var strField = tempString;
var strData = tempString.split( “,” );
}

var strMonth = parseInt( strData[ 0 ], 10 );
var strYear = parseInt( strData[ 1 ], 10 );

var dateObject = new Date();
var thisMonth = dateObject.getMonth();
var thisYear = dateObject.getFullYear();
var curMonth = dateObject.getMonth();
var curDay = dateObject.getDate();
var curYear = dateObject.getFullYear();

if (( strMonth > 0 ) || ( strYear > 0 )) {
curMonth = strMonth;
curYear = strYear;
}

var today = new Date( curYear, curMonth, 1 );
var today = today.getDay();

function daysPerMonth( month, year ) {
days = 31;
if (( month == 4 )||( month == 6 )||( month == 9 )||( month == 11 )) {
days = 30;
} else if ( month == 2 ) {
if (((( year % 100 ) == 0 ) && (( year % 400 ) == 0 )) || ((( year % 100 )!=0 ) && (( year % 4 ) == 0 ))) {
days = 29;
} else {
days = 28;
}
}
return days;
}

function monthBack( month, year ) {
if ( month == 0 ) {
location.href = thisPage + “?” + strField + “&” + 11 + “,” + ( year – 1 );
} else {
location.href = thisPage + “?” + strField + “&” + ( month – 1 ) + “,” + year;
}
}

function monthForward( month, year ) {
if ( month == 11 ) {
location.href = thisPage + “?” + strField + “&” + “0” + “,” + ( year + 1 );
} else {
location.href = thisPage + “?” + strField + “&” + ( month + 1 ) + “,” + year;
}
}

function yearBack( month, year ) {
location.href = thisPage + “?” + strField + “&” + ( month ) + “,” + ( year – 1 );
}

function yearForward( month, year ) {
location.href = thisPage + “?” + strField + “&” + ( month ) + “,” + ( year + 1 );
}

function getYear( year ) {
retval = new String( year );
<!– –>retval = retval.slice( 2, 4 );
return retval;
}

function getMonth( month ) {
month++;
retval = new String( month );
<!– –>if ( retval < 10 ) {
<!– –>retval = “0” + retval;
switch(retval)
{
case ( retval = 1 ):
retval = “JAN”
break
case ( retval = 2 ):
retval = “FEB”;
break
case ( retval = 3 ):
retval = “MAR”;
break
case ( retval = 4 ):
retval = “APR”;
break
case ( retval = 5 ):
retval = “MAY”;
break
case ( retval = 6 ):
retval = “JUN”;
break
case ( retval = 7 ):
retval = “JUL”;
break
case ( retval = 8 ):
retval = “AUG”;
break
case ( retval = 9 ):
retval = “SEP”;
case ( retval = 10 ):
retval = “OCT”;
break
case ( retval = 11 ):
retval = “NOV”;
break
case ( retval = 12 ):
retval = “DEC”;
break
}
return retval;
}

function getDay( day ) {
retval = new String( day );
if ( retval < 10 ) {
retval = “0” + retval;
}
return retval;
}

function createCSS() {
var cssStyle = “”;
cssStyle = cssStyle + “”;
cssStyle = cssStyle + “<style type=text/css>”;
cssStyle = cssStyle + “A:link { COLOR:#101010;TEXT-DECORATION:none; }”;
cssStyle = cssStyle + “A:visited { COLOR:#101010;TEXT-DECORATION:none; }”;
cssStyle = cssStyle + “A:active { COLOR:#101010;TEXT-DECORATION:none; }”;
cssStyle = cssStyle + “A:hover { COLOR: #101010;TEXT-DECORATION:none; }”;
cssStyle = cssStyle + “.Numeric { FONT-FAMILY:Verdana,Arial,Helvetica;FONT-SIZE:10px;LINE-HEIGHT:16px; }”;
cssStyle = cssStyle + “.DaysLabel { FONT-FAMILY:Verdana,Arial,Helvetica;FONT-SIZE:11px;LINE-HEIGHT:16px;COLOR:#FF0000; }”;
cssStyle = cssStyle + “.TextLabel { FONT-FAMILY:Verdana,Arial,Helvetica;FONT-SIZE:12px;LINE-HEIGHT:16px; }”;
cssStyle = cssStyle + “</style>rn”;
return cssStyle;
}

…I attempted to place a case to return the name of the month based on the “retval” but now all I get is the number “12”?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisJan 27.2004 — When you need to compare two values, you use "==". When you assign a value to a variable, you use "=".
Copy linkTweet thisAlerts:
@oleragJan 27.2004 — Your "switch" syntax should look something like below. Don't

use the "assignment" operator and ALWAYS include

a "default" if no cases are found within.
[code=php]
switch(retval) {
case retval "1":
retval = "JAN"
break;
case retval "2":
retval = "FEB";
break;
default:
retval = "";
break;
}
[/code]

Since no cases are found, the last case is returned and,

in your case, that is "12" since no default is included.

A better way to do this can be to "prototype" these functions.

In this case, it could be of type String and you can also make

it of type Date to fullfil alot of your other functions.
×

Success!

Help @heprox spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...