Go4Expert

Go4Expert (http://www.go4expert.com/)
-   JSP (http://www.go4expert.com/articles/jsp-tutorials/)
-   -   The Date object in Java Script (http://www.go4expert.com/articles/date-object-java-script-t3911/)

Sanskruti 17Apr2007 18:14

The Date object in Java Script
 
The Date object is used to work with dates and times. The Date object is useful when you want to display a date or use a timestamp in some sort of calculation. In java you can either make a Date object by supplying the date of your choice, or you can let Javascript create a Date object based on your visitor's system clock. It is usually best to let Javascript simply use the system clock. When creating a Date object based on the computer's (not web server's!) internal clock, it is important to note that if someone's clock is off by a few hours or they are in a different time zone, then the Date object will create a different time than the one created with your own computer.

Now let us see how to use the Date() method to get today's date.

HTML Code:

<html>
  <body>
    <script type="text/javascript">document.write(Date())</script>
  </body>
</html>

Now let us see how to Use getTime() to calculate the years since 1970.

HTML Code:

<html>
  <body>
    <script type="text/javascript">
      var min = 1000*60
      var hrs = min*60
      var days = hrs*24
      var yrs = days*365
      var d = new Date()
      var t = d.getTime()
      var y = t/yrs
      document.write("It's been: " + y + " yrs since 1970/01/01!")
    </script>
  </body>
</html>

Now let us see how to Use getDay() and will get Today’s week day.

HTML Code:

<html>
  <body>
  <script type="text/javascript">
    var d=new Date()
    var weekday=new Array(7)
    weekday[0]="Sunday"
    weekday[1]="Monday"
    weekday[2]="Tuesday"
    weekday[3]="Wednesday"
    weekday[4]="Thursday"
    weekday[5]="Friday"
    weekday[6]="Saturday"
    document.write("Today it is " + weekday[d.getDay()])
  </script>
  </body>
</html>

Now let us see how to how to display a clock on your web page.

HTML Code:

<html>
  <head>
    <script type="text/javascript">
      function startTime()
      {
        var today=new Date()
        var h=today.getHours()
        var m=today.getMinutes()
        var s=today.getSeconds()
        // add a zero in front of numbers<10
        m=checkTime(m)
        s=checkTime(s)
        document.getElementById('txt').innerHTML=h+":"+m+":"+s
        t=setTimeout('startTime()',500)
      }
      function checkTime(i)
      {
        if (i<10)
          {i="0" + i}
        return i
      }
    </script>

  </head>
  <body onload="startTime()">
    <div id="txt"></div>
  </body>
</html>

Defining Dates



The Date object is used to work with date and time. We define a Date object with the new keyword. The following code line defines a Date object called myDate:

var myDate=new Date()

The Date object will automatically hold the current date and time as its initial value!We can easily manipulate the date by using the methods available for the Date object.

In the example below we set a Date object to a specific date (14th January 2010):

var myDate=new Date()
myDate.setFullYear(2010,0,14)

And in the following example we set a Date object to be 5 days into the future:

var myDate=new Date()
myDate.setDate(myDate.getDate()+5)

If adding five days to a date shifts the month or year, the changes are handled automatically by the Date object itself!The Date object is also used to compare two dates.

The following example compares today's date with the 14th January 2010:

HTML Code:

var myDate=new Date()
myDate.setFullYear(2010,0,14)
var today = new Date()
if (myDate>today)
  alert("Today is before 14th January 2010")
else
  alert("Today is after 14th January 2010")


pradeep 18Apr2007 17:39

Re: The Date object in Jsp
 
This is not JSP, it's JavaScript!

shabbir 18Apr2007 20:17

Re: The Date object in Jsp
 
Quote:

Originally Posted by pradeep
This is not JSP, it's JavaScript!

Edited the title. Thanks for the input.

saasbs 21May2007 19:25

Re: The Date object in Java Script
 
hi, this code can be for calender?
i mean its the same code for the calender?
cuz i want 2 put a calender in my project for the check in date and check out, is that code is the right code for that or not?
rhnx :)

pradeep 21May2007 22:51

Re: The Date object in Java Script
 
You can make a calendar, using JavaScript but this is not a calendar!
You'll find a lot of ready-to-use calendars at www.dynamicdrive.com

parvez.yu 6Mar2008 17:02

Re: The Date object in Java Script
 
thanks even i need to work with dates and times

trinitybrown 25Oct2008 12:31

Re: The Date object in Java Script
 
good script, can you share a script for displaying scrolling tect in vertical motion as i am finding it but still i don't get it , kindly help me

gkumar 24Jun2009 16:35

Re: The Date object in Java Script
 
Describes the JavaScript Date Object including properties, constructors, and methods.
Properties

* prototype - For creating more properties.

Constructors

* Date() - Use the current date and time to create an instance of the object date.
* Date(dateString) - Use the date specified by the string to create the instance of the date object. String format is "month day, year hours:minutes:seconds".
* Date(year, month, day) - Create an instance of date with the specified values. Year is 0 to 99.
* Date(year, month, day, hours, minutes, seconds) - Create an instance of date with the specified values.

Methods

* getDate() - Get the day of the month. It is returned as a value between 1 and 31.

Code:

var curdate = new Date()
var mday = curdate.getDate()
document.write(mday + "<BR>")

#

The above code prints the day of the month.
# getDay() - Get the day of the week as a value from 0 to 6
Code:

var curdate = new Date()
var wday = curdate.getDate()
document.write(wday + "<BR>")

#

The above code prints the day of the week.
# getHours() - The value returned is 0 through 23.
Code:

var curdate = new Date()
var hours = curdate.getHours()
document.write(hours + "<BR>")

#

The above code prints the hours since midnight.
# getMinutes() - The value returned is 0 through 59.
Code:

var curdate = new Date()
var minutes = curdate.getMinutes()
document.write(minutes + "<BR>")

#

The above code prints the minutes past the hour.
# getMonth() - Returns the month from the date object as a value from 0 through 11.
Code:

var curdate = new Date()
var month = curdate.getMonth()
document.write(month + "<BR>")

#

The above code prints the numeric value of the month.
# getSeconds() - The value returned is 0 through 59.
Code:

var curdate = new Date()
var seconds = curdate.getSeconds()
document.write(seconds + "<BR>")

#

The above code prints the seconds since the last minute.
# getTime() - The number of milliseconds since January 1, 1970. this function allows you to manipulate the date object based on a millisecond value then convert it back to the form you want. In the example below, it is used to set a future expiration time of a cookie.
Code:

var futdate = new Date()
var expdate = futdate.getTime()
expdate += 3600*1000 //expires in 1 hour(milliseconds)
futdate.setTime(expdate)

getTimeZoneOffset() - Time zone offset in hours which is the difference between GMT and local time.
Code:

var curdate = new Date()
var offset = curdate.getTimeZoneOffset()
document.write(offset + "<BR>")

#

The above code prints the number of hours different between your timezone and GMT. This value may change with daylight savings time..
# getYear() - Returns the numeric four digit value of the year.

Code:

var curdate = new Date()
var year = curdate.getYear()
document.write(year + "<BR>")

#

The above code prints the numeric value of the year which is currently 2000.
# parse() - The number of milliseconds after midnight January 1, 1970 till the given date espressed as a string in the example which is IETF format.
Code:

var curdate = "Wed, 18 Oct 2000 13:00:00 EST"
var dt = Date.parse(curdate)
document.write(dt + "<BR>")

# setDate(value) - Set the day of the month in the date object as a value from 1 to 31.
# setHours(value) - Set the hours in the date object with a value of 0 through 59.
# setMinutes(value) - Set the minutes in the date object with a value of 0 through 59.
# setMonth(value) - Set the month in the date object as a value of 0 through 11.
# setSeconds(value) - Set the seconds in the date object with a value of 0 through 59.
# setTime(value) - Sets time on the basis of number of milliseconds since January 1, 1970. The below example sets the date object to one hour in the future.
Code:

var futdate = new Date()
var expdate = futdate.getTime()
expdate += 3600*1000 //expires in 1 hour(milliseconds)
futdate.setTime(expdate)

# setYear(value) - Set the year in the date instance as a 4 digit numeric value.
# toGMTString() - Convert date to GMT format in a form similar to "Fri, 29 Sep 2000 06:23:54 GMT".
Code:

var curdate = new Date()
dstring = curdate.toGMTString()
document.write(dstring + "<BR>" + curdate.toLocaleString() + "<BR>")

The above example produces:
Code:

Wed, 18 Oct 2000 18:08:11 UTC
10/18/2000 14:08:11

# toLocaleString() - Convert date to local time zone format. See the example, above.
# UTC() - Based on a comma delimited string, the number of milliseconds after midnight January 1, 1970 GMT is returned. The syntax of the string is "year, month, day [, hrs] [, min] [, sec]". An example is "2000, 9, 29, 5, 43, 0" for Sept 29, 2000 at 5:43:0. The string is considered to be GMT. The hours, minutes, and seconds are optional.
Code:

document.write(Date.UTC(2000, 9, 29, 5, 43, 0) + "
")


salman8200 11Aug2009 12:17

Re: The Date object in Java Script
 
such a nice trickes really good thank you so much


All times are GMT +5.5. The time now is 11:29.