SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast snorky's Avatar
    Join Date
    Jul 2003
    Location
    Great Northwet
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Solved: Date Display in IE vs FireFox

    The survey sez:

    It's a known, um, anomaly, in FireFox.

    Use function getFullYear() instead of getYear()

    That's all, folks!

    I have a simple piece of code that works as expected in IE6 and IE7. It displays strangely in FireFox 2x and Konqueror3.5

    The code:

    <script language="JavaScript" type="text/javascript">
    document.write(" OHSD 2002-" + cDate.getYear());
    </script>


    In IE it displays correctly:

    OHSD 2002-2007

    In FireFox and Konqueror, it displays in a bizarre fashion:

    OHSD 2002-107

    Considering that FF and Konqueror tend to be more standards-compliant than IE, I suspect it's bad code. In any case, what would be better code, or a fix, or a work-around?
    Last edited by snorky; Oct 9, 2007 at 12:22. Reason: Problem Solved
    Snorky
    Bless the little children while they're still too young to hate.
    - Tom T. Hall

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    getYear() never was Y2K compliant and so was deprecated in favour of getFullYear() back in 1999. Anyone still using getYear() is nine years behind in their knowledge of JavaScript.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When something works one way in IE and another way in the other browsers, why would you assume IE has it right?

    getFullYear and setFullYear are the correct syntax for reading or setting a year. It works the same in IE as in firefox and the other browsers.

  4. #4
    SitePoint Enthusiast snorky's Avatar
    Join Date
    Jul 2003
    Location
    Great Northwet
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mrhoo View Post
    When something works one way in IE and another way in the other browsers, why would you assume IE has it right?
    I didn't assume that IE has it right (I know better). I said that
    1. FF and Konqueror tend to be more standards-compliant than IE
    2. I assume it's bad code
    Snorky
    Bless the little children while they're still too young to hate.
    - Tom T. Hall

  5. #5
    SitePoint Enthusiast snorky's Avatar
    Join Date
    Jul 2003
    Location
    Great Northwet
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    getYear() never was Y2K compliant and so was deprecated in favour of getFullYear() back in 1999. Anyone still using getYear() is nine years behind in their knowledge of JavaScript.
    My 1500 page, brick-heavy JS reference guide was published in 2002. Even thought it's only 5 years behind, it doesn't mention any deprecation or any reason to favor one function over the other. My other 5 books - including a 2006 (1 year behind) SitePoint book - are silent on getYear vs. getFullYear

    You're being generous in saying that I'm only 9 years behind.

    Now I know why I find ways to use PHP instead of JS: the correct information about PHP is far easier to find... and the denizens are friendlier.
    Snorky
    Bless the little children while they're still too young to hate.
    - Tom T. Hall

  6. #6
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    JavaScript date differences between browsers

    The only Date difference is the getYear method.

    Per the ECMAScript specification (which is the specification JavaScript follows), the method is not Y2k-compliant, and running new Date().getYear() in 2004 will return "104".

    Per the ECMAScript specification, getYear returns the year minus 1900, originally meant to return "98" for 1998. getYear was deprecated in ECMAScript Version 3 and replaced with getFullYear().

    Internet Explorer changed getYear() to work like getFullYear() and make it Y2k-compliant, while Mozilla kept the standard behavior.
    Quoted from
    http://www-128.ibm.com/developerwork...y/wa-ie2mozgd/

    I suppose you could put this in your code-

    Date.prototype.getYear=Date.prototype.getFullYear;
    Date.prototype.setYear=Date.prototype.setFullYear;

    But it would be better to just use getFullYear and setFullYear.

  7. #7
    SitePoint Guru
    Join Date
    Mar 2004
    Location
    Earth
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    JavaScript Anthology makes this point On page 145:

    getFullYear Returns the year as a four-digit number. There is also a getYear method, but that returns a two-digit number and is not Y2K safe


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •