Javascript Date (Year) - Stack Overflow

<script language="JavaScript"><!-- Hide from older browservar x= new Date()var y=

<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 .png/

<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/

Share Improve this question asked Oct 10, 2011 at 10:11 Hafizan AzizHafizan Aziz 2691 gold badge3 silver badges13 bronze badges 1
  • Use the getFullYear() method instead – Smamatti Commented Oct 10, 2011 at 10:14
Add a ment  | 

1 Answer 1

Reset to default 7

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/en/JavaScript/Reference/Global_Objects/Date/getYear

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745446711a4628079.html

相关推荐

  • Javascript Date (Year) - Stack Overflow

    <script language="JavaScript"><!-- Hide from older browservar x= new Date()var y=

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信