[thelist] JavaScript date problem

kristina kristina at kfx-design.co.uk
Wed Jan 2 10:32:17 CST 2002


on Wednesday, January 02, 2002, 4:19:40 PM, kristina wrote:

::
>   The client wants the date on his website eg:
>   Good Afternoon it's 4:14 pm on Wednesday the 2nd of January 2002
::
Figured it out myself so

in case anyone else is interested in how to do this -

<script language="JavaScript" type="text/javascript">
  <!--
    var Days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
    var Months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
    var DaySub = new Array('1st','2nd','3rd','4th','5th','6th','7th','8th','9th','10th','11th','12th','13th','14th','15th','16th','17th','18th','19th','20th','21st','22nd','23rd','24th','25th','26th','27th','28th','29th','30th','31st');

    var today = new Date();
    var Year = takeYear(today);
    var Month = leadingZero(today.getMonth()+1);
    var DayName = Days[today.getDay()];
    var Month = Months[today.getMonth()];
    var Day = leadingZero(today.getDate());
    var Day = DaySub[today.getDay()];
    var Hours = today.getHours();
    Hours2 = Hours;
    Hours = leadingZero(Hours);
    var ampm = "am";
    if (Hours2 > 11)
      ampm = "pm";
    if (Hours2 > 12)
      Hours2 -= 12;
    if (Hours2 == 0) Hours2 = 12;
    Hours2 = leadingZero(Hours2);
    var Minutes = leadingZero(today.getMinutes());
    var Seconds = leadingZero(today.getSeconds());
    
    function takeYear(theDate)
    {
      x = theDate.getYear();
      var y = x % 100;
      y += (y < 38) ? 2000 : 1900;
      return y;
    }
    
    function leadingZero(nr)
    {
      if (nr < 10) nr = "0" + nr;
      return nr;
    }
    
    now = new Date                           
    
    if (now.getHours() < 12) {
      document.write ('<Good Morning it\'s ' + Hours2 + ':' + Seconds + ' ' + ampm + ' on ' +  DayName + ' ' + 'the' + ' ' +  Day  + ' ' + 'of'  + ' ' +  Month + ' ' + Year)
    }
    else if (now.getHours() < 18) {
      document.write ('Good Afternoon it\'s ' + Hours2 + ':' + Seconds + ' ' + ampm + ' on ' +  DayName + ' ' + 'the' + ' ' +  Day  + ' ' + 'of'  + ' ' +  Month + ' ' + Year)
    }
    else {
      document.write ('Good Evening it\'s ' + Hours2 + ':' + Seconds + ' ' + ampm + ' on ' +  DayName + ' ' + 'the' + ' ' +  Day  + ' ' + 'of'  + ' ' +  Month + ' ' + Year)
    }
  //-->
</script>

If anyone can see anything wrong with the above - please do let me
know

-- 
hth
 kristina                      

kristina at kfx-design.co.uk





More information about the thelist mailing list