PDA

View Full Version : year.length problem



magik
12-23-2008, 12:05 PM
<script type="text/javascript">
var date = new Date();

//year
var year = date.getYear();
if (year.length == 4) {
var year1=year.charAt(2);
var year2=year.charAt(3);
}
else {
var year1="err";
var year2="or";
}
var theyear=year1+year2;
document.write(theyear);
</script>

This code is returning error however, when i replace:

var year = date.getYear();
with:

var year = "2008";
it returns "08"

when i do
document.write(year.length) I get an error as well.

What did I do wrong?
magik

vwphillips
12-23-2008, 12:44 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
</head>

<body>
<script type="text/javascript">
var date = new Date();

//year
var year = date.getFullYear().toString();
if (year.length == 4) {
var year1=year.charAt(2);
var year2=year.charAt(3);
}
else {
var year1="err";
var year2="or";
}
var theyear=year1+year2;
document.write(theyear);
</script>
</body>

</html>