Wednesday 23 November 2011

Date Function


Ø       Date()

·         This function return the current date and time.

            Syntax : Date()

Example

<html>
          <head>
                   <script language="javascript">
                             var currentdate = new Date();
                             document.write(currentdate);                 
</script>
          </head>
     </html>

Output

          Thu Jul 14 2011 23:49:04

Ø       getDate()

·         This function return the day of the month.
·         The value returned by getDate() is a number between 1 and 31.

            Syntax :  dateobject.getDate()

Example

<html>
          <head>
                   <script language="javascript">
                             var d = new Date();
                             document.write(d.getDate());                 
</script>
          </head>
     </html>

Output

          14

Ø       getDay()

·         The getDay() function return a number that represents the day of the week.
·       The value returned by getDay() is a number between 0 and 6. Sunday is 0 and Monday is 1 and so on.

          Syntax :  dateobject.getDay()

Example

<html>
          <head>
                   <script language="javascript">
                             var d = new Date();
                             document.write(d.getDay());                  
</script>
          </head>
     </html>

Output

          4

Ø       getMonth()

·         The getMonth() function returns the month as a number.
·        The value returned by getMonth() is a number between 0 to 11. January is 0 and February is 1 and so on.

            Syntax :  dateobject.getMonth()

Example

<html>
          <head>
                   <script language="javascript">
                             var d = new Date();
                             document.write(d.getMonth());               
</script>
          </head>
     </html>

Output           

6

Ø       getYear()

·         The getYear() method returns the year, as a two-digit or a four-digit number.
·      The value returned by getYear() is not always 4 numbers. For years between 1900 and 1999 the getYear() method returns only two digits. For years before 1900 and after 1999 it returns four digits.

Syntax :  dateobject.getYear()

Example

<html>
          <head>
                   <script language="javascript">
                             var d = new Date();
                             document.write(d.getYear());                 
</script>
          </head>
     </html>

Output           

                        2011

Ø       getFullYear()

·         The getFullYear() method returns a four-digit number that represents a year.

Syntax :  dateobject.getFullYear()

Example

<html>
          <head>
                   <script language="javascript">
                             var d = new Date();
                             document.write(d.getFullYear());             
</script>
          </head>
     </html>

Output           

               2011

Ø       getHours()

·         The getHours() method returns the hours of a time.
·      The value returned by getHours() is a two digit number. However, the return value is not always two digits, if the value is less than 10 it only returns one digit.

Syntax :  dateobject.getHours()

Example

<html>
          <head>
                   <script language="javascript">
                             var d = new Date();
                             document.write(d.getHours());                
</script>
          </head>
     </html>

Output           

               23

Ø       getMinutes()

·         The getMinutes() method returns the minutes of a time.
·       The value returned by getMinutes() is a two digit number. However, the return value is not always two digits, if the value is less than 10 it only returns one digit.

Syntax :  dateobject.getMinutes()

Example

<html>
          <head>
                   <script language="javascript">
                             var d = new Date();
                             document.write(d.getMinutes());             
</script>
          </head>
     </html>

Output           

50

Ø       getSeconds()

·         The getSeconds() method returns the seconds of a time.
·        The value returned by getSeconds() is a two digit number. However, the return value is not always two digits, if the value is less than 10 it only returns one digit.

Syntax :  dateobject.getSeconds()

Example

<html>
          <head>
                   <script language="javascript">
                             var d = new Date();
                             document.write(d.getSeconds());            
</script>
          </head>
     </html>

Output        

               28

Ø       getMilliseconds()

·         The getMilliseconds() method returns the milliseconds of a time.
·         The value returned by getSeconds() is a three digit number. However, the return value is not always three digits, if the value is less than 100 it only returns one digit, and if the value is less than 10 it only returns one digit.

Syntax :  dateobject.getMilliseconds()

Example

<html>
          <head>
                   <script language="javascript">
                             var d = new Date();
                             document.write(d.getMilliseconds());                 
</script>
          </head>
     </html>

Output           

               671

Ø       setDate()

·         The setDate() method is used to set the day of the month.

Syntax :  dateobject.setDate(day)

          Parameter

Description

Day
Required, a numeric value (from 1 to 31) that represents a day in a month.


Example

<html>
          <head>
                   <script language="javascript">
                             var d = new Date();
                             d.setDate(10);
                             document.write(d.getDay());;                 
</script>
          </head>
     </html>

Output           

               10

Ø       setMonth()

·         The setMonth() method is used to set the month.
·         The value set by setMonth() is a number between 0 and 11. January is 0, February is 1 and so on.

Syntax :  dateobject.setMonth(month, [day])

          Parameter

Description

Month

Required, a numeric value between 0 to 11 represents the day.

Day

Optional, a numeric value between 1 and 31 representing the day.

Example

<html>
          <head>
                   <script language="javascript">
                             var d = new Date();
                             d.setMonth(10);
                             document.write(d.getMonth());
</script>
          </head>
     </html>

Output           

               3

Ø       setYear()

·         The setYear() method is used to set the year.
·         If the year parameter is two digit number, like setYear(91), it will be precive as 1991. to specify a year before 1900 ao after 1999, always use four digits.

Syntax :  dateobject.setYear(year)

          Parameter

Description

Year

Required, a two or four digit number that indicates the year.

Example

<html>
          <head>
                   <script language="javascript">
                             var d = new Date();
                             d.setYear(99);
                             document.write(d.getYear());
</script>
          </head>
     </html>

Output     

99

Ø       setFullYear()

·         The setFullYear() method Is used to set the year.

Syntax :  dateobject.setFullYear(year,[month],[day])

          Parameter

Description

Year

Required, a four digit value representing the year.

Month

Optional, a numeric value between 0 to 11 representing the month.

Day

Optional, a numeric value between 1 to 31 representing the date.

Example

<html>
          <head>
                   <script language="javascript">
                             var d = new Date();
                             d.setFullYear(1999);
                             document.write(d.getFullYear());
</script>
          </head>
     </html>

Output       
    

2011

Ø       setHours()

·         The setHours() method is used to set the hour of a specified time.

Syntax :  dateobject.setHours(hour,[min],[sec],[millisec])

          Parameter

Description

hour

Required, a numeric value between 0 to 23 representing the hour.

Min

Optional, a numeric value between 0 to 59 representing the minutes.

Sec

Optional, a numeric value between 0 to 59 representing the second.

Millisec

Optional, A numeric value between 0 to 999 representing the milliseconds

Example

<html>
          <head>
                   <script language="javascript">
                             var d = new Date();
                             d.setHours(9);
                             document.write(d.getHours());
</script>
          </head>
     </html>

Output


9

Ø       setMinutes()

·         The setMinutes() method is used to set the minutes of a specified time.
·         If one of the parameter above is specified with a one – digit number, javascript adds one or two leading zeros in the result.

Syntax :  dateobject.setHours(min,[sec],[millisec])

          Parameter

Description

Min

Required, a numeric value between 0 to 59 representing the minutes.

Sec

Optional, a numeric value between 0 to 59 representing the second.

Millisec

Optional, A numeric value between 0 to 999 representing the milliseconds

Example

<html>
          <head>
                   <script language="javascript">
                             var d = new Date();
                             d.setMinutes(59);
                             document.write(d.getMinutes());
</script>
          </head>
     </html>

Output

59

Ø       setSeconds()

·         The setSeconds() methods is used to set the second of a specified time.
·         If one of the parameters above is specified with a one – digit number, javascript adds one or two leading zeros in the result.

Syntax :  dateobject.setSeconds(sec,[millisec])

          Parameter

Description

Sec

Required, a numeric value between 0 to 59 representing the second.

Millisec

Optional, A numeric value between 0 to 999 representing the milliseconds

Example

<html>
          <head>
                   <script language="javascript">
                             var d = new Date();
                             d.setSeconds(10);
                             document.write(d.getSeconds());
</script>
          </head>
     </html>

Output

10

Posted By : Hemangi Zala

No comments:

Post a Comment