Your Ad Here

Sunday, June 29, 2008

Javascript Time functions

The Date object has been created and now we have a variable that holds the current date. To get the information we need to print out the date we have to utilize some or all of the following functions:

  • getTime() - Number of milliseconds since 1/1/1970 @ 12:00 AM
  • getSeconds() - Number of seconds (0-59)
  • getMinutes() - Number of minutes (0-59)
  • getHours() - Number of hours (0-23)
  • getDay() - Day of the week(0-6). 0 = Sunday, … , 6 = Saturday
  • getDate() - Day of the month (0-31)
  • getMonth() - Number of month (0-11)
  • getFullYear() - The four digit year (1970-9999)

Today's Date

<!– hide from old browsers
document.write(getDateStr())
//–>
June 19, 2008

 
<SCRIPT Language="JavaScript">
<!-- hide from old browsers
  function GetMonth(intMonth){
    var MonthArray = new Array("January", "February", "March",
                               "April", "May", "June",
                               "July", "August", "September",
                               "October", "November", "December")
    return MonthArray[intMonth]
    }
  function getDateStr(){
    var today = new Date()
    var year = today.getYear()
    if(year<1000) year+=1900
    var todayStr = GetMonth(today.getMonth()) + " " + today.getDate()
    todayStr += ", " + year
    return todayStr
    }
//–>
</SCRIPT>

 
<SCRIPT Language="JavaScript">
<!-- hide from old browsers
    document.write(getDateStr())
//-->
</SCRIPT>

Today's Date including day of week

<!– hide from old browsers
document.write(getDateStrWithDOW())
//–>
Thursday, June 19, 2008

 
<SCRIPT Language="JavaScript">
<!-- hide from old browsers
  function GetDay(intDay){
    var DayArray = new Array("Sunday", "Monday", "Tuesday", "Wednesday",
                         "Thursday", "Friday", "Saturday")
    return DayArray[intDay]
    }
 
  function GetMonth(intMonth){
    var MonthArray = new Array("January", "February", "March",
                               "April", "May", "June",
                               "July", "August", "September",
                               "October", "November", "December")
    return MonthArray[intMonth]
    }
  function getDateStrWithDOW(){
    var today = new Date()
    var year = today.getYear()
    if(year<1000) year+=1900
    var todayStr = GetDay(today.getDay()) + ", "
    todayStr += GetMonth(today.getMonth()) + " " + today.getDate()
    todayStr += ", " + year
    return todayStr
    }
//–>
</SCRIPT>

 
<SCRIPT Language="JavaScript">
<!-- hide from old browsers
    document.write(getDateStrWithDOW())
//-->
</SCRIPT>

Todays Date (short format)

<!– hide from old browsers
var today = new Date()
var year = today.getYear()
if(year<1000) year+=1900

document.write((today.getMonth()+1) + "/" +
today.getDate() + "/" + (year+"").substring(2,4))
//–>
6/19/08

 
<SCRIPT Language="JavaScript">
<!-- hide from old browsers
  var today = new Date()
  var year = today.getYear()
  if(year<1000) year+=1900
 
  document.write((today.getMonth()+1) + "/" +
                  today.getDate() + "/" + (year+"").substring(2,4))
//-->
</SCRIPT>

Todays Date (short format, full year)

<!– hide from old browsers
var today = new Date()
var year = today.getYear()
if(year<1000) year+=1900

document.write((today.getMonth()+1) + "/" +
today.getDate() + "/" + year)
//–>
6/19/2008

 
<SCRIPT Language="JavaScript">
<!-- hide from old browsers
  var today = new Date()
  var year = today.getYear()
  if(year<1000) year+=1900
 
  document.write((today.getMonth()+1) + "/" +
                  today.getDate() + "/" + year)
//-->
</SCRIPT>

Todays Date (short format, leading zeros)

<!– hide from old browsers
var today = new Date()
var month = today.getMonth()+1
var year = today.getYear()
var day = today.getDate()
if(day<10) day = "0″ + day
if(month<10) month= "0″ + month
if(year<1000) year+=1900

document.write(month + "/" + day +
"/" + (year+"").substring(2,4))
//–>
06/19/08

 
<SCRIPT Language="JavaScript">
<!-- hide from old browsers
  var today = new Date()
  var month = today.getMonth()+1
  var year = today.getYear()
  var day = today.getDate()
  if(day<10) day = "0" + day
  if(month<10) month= "0" + month
  if(year<1000) year+=1900
 
  document.write(month + "/" + day +
                 "/" + (year+"").substring(2,4))
//-->
</SCRIPT>

Todays Date (short format, leading zeros, full year)

<!– hide from old browsers
var today = new Date()
var month = today.getMonth()+1
var year = today.getYear()
var day = today.getDate()
if(day<10) day = "0″ + day
if(month<10) month= "0″ + month
if(year<1000) year+=1900

document.write(month + "/" + day +
"/" + year)
//–>
06/19/2008

 
<SCRIPT Language="JavaScript">
<!-- hide from old browsers
  var today = new Date()
  var month = today.getMonth()+1
  var year = today.getYear()
  var day = today.getDate()
  if(day<10) day = "0" + day
  if(month<10) month= "0" + month
  if(year<1000) year+=1900
 
  document.write(month + "/" + day +
                 "/" + year)
//-->
</SCRIPT>

Todays Date (European format)

<!– hide from old browsers
var today = new Date()
var year = today.getYear()
if(year<1000) year+=1900

document.write(today.getDate() + "." +
(today.getMonth()+1) + "." + (year+"").substring(2,4))
//–>
19.6.08

 
<SCRIPT Language="JavaScript">
<!-- hide from old browsers
  var today = new Date()
  var year = today.getYear()
  if(year<1000) year+=1900
 
  document.write(today.getDate() + "." +
                 (today.getMonth()+1) + "." + (year+"").substring(2,4))
//-->
</SCRIPT>

Basic Date and Time

<!– hide from old browsers
var curDateTime = new Date()
document.write(curDateTime)
//–>
Thu Jun 19 2008 12:02:04 GMT+0530 (India Standard Time)

 
<SCRIPT Language="JavaScript">
<!-- hide from old browsers
  var curDateTime = new Date()
  document.write(curDateTime)
//-->
</SCRIPT>

Date and Time (24-hr format)

<!– hide from old browsers
var curDateTime = new Date()
document.write(curDateTime.toLocaleString())
//–>
Thursday, 19 June, 2008 12:02

 
<SCRIPT Language="JavaScript">
<!-- hide from old browsers
  var curDateTime = new Date()
  document.write(curDateTime.toLocaleString())
//-->
</SCRIPT>

Basic GMT Date and Time

<!– hide from old browsers
var curDateTime = new Date()
document.write(curDateTime.toGMTString())
//–>
Thu, 19 Jun 2008 06:32:04 GMT

 
<SCRIPT Language="JavaScript">
<!-- hide from old browsers
  var curDateTime = new Date()
  document.write(curDateTime.toGMTString())
//-->
</SCRIPT>

Time in 12-hr format

<!– hide from old browsers
var curDateTime = new Date()
var curHour = curDateTime.getHours()
var curMin = curDateTime.getMinutes()
var curSec = curDateTime.getSeconds()
var curAMPM = " AM"
var curTime = ""
if (curHour >= 12){
curHour -= 12
curAMPM = " PM"
}
if (curHour == 0) curHour = 12
curTime = curHour + ":"
+ ((curMin < 10) ? "0″ : "") + curMin + ":"
+ ((curSec < 10) ? "0″ : "") + curSec
+ curAMPM
document.write(curTime)
//–>
12:02:04 PM

 
<SCRIPT Language="JavaScript">
<!-- hide from old browsers
  var curDateTime = new Date()
  var curHour = curDateTime.getHours()
  var curMin = curDateTime.getMinutes()
  var curSec = curDateTime.getSeconds()
  var curAMPM = " AM"
  var curTime = ""
  if (curHour >= 12){
    curHour -= 12
    curAMPM = " PM"
    }
  if (curHour == 0) curHour = 12
  curTime = curHour + ":"
    + ((curMin < 10) ? "0" : "") + curMin + ":"
    + ((curSec < 10) ? "0" : "") + curSec
    + curAMPM
  document.write(curTime)
//-->
</SCRIPT>

Time in 24-hr format

<!– hide from old browsers
var curDateTime = new Date()
var curHour = curDateTime.getHours()
var curMin = curDateTime.getMinutes()
var curSec = curDateTime.getSeconds()
var curTime =
((curHour < 10) ? "0″ : "") + curHour + ":"
+ ((curMin < 10) ? "0″ : "") + curMin + ":"
+ ((curSec < 10) ? "0″ : "") + curSec
document.write(curTime)
//–>
12:02:04

 
<SCRIPT Language="JavaScript">
<!-- hide from old browsers
  var curDateTime = new Date()
  var curHour = curDateTime.getHours()
  var curMin = curDateTime.getMinutes()
  var curSec = curDateTime.getSeconds()
  var curTime =
    ((curHour < 10) ? "0" : "") + curHour + ":"
    + ((curMin < 10) ? "0" : "") + curMin + ":"
    + ((curSec < 10) ? "0" : "") + curSec
  document.write(curTime)
//-->
</SCRIPT>

Time for Specific Time Zone

<!– hide from old browsers
// Copyright 1999, 2000 by Ray Stott
// OK to use if this copyright is included
// Script available at http://www.crays.com/jsc
var TimezoneOffset = -8 // adjust for time zone
var localTime = new Date()
var ms = localTime.getTime()
+ (localTime.getTimezoneOffset() * 60000)
+ TimezoneOffset * 3600000
var time = new Date(ms)
var hour = time.getHours()
var minute = time.getMinutes()
var second = time.getSeconds()
var curTime = "" + ((hour > 12) ? hour - 12 : hour)
if(hour==0) curTime = "12″
curTime += ((minute < 10) ? ":0″ : ":") + minute
curTime += ((second < 10) ? ":0″ : ":") + second
curTime += (hour >= 12) ? " PM" : " AM"
document.write(curTime + " - US Pacific Time")
//–>
10:32:04 PM - US Pacific Time

 
<SCRIPT Language="JavaScript">
<!-- hide from old browsers
  // Copyright 1999, 2000 by Ray Stott
  // OK to use if this copyright is included
  // Script available at http://www.crays.com/jsc
  var TimezoneOffset = -8  // adjust for time zone
  var localTime = new Date()
  var ms = localTime.getTime()
             + (localTime.getTimezoneOffset() * 60000)
             + TimezoneOffset * 3600000
  var time =  new Date(ms)
  var hour = time.getHours()
  var minute = time.getMinutes()
  var second = time.getSeconds()
  var curTime = "" + ((hour > 12) ? hour - 12 : hour)
  if(hour==0) curTime = "12"
  curTime += ((minute < 10) ? ":0" : ":") + minute
  curTime += ((second < 10) ? ":0" : ":") + second
  curTime += (hour >= 12) ? " PM" : " AM"
  document.write(curTime + " US Pacific Time")
//-->
</SCRIPT>

GMT Time

<!– hide from old browsers
var curDateTime = new Date()
var curHour = curDateTime.getHours()
+ curDateTime.getTimezoneOffset()/60
if (curHour > 24) curHour -= 24
if (curHour < 0) curHour += 24
var curMin = curDateTime.getMinutes()
var curSec = curDateTime.getSeconds()
var curTime =
((curHour < 10) ? "0″ : "") + curHour + ":"
+ ((curMin < 10) ? "0″ : "") + curMin + ":"
+ ((curSec < 10) ? "0″ : "") + curSec
document.write(curTime + " GMT")
//–>
06.5:02:04 GMT

 
<SCRIPT Language="JavaScript">
<!-- hide from old browsers
  var curDateTime = new Date()
  var curHour = curDateTime.getHours()
     + curDateTime.getTimezoneOffset()/60
  if (curHour > 24)  curHour -= 24
  if (curHour < 0) curHour += 24
  var curMin = curDateTime.getMinutes()
  var curSec = curDateTime.getSeconds()
  var curTime =
    ((curHour < 10) ? "0" : "") + curHour + ":"
    + ((curMin < 10) ? "0" : "") + curMin + ":"
    + ((curSec < 10) ? "0" : "") + curSec
  document.write(curTime + " GMT")
//-->
</SCRIPT>

Offset from GMT

<!– hide from old browsers
var curDateTime = new Date()
document.write("GMT Offset for your time zone is ")
document.write(-(curDateTime.getTimezoneOffset()/60))
//–>
GMT Offset for your time zone is 5.5

 
<SCRIPT Language="JavaScript">
<!-- hide from old browsers
  var curDateTime = new Date()
  document.write("GMT Offset for your time zone is ")
  document.write(-(curDateTime.getTimezoneOffset()/60))
//-->
</SCRIPT>

Days till Y3K

<!– hide from old browsers
var today = new Date()
var targetDate = new Date("01/01/3000″) //use full year
var timeBeforeTarget = Math.floor(( targetDate.getTime()
- today.getTime()) / 86400000)
var msg = "<B>There are only " + (timeBeforeTarget +1)
+ " days until the year 3000.</B>"
document.write(msg)
//–>
There are only 362151 days until the year 3000.

 
<SCRIPT Language="JavaScript">
<!-- hide from old browsers
var today = new Date()
var targetDate = new Date("01/01/3000") //use full year
var timeBeforeTarget = Math.floor(( targetDate.getTime()
        - today.getTime()) / 86400000)
var msg = "<B>There are only "  + (timeBeforeTarget +1)
         + " days until the year 3000.</B>"
document.write(msg)
//-->
</SCRIPT>

Days after a Certain Date

<!– hide from old browsers
var today = new Date()
var targetDate = new Date("12/31/1999″) //use full year
var timeAfterTarget = Math.floor(( today.getTime()
- targetDate.getTime() ) / 86400000)
var msg = "This is day number " + timeAfterTarget + " for this year."
document.write(msg)
//–>
This is day number 3093 for this year.

 
<SCRIPT Language="JavaScript">
<!-- hide from old browsers
var today = new Date()
var targetDate = new Date("12/31/1999") //use full year
var timeAfterTarget = Math.floor(( today.getTime()
        - targetDate.getTime() ) / 86400000)
var msg = "This is day number " + timeAfterTarget + " for this year."
document.write(msg)
//-->
</SCRIPT>

Additional Information on These Scripts

All scripts are Y2K compliant.

The Basic Date and Time, Date and Time (24-hr format) and Basic GMT Date and Time are easy to implement but the results displayed will be different for different browsers and operating systems. All of the other scripts should display the same results for all browsers and operating systems.

The Time for a Specific Time Zone script can be set for any time zone by changing the TimeZoneOffset variable. If you don't know your time zone offset, you can find it out by using the Offset from GMT script. You may need to change this value twice a year to adjust for Daylight Savings Time.

Days till Y3K script can be used for other important dates by changing the date that is used to initialize the targetDate variable. Likewise, The Days after a Certain Date script can be used to count the days after a different date by changing the date that is used to initialize the targetDate variable. It is also important that you specify these dates with the full year, ie 2000 rather than 00, to be Y2K compliant.

No comments:

Your Ad Here
#chitikatest=doctor