Date in Javascript is coming differently

I have a string 'dtPlanStart' which has the value : 5/16/11

WHEN CONVERTED TO DATE LIKE:

var newdtPlanStart = new Date(dtPlanStart)

I GET THE VALUE AS : 5/16/1911 but it is suppose to be 5/16/2011. What could be wrong

I EVEN DID THIS BUT STILL GET 1911 as the YEAR

newdtPlanStart = (newdtPlanStart.getMonth() + 1) + "/" + newdtPlanStart.getDate() + "/" + newdtPlanStart.getFullYear();
vmandemAsked:
Who is Participating?
 
leakim971Connect With a Mentor PluritechnicianCommented:
could you confirm?
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
a = "5/16/11";
var dtPlanStart = new Date(a);
var newdtPlanStart = new Date(dtPlanStart)
alert(newdtPlanStart);
newdtPlanStart = (newdtPlanStart.getMonth() + 1) + "/" + newdtPlanStart.getDate() + "/" + newdtPlanStart.getFullYear();
alert(newdtPlanStart);
</script>
</head>
<body>
</body>
</html>

Open in new window

0
 
hieloCommented:
Since you are not specifying 2011 explicitly, then 1900 is used as default. Thus 5/16/11 becomes 5/16/1911.

Try
var newdtPlanStart = new Date( dtPlanStart.replace(/(.+)(\d\d)\s*$/, "$120$2") )
0
 
hieloCommented:
@leakim971:
>>newdtPlanStart.getFullYear();
That will get you the 4 digit of the current date object. His problem is that the constructor sees/thinks 11 is 1911. So that is what he will get for newdtPlanStart.getFullYear()
0
 
leakim971PluritechnicianCommented:
OK, on Chrome & Safari I got 2011 and 1911 on IE, FF and Opera
0
 
vmandemAuthor Commented:
Resolved myself
0
All Courses

From novice to tech pro — start learning today.