admin管理员组文章数量:1428982
<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
1 Answer
Reset to default 7The 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
本文标签: Javascript Date (Year)Stack Overflow
版权声明:本文标题:Javascript Date (Year) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745446711a2658695.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论