I have problem to case value from calendar value. Ex : Input text value from calendar = "2013-08-25" then the script case like this:
<script>
function convertDate() {
var z = document.forms["form1"]["date_get"].value;
var month = z.substring(5,7);
switch (month) {
case 01:
x="I";
break;
case 02:
x="II";
break;
case 03:
x="III";
break;
case 04:
x="IV";
break;
case 07:
x="VII";
break;
case 08:
x="VIII";
break;
}
document.forms["form1"]["trans_code"].value = x;
}
</script>
Result Still I
.
The case statements are not working.
Try:
case "01":
x="I";
break;
case "02":
x="II";
break;
etc...
Note that since you comparing Strings - those values 01, 02 etc need to be represented as Strings "01","02" etc.
If you provide 01 without the quotes it will be interpreted as 1 (integer), which is often taken to mean "true". That's why it accepts the first case.
You forgot the single or double quotes for string 01 02 03 ...
var z = '2013-08-25';
var month = z.substring(5,7);
var x = '';
switch (month)
{
case '01':
x="I";
break;
case '02':
x="II";
break;
case '03':
x="III";
break;
case '04':
x="IV";
break;
case '07':
x="VII";
break;
case '08':
x="VIII";
break;
// etc.
}
alert(x);
use Quotes inside case statement like case "01"
A dict might be better than switch case:
var trans = {'01': 'I', '02': 'II'};
var month = '02';
alert(trans[month]);
see also: http://james.padolsey.com/javascript/how-to-avoid-switch-case-syndrome/
http://encosia.com/first-class-functions-as-an-alternative-to-javascripts-switch-statement/
Explicitly cast the substring into a number and be aware of octal notation in JavaScript for numbers starting with a zero.
<script>
function convertDate() {
var z = document.forms["form1"]["date_get"].value;
var month = +z.substring(5,7); //or parseInt(...)
switch (month) {
case 01:
x="I";
break;
case 02:
x="II";
break;
case 03:
x="III";
break;
case 04:
x="IV";
break;
case 07:
x="VII";
break;
case 08:
x="VIII";
break;
}
document.forms["form1"]["trans_code"].value = x;
}
</script>