Javascript Reference Guide

 
 
This guide contains useful Javascript tips.

rss feed






Date and time.



Information
none


Code
<script type="text/javascript" language="JavaScript">
//<![CDATA[

now = new Date(); 
now_time = now.getTime();
function makeTime(time) { 
  now = new Date();
  zero = (new Date(now.getFullYear(),now.getMonth(),now.getDate(),0,0,0)).getTime();
  return zero + (Math.floor(time)*60*60*1000) + (time - Math.floor(time))*100*60*1000;
}

function showDate() {
  weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday",
  "Saturday");
  monthname=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct",
  "Nov","Dec");
  date = weekday[now.getDay()] + " " + now.getDate() + " " + 
  monthname[now.getMonth()] + " " + now.getFullYear();
  alert(date);
}

function showTime() {
  hour= now.getHours();
  minute = now.getMinutes();
  alert(hour + ":" + minute);
}

function setNewDate(newdate) {
  today = new Date();
  //parse requires: 'MonthName, DayNr Year Hrs:Min:Sec'
  today.setTime(Date.parse(newdate)); 
  return today.toLocaleString();
  
}

function isOfficeOpen(){
  start_time = makeTime(document.timeForm.otime.value);
  end_time = makeTime(document.timeForm.ctime.value);
  if ((now_time > start_time) && (now_time < end_time)) {
    alert("Office is open");
  } else {
    alert("Office is closed");
  }
}       

//]]>  
</script>   
  
  
<form name="timeForm" 
  action="../javascript_reference_guide/javascript_quickguide_date_and_time.html" >
  <table summary="" border="1">
  <tr>
    <td>Set the opening time (hh.mm)</td>
    <td><input type="text" name="otime" maxlength="5" size="5" value="8.30" /> </td>
  </tr>
  <tr>
    <td>Set the closing time (hh.mm)</td>
    <td> <input type="text" name="ctime" maxlength="5" size="5" value="17.00" /> </td>
  </tr> 
  </table>
  <br />
  <input type="button" onfocus="this.blur()" value="Is the office open?" 
  onclick="javascript:isOfficeOpen();return false;" />      
  <input type="button" onfocus="this.blur()" value="Show date" 
  onclick="javascript:showDate();return false;" />      
  <input type="button" onfocus="this.blur()" value="Show time" 
  onclick="javascript:showTime();return false;" />     
</form> 
<br /><br />
Methods available with the date() function:
<br /><br />
<script type="text/javascript" language="JavaScript">
  document.write("getDay() - Integer value of day of week (0 - 6) = " + 
  now.getDay() + "<br />");
  document.write("getUTCDay() - Integer value of day of week (0 - 6) = " + 
  now.getUTCDay() + "<br />");
  document.write("getDate() - Day of month = " + 
  now.getDate() + "<br />");
  document.write("getUTCDate() - Day of month = " + 
  now.getUTCDate() + "<br />");
  document.write("getMonth()  - Month of year (0 - 11) = " + 
  now.getMonth()  + "<br />");
  document.write("getUTCMonth() - Month of year (0 - 11) = " + 
  now.getUTCMonth() + "<br />");
  document.write("getFullYear() - Four digit year = " + 
  now.getFullYear()  + "<br />");
  document.write("getUTCFullYear() - Four digit year = " + 
  now.getUTCFullYear() + "<br />");
  document.write("getHours() - Hour of day (0 - 23) = " + 
  now.getHours() + "<br />");   
  document.write("getUTCHours() - Hour of day (0 - 23) = " + 
  now.getUTCHours() + "<br />");
  document.write("getMinutes() - Minutes into the hour = " + 
  now.getMinutes() + "<br />");
  document.write("getUTCMinutes() - Minutes into the hour = " + 
  now.getUTCMinutes()  + "<br />");
  document.write("getSeconds() - Seconds into the minute = " + 
  now.getSeconds() + "<br />");
  document.write("getUTCSeconds() - Seconds into the minute = " + 
  now.getUTCSeconds() + "<br />");
  document.write("getTime() - Milliseconds since midnight 1st January, 1970 = " + 
  now.getTime() + "<br />");
  document.write("getTimezoneOffset() - Minutes difference between local time and UTC = " 
  + now.getTimezoneOffset() + "<br />");
  document.write("toLocaleString() - Local date and time in string format = " + 
  now.toLocaleString() + "<br />");
  document.write("toString() - UTC date and time in string format = " + 
  now.toString() + "<br />");
  document.write("toGMTString() - GMT date and time in string format = " + 
  now.toGMTString() + "<br />");
  document.write("parse() - Reading in strings = " + 
  setNewDate('September, 4 2050 07:04:11') + "<br />");
</script> 
 
Result



Set the opening time (hh.mm)
Set the closing time (hh.mm)

       


Methods available with the date() function: