0
<script language="JavaScript">
<!-- Hide from older browser
var x= new Date()
var y= x.getYear()
var m= x.getMonth()+1  // added +1 because javascript counts month from 0
var d= x.getDate()
var h= x.getHours()
var mi= x.getMinutes()
var s= x.getSeconds()
document.write("Today's date is: ")
document.write(m+'/'+d+'/'+y+'  '+h+'.'+mi+'.'+s)
//-->
</script>

E.g Today date is 10 - 10 - 2011 ( d-m-Y) format When i tested in Firefox 6.02 and Chrome 14 .0385 and opera 10.53 safari 5 : Today's date is: 10/10/111 18.1.6 On internet explorer ie8 :Today's date is: 10/10/2011 18.3.47 ** i testing other thing.. but don't know why year getting wrong output except internet explorer.Or other browser had different implementation getting year. ?? ** print screen of the browser available if required.. link image http://imageshack.us/photo/my-images/502/javascriptdate.png/

1
  • Use the getFullYear() method instead
    – Smamatti
    Commented Oct 10, 2011 at 10:14

1 Answer 1

8

The getYear method returns the year minus 1900. Use getFullYear:

var y= x.getFullYear()

In some versions of Javascript the getYear method returns a two digit year for years in the 1900-1999 range, and a four digit year outside that range. This applies to Javascript 1.2 and earlier, and all versions of JScript (i.e. Internet Explorer).

This inconsistency means that you would have to analyse the result and change it depending on what the year reasonably could be. If you can't limit the possible rangle of years, it's impossible to tell if a result of 111 means 2011 or if it's actually year 111.

Reference:

http://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getYear

"getYear is no longer used and has been replaced by the getFullYear method."

http://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getFullYear

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.