1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Here is working and corrected script for showing current date on website

Discussion in 'JavaScript' started by richardm55, Jun 10, 2012.

  1. #1
    Some time ago I have found script for showing present date on website. It worked properly for Mozilla but not for IE so I have corrected it. And here is below:

    DayName = new Array(7)
    DayName[0] = "Sunday "
    DayName[1] = "Monday "
    DayName[2] = "Tuesday "
    DayName[3] = "Wednesday "
    DayName[4] = "Thursday "
    DayName[5] = "Friday "
    DayName[6] = "Saturday "
    MonthName = new Array(12)
    MonthName[0] = "January "
    MonthName[1] = "February "
    MonthName[2] = "March "
    MonthName[3] = "April "
    MonthName[4] = "May "
    MonthName[5] = "June "
    MonthName[6] = "July "
    MonthName[7] = "August "
    MonthName[8] = "September "
    MonthName[9] = "October "
    MonthName[10] = "November "
    MonthName[11] = "December "
    function getDateStr(){
    var Today = new Date()
    var WeekDay = Today.getDay()
    var Month = Today.getMonth()
    var Day = Today.getDate()
    var Year = Today.getYear()
    if( navigator.appName !== "Microsoft Internet Explorer" ) {Year += 1900}
    return DayName[WeekDay] + "," + " " + Day + " " + MonthName[Month] + ", " + Year
    }

    As you know you can insert this script either in head section using script tag or in external js file While in body section in place you want the date to appear you will write down this html code: <script language="JavaScript">document.write("Today is " + getDateStr())</script>

    I hope this code will be helpful for some people.
     
    Last edited: Jun 10, 2012
    richardm55, Jun 10, 2012 IP
  2. ketting00

    ketting00 Well-Known Member

    Messages:
    772
    Likes Received:
    27
    Best Answers:
    3
    Trophy Points:
    128
    #2
    Nice, but hard to read.
     
    ketting00, Jun 11, 2012 IP
  3. rae081

    rae081 Member

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #3
    Yes it is a little difficult to read...
     
    rae081, Jun 11, 2012 IP
  4. richardm55

    richardm55 Active Member

    Messages:
    762
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    65
    #4
    I am sorry guys but it was not my fault. When I put this code it was ok but after sending it was formatted so stupidly by this program. Anyway I will try to do it again and I must think to do it in better way
     
    richardm55, Jun 11, 2012 IP
  5. richardm55

    richardm55 Active Member

    Messages:
    762
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    65
    #5
    I have corrected it already so it looks normally now. Sorry again for previous stupid look of it.
     
    richardm55, Jun 11, 2012 IP
  6. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #6
    You might want to learn to use code tags.
     
    Rukbat, Jun 16, 2012 IP
  7. richardm55

    richardm55 Active Member

    Messages:
    762
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    65
    #7
    What do you mean, Rukbat?
     
    richardm55, Jun 16, 2012 IP
  8. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #8
    
    DayName = new Array(7)
    DayName[0] = "Sunday "
    DayName[1] = "Monday "
    DayName[2] = "Tuesday "
    
    Code (markup):
    formats nicer than
    DayName = new Array(7)
    DayName[0] = "Sunday "
    DayName[1] = "Monday "
    DayName[2] = "Tuesday "

    Adding some blank lines and spaces in your code helps make it more easily readable too.
    
    DayName[6] = "Saturday "
    
    MonthName = new Array(12)
    MonthName[0] = "January "
    
    function getDateStr(){
       var Today = new Date();
       var WeekDay = Today.getDay();
       var Month = Today.getMonth();
       var Day = Today.getDate();
       var Year = Today.getYear();
    
       if( navigator.appName !== "Microsoft Internet Explorer" ) {Year += 1900}
    
       return DayName[WeekDay] + ", " + Day + " " + MonthName[Month] + ", " + Year
    }
    
    Code (markup):
    BTW, just checking to make sure that the year is greater than 1999, and adding 1900 if it's not, eliminates some code. (new Date() will never be less than 2000 again until we change the calendar.)
     
    Rukbat, Jun 16, 2012 IP
  9. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #9
    Better to use:
    
    var Year = Today.getFullYear();
    
    Code (markup):
    so that you eliminate the 19xx/20xx issue and makes the code reusable elsewhere.
     
    rainborick, Jun 17, 2012 IP
  10. richardm55

    richardm55 Active Member

    Messages:
    762
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    65
    #10
    You mean I just replace line var Year=Today.getYear(); with line var Year = Today.getFullYear(); and also I may remove next line if( navigator.appName !== "Microsoft Internet Explorer" ) {Year += 1900} because it is not needed now?
    I must check it if it really works on all browsers.
     
    richardm55, Jun 17, 2012 IP
  11. richardm55

    richardm55 Active Member

    Messages:
    762
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    65
    #11
    Yes, this code works properly. Thank you, mate for the correction.
     
    richardm55, Jun 17, 2012 IP
  12. richardm55

    richardm55 Active Member

    Messages:
    762
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    65
    #12
    I will finally correct the code in main thread on 11th July because I am not allowed to do that now.
     
    richardm55, Jun 17, 2012 IP
  13. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #13
    Lots of luck. You can check it on the most popular ones (IE [all versions back to 6 at least], Firefox, Chrome, Opera, Safari - and the popular ones used on Android phones), but there are a few hundred standard browsers and hundreds more that only a few people use.

    Knowing that at least one browser screwed the pooch on this one, I'd leave in the trivial code to make sure that the year was in the 3rd millennium.
     
    Rukbat, Jun 17, 2012 IP
  14. richardm55

    richardm55 Active Member

    Messages:
    762
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    65
    #14
    I will check it on the most popular browsers only. If it does not work on rest of them it is not big matter.
     
    richardm55, Jun 17, 2012 IP
  15. richardm55

    richardm55 Active Member

    Messages:
    762
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    65
    #15
    richardm55, Jun 17, 2012 IP
  16. Ev0Lv

    Ev0Lv Greenhorn

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #16
    I think there is a code PHP i guess that would get date for you.
     
    Ev0Lv, Jun 17, 2012 IP
  17. richardm55

    richardm55 Active Member

    Messages:
    762
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    65
    #17
    I don't see any sense using php code if this JavaScript code does this job.
     
    richardm55, Jun 18, 2012 IP
  18. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #18
    If the page is being generated by PHP, doing it iin PHP will speed up the page load. If it's an html page, Javascript is the proper choice.
     
    Rukbat, Jun 19, 2012 IP