We now have a youtube channel. Subscribe!

JavaScript Date Methods with examples


Hello guys! morning to you all. Welcome to my new tutorial post on JavaScript. Just like i made mention in my last tutorial, we are going to be discussing about the various JavaScript Date Methods with series of examples.

It is going to be an in-depth tutorial with up to 47 different Date methods explained properly with the use of examples. Just read through carefully and ask your questions via the comment box. Am going to be starting with the Date() method.

Date() Method

JavaScript Date() method returns the current date and time and does not need any object to be called.

Syntax

Its syntax is as follows -

Date()

Return Value

Returns the current date and time.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript Date Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  Date();
               document.write("Current Date and Time is :  "  +  dt  );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.


You can also read our tutorial post on: Learn how to work with core JavaScript Date Properties


Date getDate() Method

JavaScript Date getDate() method returns the date of the month for the specified date according to local time. The value returned by getDate() is an integer between 1 and 31. 

Syntax

Its syntax is as follows -

Date.getDate()

Return Value

Returns the date of the month for the specified date according to local time.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript getDate Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April   05,   1993   19 : 20 : 00");
               document.write("getDate() :  "  +  dt.getDate()  );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.

getDate()  :  05

Date getDay() Method

JavaScript Date getDay() method returns the day of the week for the specified date according to local time. The value returned by getDay() is an integer number corresponding to the days of the week; 0 for Sunday, 1 for Monday, 2 is for Tuesday,  and so on.

Syntax

Its syntax is as follows -

Date.getDay()

Return Value

Returns the day of the week for the specified date according to local time.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript getDay Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April   05,   1993 ");
               document.write("getDay() :  "  +  dt.getDay()  );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.

getDay()  :  1



Date getFullYear() Method

Date getFullYear() method returns the year of the specified date according to local time. The value returned by getFullYear() is an absolute number. For dates between the years 1000 and 9999, getFullYear() returns a four digit number, for example 2005.

Syntax

Its syntax is as follows -

Date.getFullYear()

Return Value

Returns the year of the specified date according to local time.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript getFullYear Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April   05,   1993 ");
               document.write("getFullYear() :  "  +  dt.getFullYear()  );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.

getFullYear()  :  1993


Date getHours() Method

JavaScript Date getHours() method returns the hour of the specified date according to local time. The value returned by getHours() is an integer between 0 and 23.

Syntax

Its syntax is as follows -

Date.getHours()

Return Value

Returns the hour of the specified date according to local time.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript getHours Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April   05,   1993    19 : 20 : 00 ");
               document.write("getHours() :  "  +  dt.getHours()  );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.

getHours()  :  19

Date getMilliseconds() Method

The Date getMilliseconds() method returns the milliseconds of the current time according to local time. The value or number that is returned by getMilliseconds() is an integer between 0 and 999.

Syntax

Its syntax is as follows -

Date.getMilliseconds()

Return Value

Returns the milliseconds of the specified date according to local time.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript getMilliseconds Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date( );
               document.write("getMilliseconds() :  "  +  dt.getMilliseconds()  );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.



Date getMinutes() Method

Date getMinutes() method returns the minutes of the specified date according to local time. The value returned by getMinutes() is an integer between 0 and 59.

Syntax

Its syntax is as follows -

Date.getMinutes()

Return Value

Returns the minutes of the specified date according to local time.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript getMinutes Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April   05,   1993    19 : 20 : 00 ");
               document.write("getMinutes() :  "  +  dt.getMinutes()  );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.

getMinutes()  :  20

You can also read our tutorial post on: How to implement JavaScript string Html Wrappers?

Date getMonth() Method

JavaScript Date getMonth() method returns the month of the specified date according to local time. The value returned by getMonth() is an integer value from 0 and 11. 0 corresponding to January, 1 corresponding to February, etc.

Syntax

Its syntax is as follows -

Date.getMonth()

Return Value

Returns the month of the specified date according to local time.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript getMonth Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April   05,   1993    19 : 20 : 00 ");
               document.write("getMonth() :  "  +  dt.getMonth()  );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.

getMonth()  :  03

You can also read our tutorial post on: JavaScript Events tutorial with examples

Date getSeconds() Method

Date getSeconds() method returns the seconds of the specified date according to local time. The value returned by getSeconds() is an integer between 0 and 59.

Syntax

Its syntax is as follows -

Date.getSeconds()

Return Value

Returns the seconds of the specified date according to local time.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript getSeconds Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April   05,   1993    19 : 20 : 10 ");
               document.write("getSeconds() :  "  +  dt.getSeconds()  );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.

getSeconds()  :  10


Date getTime() Method

The Date getTime() method returns the numeric value corresponding to the time for the specified date according to universal time. The value returned by the getTime method is the number of milliseconds since 1 January 1970  00 : 00 : 00. 

You can make use of this method to assign a date and time to another Date object. 

Syntax

Its syntax is as follows -

Date.getTime()

Return Value

Returns the numeric value corresponding to the time for the specified date according to the universal time.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript getTime Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April   05,   1993  ");
               document.write("getTime() :  "  +  dt.getTime()  );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.


Date getTimezoneOffset

Date getTimezoneOffset() method returns the time-zone offset in minutes for the current locale. The time-zone offset is the minutes in difference, the Greenwich Mean Time (GMT) is relative to your local time.

For example, if your time zone is GMT + 10, -600 will be returned. Daylight savings time prevents this value from being a constant.

Syntax

Its syntax is as follows -

Date.getTimezoneOffset()

Return Value

Returns the time-zone offset in minutes for the current locale.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript getTimezoneOffset Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date( );
               var  tz =  dt.getTimezoneOffset();
               document.write("getTimezoneOffset() :  "  +  tz  );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.


You can also read our tutorial post on: Css Visibility 

Date getUTCDate() Method

Date getUTCDate() method returns the day of the month in the specified date according to universal time. The value returned by the getUTCDate() is an integer between 1 and 31.

Syntax

Its syntax is as follows -

Date.getUTCDate()

Return Value

Returns the day of the month in the specified date according to universal time.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript getUTCDate Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April  05,   1993    19 : 20 : 10");
               document.write("getUTCDate() :  "  +  dt.getUTCDate()  );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.

getUTCDate()  :  05

Date getUTCDay() Method

Date getUTCDay() method returns the day of the week in the specified date that is according to universal time. The value returned by the getUTCDay() is an integer corresponding to the day of the week; 0 for Sunday, 1 for Monday, 2 for Tuesday, 3 for Wednesday, and so on. 

Syntax

Its syntax is as follows -

Date.getUTCDay()

Return Value

Returns the day of the week in the specified date according to universal time.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript getUTCDay Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April  05,   1993    19 : 20 : 10");
               document.write("getUTCDay() :  "  +  dt.getUTCDay()  );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.

getUTCDate()  :  1


Date getUTCFullYear() Method

The Date getUTCFullYear() method returns the year in the specified date according to universal time. The value that is returned by the Date getUTCFullYear() is an absolute number that is compliant with year 2000, for example, 2006.

Syntax

Its syntax is as follows -

Date.getUTCFullYear()

Return Value

Returns the year in the specified date according to universal time.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript getUTCFullYear Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April  05,   1993 ");
               document.write("getUTCFullYear() :  "  +  dt.getUTCFullYear()  );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.

getUTCFullYear()  :  1993

You can also read our tutorial post on: How to implement JavaScript for...in loop with examples

Date getUTCHours() Method

Date getUTCHours() method returns the hours in the specified date according to universal time. The value returned by the getUTCHours() is an integer between 0 and 23.

Syntax

Its syntax is as follows -

Date.getUTCHours()

Return Value

Returns the hours in the specified date according to universal time.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript getUTCHours Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date( );
               document.write("getUTCHours() :  "  +  dt.getUTCHours()  );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.



Date getUTCMilliseconds() Method

Date getUTCMilliseconds() method returns the milliseconds in the specified date according to  the universal time. The value returned by the getUTCMilliseconds() is an integer between 0 and 999.

Syntax

Its syntax is as follows -

Date.getUTCMilliseconds()

Return Value

Returns the milliseconds in the specified date according to universal time.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript getUTCMilliseconds Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date( );
               document.write("getUTCMilliseconds() :  "  +  dt.getUTCMilliseconds()  );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.


Date getUTCMinutes() Method

The Date getUTCMinutes() method returns the minutes in the specified date according to  the universal time. The value that is  returned by the getUTCMinutes() is an integer that is between 0 and 59.

Syntax

Its syntax is as follows -

Date.getUTCMinutes()

Return Value

Returns the minutes in the specified date according to universal time.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript getUTCMinutes Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date( );
               document.write("getUTCMinutes() :  "  +  dt.getUTCMinutes()  );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.


You can also read our tutorial post on: JavaScript if....else Statement

Date getUTCMonth() Method

Date getUTCMonth() method returns the month in the specified date according to  the universal time. The value returned by the getUTCMonth() is an integer between 0 and 11 corresponding to the month. 0 for January, 1 for February, and so on. 

Syntax

Its syntax is as follows -

Date.getUTCMonth()

Return Value

Returns the month in the specified date according to universal time.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript getUTCMonth Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date( );
               document.write("getUTCMonth() :  "  +  dt.getUTCMonth()  );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.



Date getUTCSeconds() Method

The Date getUTCSeconds() method returns the seconds in the specified date according to  the universal time. The value that is  returned by the getUTCSeconds() is an integer that is between 0 and 59.

Syntax

Its syntax is as follows -

Date.getUTCSeconds()

Return Value

Returns the seconds in the specified date according to universal time.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript getUTCSeconds Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date( );
               document.write("getUTCSeconds() :  "  +  dt.getUTCSeconds()  );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.


Date getYear() Method

Date getYear() method returns the year in the specified date according to  the universal time. The getYear() is no longer used and has been replaced by the Date getFullYear() method.

The value returned by the getYear() method is the current year minus 1900. JavaScript 1.2 and earlier versions return either a 2-digits or 4-digits year. For example, if the year 2022, the value returned is 2022. So before testing this function, you need to be sure of the JavaScript version you are making use of.

Syntax

Its syntax is as follows -

Date.getYear()

Return Value

Returns the year in the specified date according to universal time.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript getYear Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date( );
               document.write("getYear() :  "  +  dt.getYear()  );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.



Date setDate() Method

JavaScript Date setDate() method sets the day of the month for a specified date according to local time.

Syntax

Its syntax is as follows -

Date.setDate( dayValue )

Parameter Details

dayValue - An integer from 1 to 31, representing the day of the month.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript setDate Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April  05,   1993  19:20:10 " );
               dt.setDate( 22 );
               document.write( dt );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.


You can also read our tutorial post on: JavaScript - Loop Control

Date setFullYear() Method

Date setFullYear() method sets the full year for a specified date according to local time.

Syntax

Its syntax is as follows -

Date.setFullYear(yearValue[,   monthValue[,   dayValue ]] )

Parameter Details

  • yearValue - An integer specifying the numeric value of the year, for example, 2005.
  • monthValue - An integer between 0 and 11 that represents the months January through December.
  • dayValue - An integer between 1 and 31 that represents the day of the month. If you specify the Date dayValue parameter, you must also specify the monthValue.
If you do not specify the Date monthValue and the dayValue parameters, the values returned from the getMonth and getDate methods are used.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript setFullYear Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April  05,   1993  19:20:10 " );
               dt.setFullYear( 1995 );
               document.write( dt );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.


Date setHours() Method

JavaScript Date setHours() method sets the full hours for a specified date according to local time.

Syntax

Its syntax is as follows -

Date.setHours(hoursValue[,   minutesValue[,   secondsValue[,   millisecondsValue] ]] )

Parameter Details

  • hoursValue - An integer between 0 and 23, representing the hours.
  • minutesValue - An integer between 0 and 59, representing the minutes.
  • secondsValue - An integer between 0 and 59, representing the seconds. If you specify the secondsValue parameter, you must also make sure you specify the minutesValue. 
  • millisecondsValue - An integer that is between 0 and 999, representing the millisecond. If you specify the Date msValue parameter, you must also specify the Date minutesValue and secondsValue.
If you do not specify the Date minutesValue, secondsValue and msVaule parameters, the values returned from the getUTCMinutes, getUTCSeconds and getMilliseconds methods are used.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript setHours Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April  05,   1993  19:20:10 " );
               dt.setHours( 05 );
               document.write( dt );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.



Date setMilliseconds() Method

JavaScript Date setMilliseconds() method sets the milliseconds for a specified date according to local time.

Syntax

Its syntax is as follows -

Date.setMilliseconds( millisecondsValue )

Note - Parameters in the bracket are always optional.

Parameter Details

millisecondsValue - An integer between 0 and 999, representing the milliseconds.

If you specify a number outside the expected range, the date information in the Date object is updated accordingly. For example, if you specify 1010, the number of seconds is incremented by 1, and 10 is used for the milliseconds.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript setMilliseconds Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April  05,   1993  19:20:10 " );
               dt.setMilliseconds( 1010 );
               document.write( dt );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.


You can also read our tutorial post on: JavaScript Operators

Date setMinutes() Method

Date setMinutes() method sets the minutes for a specified date according to local time.

Syntax

Its syntax is as follows -

Date.setMinutes(minutesValue[,   secondsValue[,   millisecondsValue]] )

Parameter Details

  • minutesValue - An integer between 0 and 59, representing the minutes.
  • secondsValue - An integer between 0 and 59, representing the seconds. If you specify the secondsValue parameter, you must also specify the minutesValue. 
  • millisecondsValue - An integer that is between 0 and 999, representing the millisecond. If you specify the msValue parameter, you must also specify the minutesValue and secondsValue.
If you do not specify the Date secondsValue and the msVaule parameters, the values returned from the Date getUTCSeconds and getMilliseconds methods are used.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript setMinutes Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April  05,   1993  19:20:10 " );
               dt.setMinutes( 30 );
               document.write( dt );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.


Date setMonth() Method

JavaScript Date setMonth() method sets month for a specified date according to local time.

Syntax

Its syntax is as follows -

Date.setMonth(monthValue[,   dayValue ] )

Parameter Details

  • monthValue - An integer between 0 and 11 that represents the months January through December.
  • dayValue - An integer between 1 and 31 that represents the day of the month.
  • msValue - An integer between 0 and 999, representing the milliseconds. If you specify the msValue parameters,  it is very important that you must also specify the Date minutesValue and secondsValue.
If you do not specify the dayValue parameters, the values returned from the getDate methods is used. If a parameter you specify is outside the expected range, setMonth attempts to update the date information in the Date object accordingly. For example, if you make use of 15 for monthValue, the year will be implemented by 1 and 3 will be used for month.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript setMonth Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April  05,   1993  19:20:10 " );
               dt.setMonth( 3 );
               document.write( dt );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.



Date setSeconds() Method

Date setSeconds() method sets the seconds for a specified date according to local time.

Syntax

Its syntax is as follows -

Date.setSeconds(secondsValue[,   msValue ] )

Parameter Details

  • secondsValue - An integer between 0 and 59.
  • msValue - An integer between 0 and 999, representing the milliseconds.
If you do not specify the msValue parameters, the values returned from the getMilliseconds methods is used. If a parameter you specify is outside the expected range, setSeconds Method attempts to update the date information in the Date object accordingly. For example, if you make use of 100 for secondsValue, the minutes will be implemented by 1 and 40 will be used for seconds.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript setSeconds Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April  05,   1993  19:20:10 " );
               dt.setSeconds( 60 );
               document.write( dt );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.


You can also read our tutorial post on: JavaScript Variables

Date setTime() Method

JavaScript Date setTime() method sets the Date object to the time represented by a number of milliseconds since January 1, 1970, 00:00:00 UTC. 

Syntax

Its syntax is as follows -

Date.setTime( timeValue )

Note - Parameters in the bracket are always optional.

Parameter Details

timeValue - An integer representing the  number of milliseconds since January 1, 1970, 00:00:00 UTC. 

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript setTime Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April  05,   1993  19:20:10 " );
               dt.setTime( 5000000 );
               document.write( dt );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.



Date SetUTCDate() Method

Date setUTCDate() method sets the day of the month for a specified date according to universal time.

Syntax

Its syntax is as follows -

Date.setUTCDate( dayValue )

Note - Parameters in the bracket are always optional.

Parameter Details

dayValue - An integer from 1 to 31, representing the day of the month.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript setUTCDate Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April  05,   1993  19:20:10 " );
               dt.setUTCDate( 25 );
               document.write( dt );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.


Date setUTCFullYear() Method

JavaScript Date setUTCFullYear() method sets the full year for a specified date according to universal time.

Syntax

Its syntax is as follows -

Date.setUTCFullYear(yearValue[,   monthValue[,   dayValue ]] )

Parameter Details

  • yearValue - An integer specifying the numeric value of the year, for example, 2005.
  • monthValue - An integer between 0 and 11 that represents the months January through December.
  • dayValue - An integer between 1 and 31 that represents the day of the month. If you specify the Date dayValue parameter, you must also specify the monthValue.
If you you do not specify the monthValue and the dayValue parameters, the values returned from the getMonth and getDate methods are used. If a parameter you specify is outside the expected range, setUTCFullYear attempts to update the other parameters and the date information in the Date object accordingly. For example, if you specify 15 for monthValue, the year is incremented by 1 and 3 is used for the month.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript setUTCFullYear Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April  05,   1993  19:20:10 " );
               dt.setUTCFullYear( 1995 );
               document.write( dt );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.



Date SetUTCHours() Method

JavaScript Date setUTCHours() method sets the full hours for a specified date according to universal time.

Syntax

Its syntax is as follows -

Date.setUTCHours(hoursValue[,   minutesValue[,   secondsValue[,   millisecondsValue] ]] )

Parameter Details

  • hoursValue - An integer between 0 and 23, representing the hours.
  • minutesValue - An integer between 0 and 59, representing the minutes.
  • secondsValue - An integer between 0 and 59, representing the seconds. If you specify the secondsValue parameter, you must also specify the Date minutesValue. 
  • millisecondsValue - An integer that is between 0 and 999, representing the millisecond. If you specify the Date msValue parameter, you must also specify the Date minutesValue and the secondsValue.
If you do not specify the Date minutesValue, secondsValue and msVaule parameters, the values returned from the getUTCMinutes, getUTCSeconds and getMilliseconds methods are used.

If a parameter you specify is outside the expected range, setUTCHours attempts to update the date information in the Date object accordingly. For example, if you make use of 100 for secondsValue, the minutes will be incremented by 1 and 40 is going to be used as  the seconds.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript setUTCHours Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April  05,   1993  19:20:10 " );
               dt.setUTCHours( 10 );
               document.write( dt );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.


You can also read our tutorial post on: Introduction to Python Programming

Date setUTCMilliseconds() Method

Date setUTCMilliseconds() method sets the milliseconds for a specified date according to universal time.

Syntax

Its syntax is as follows -

Date.setUTCMilliseconds( millisecondsValue )

Note - Parameters in the bracket are always optional.

Parameter Details

millisecondsValue - An integer between 0 and 999, representing the milliseconds.

If you specify a number outside the expected range, the date information in the Date object is updated accordingly. For example, if you specify 1100, the number of seconds is incremented by 1, and 100 is used for the milliseconds.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript setUTCMilliseconds Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April  05,   1993  19:20:10 " );
               dt.setUTCMilliseconds( 1100 );
               document.write( dt );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.


Date SetUTCMinutes() Method

JavaScript Date setUTCMinutes() method sets the minutes for a specified date according to universal time.

Syntax

Its syntax is as follows -

Date.setUTCMinutes(minutesValue[,   secondsValue[,   millisecondsValue]] )

Parameter Details

  • minutesValue - An integer between 0 and 59, representing the minutes.
  • secondsValue - An integer between 0 and 59, representing the seconds. If you specify the secondsValue parameter, you must also specify the minutesValue. 
  • millisecondsValue - An integer that is between 0 and 999, representing the millisecond. If you specify the Date msValue parameter, you must also specify the Date minutesValue and the Date secondsValue.
If you do not specify the Date secondsValue and msVaule parameters, the values returned from the Date getUTCSeconds and getMilliseconds methods are used.

If a parameter you specify is outside the expected range, setUTCMinutes attempts to update the date information in the Date object accordingly. For example, if you use 100 for secondsValue, the minutes (minutesValue) will be incremented by 1 and 40 will be made use of for seconds. 

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript setUTCMinutes Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April  05,   1993  19:20:10 " );
               dt.setUTCMinutes( 55 );
               document.write( dt );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.



Date setUTCMonth() Method

Date setUTCMonth() method sets month for a specified date according to universal time.

Syntax

Its syntax is as follows -

Date.setUTCMonth( monthValue )

Parameter Details

monthValue - An integer between 0 and 11 that represents the months.

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript setUTCMonth Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April  05,   1993  19:20:10 " );
               dt.setUTCMonth( 3 );
               document.write( dt );          
          </script>
     </body>
</html>

Output

Below is the output of the above example.


Date setUTCSeconds() Method
JavaScript Date setUTCSeconds() method sets the seconds for a specified date according to universal time.

Syntax
Its syntax is as follows -

Date.setUTCSeconds(secondsValue[,   msValue ] )

Parameter Details
  • secondsValue - An integer between 0 and 59.
  • msValue - An integer between 0 and 999, representing the milliseconds.
If you do not specify the msValue parameters, the values returned from the getMilliseconds methods is used. If a parameter you specify is outside the expected range, setUTCSeconds Method attempts to update the date information in the Date object accordingly. For example, if you make use of 100 for secondsValue, the minutes will be implemented by 1 and 40 will be used for seconds.

Example
Try the following example below.

<html>
     <head>
          <title> JavaScript setUTCSeconds Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April  05,   1993  19:20:10 " );
               dt.setUTCSeconds( 60 );
               document.write( dt );          
          </script>
     </body>
</html>

Output
Below is the output of the above example.


You can also read our tutorial post on: How to enable JavaScript

Date setYear() Method
JavaScript Date setYear() method sets the year for a specified date according to universal time.

Syntax
Its syntax is as follows -

Date.setYear( yearValue )

Parameter Details
yearValue - An integer specifying the numeric value of the year. For example, 2005.

Example
Try the following example below.

<html>
     <head>
          <title> JavaScript setYear Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new  Date("April  05,   1993  19:20:10 " );
               dt.setYear( 2005 );
               document.write( dt );          
          </script>
     </body>
</html>

Output
Below is the output of the above example.



Date toDateString() Method
The Date toDateString() method returns the date portion of a Date object in human readable form.

Syntax
Its syntax is as follows -

Date.toDateString()

Return Value
Returns the date portion of a Date object in  a human readable form. 

Example
Try the following example below.

<html>
     <head>
          <title> JavaScript toDateString Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new Date(1993,  6,  28,  14,  39,  7);
               document.write("Formatted Date is :  "  +  dt.toDateString()  );          
          </script>
     </body>
</html>

Output
Below is the output of the above example.


Date toGMTString() Method
Date toGMTString() method converts a date to a string, using internet GMT conventions.

This method is no longer used and has been replaced by the toUTCString method.

Syntax
Its syntax is as follows -

Date.toGMTString()

Return Value
Returns a date to a string, using internet GMT conventions.

Example
Try the following example below.

<html>
     <head>
          <title> JavaScript toGTMString Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new Date(1993,  6,  28,  14,  39,  7);
               document.write("Formatted Date is :  "  +  dt.toGMTString()  );          
          </script>
     </body>
</html>

Output
Below is the output of the above example.


You can also read our tutorial post on: Html Stylesheet

Date toLocaleDateString() Method
Date toLocaleDateString() method converts a date to a string, returning the "date" portion using the operating system's locale's conventions.

Syntax
Its syntax is as follows -

Date.toLocaleDateString()

Return Value
Returns the "date" portion using the device's operating system's locale's conventions.

Example
Try the following example below.

<html>
     <head>
          <title> JavaScript toLocaleDateString Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new Date(1993,  6,  28,  14,  39,  7);
               document.write("Formatted Date is :  "  +  dt.toLocaleDateString()  );          
          </script>
     </body>
</html>

Output
Below is the output of the above example.



Date toLocaleFormat() Method
The Date toLocaleFormat() method converts a date to a string a specified formatting.

Note - This method may not be compatible with all browsers.

Syntax
Its syntax is as follows -

Date.toLocaleFormat()

Parameter Details
formatString - A format string in the same format expected by the strftime function in C. 

Return Value
Returns the formatted date.

Example
Try the following example below.

<html>
     <head>
          <title> JavaScript toLocaleFormat Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new Date(1993,  6,  28,  14,  39,  7);
               document.write("Formatted Date is :  "  +  dt.toLocaleFormat( "%A,  %B,  %e,  %Y" )  );          
          </script>
     </body>
</html>

Output
Below is the output of the above example.

Formatted Date is : Wed Jul 28 1993  14:39:07  GMT+0530

Date toLocaleString() Method
The Date toLocaleString() method converts a date to a string, using the device's operating system's locale's conventions.

The Date toLocaleString method relies on the underlying operating system in formatting dates. It converts the date to a string using the formatting convention of the operating system where the script is running. For example, in the United States, the month appears before the date (04/15/1993), whereas in Germany the date appears before the month (15/04/1993).

Syntax
Its syntax is as follows -

Date.toLocaleString()

Return Value
Returns the formatted date in a string format.

Example
Try the following example below.

<html>
     <head>
          <title> JavaScript toLocaleString Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new Date(1993,  6,  28,  14,  39,  7);
               document.write("Formatted Date is :  "  +  dt.toLocaleString()  );          
          </script>
     </body>
</html>

Output
Below is the output of the above example.


Date toLocaleTimeString() Method
Date toLocaleTimeString() method converts a date to a string, returning the "date" portion using the current locale's conventions.

The Date toLocaleString method relies on the underlying operating system in formatting dates. It converts the date to a string using the formatting convention of the operating system where the script is running. For example, in the United States, the month appears before the date (04/15/1993), whereas in Germany the date appears before the month (15/04/1993).

Syntax
Its syntax is as follows -

Date.toLocaleTimeString()

Return Value
Returns the formatted date in a string format.

Example
Try the following example below.

<html>
     <head>
          <title> JavaScript toLocaleTimeString Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new Date(1993,  6,  28,  14,  39,  7);
               document.write("Formatted Date is :  "  +  dt.toLocaleTimeString()  );          
          </script>
     </body>
</html>

Output
Below is the output of the above example.



Date toSource() Method
JavaScript Date toSource() method returns a string representing the source code of the object.

Note - This method may not be compatible with all browsers.

Syntax
Its syntax is as follows -

Date.toSource()

Return Value
  • For the built in Date object, toSource returns a string (new Date() ) indicating that the source code is not available.
  • For instances of Date, toSource returns a string representing the source code.

Example
Try the following example below.

<html>
     <head>
          <title> JavaScript toSource Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new Date(1993,  6,  28,  14,  39,  7);
               document.write("Formatted Date is :  "  +  dt.toSource()  );          
          </script>
     </body>
</html>

Output
Below is the output of the above example.

Formatted Date is :  (new  Date(743850547000))

You can also read our tutorial post on: Html Marquees

Date toString() Method
JsvaScript Date toString() method returns a string representing the specified Date object.

Syntax
Its syntax is as follows -

Date.toString()

Return Value
Returns a string representing the specified Date object.

Example
Try the following example below.

<html>
     <head>
          <title> JavaScript toString Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dt =  new Date(1993,  6,  28,  14,  39,  7);
               stringobj  =  dateobject.toString();
               document.write("String object is :  "  +  stringobj  );          
          </script>
     </body>
</html>

Output
Below is the output of the above example.

String object is :  Wed  Jul  28  1993   14:39:07  GMT+0530


Date toTimeString() Method
Date toTimeString() method returns the time portion of a Date object in a human readable form.

Syntax
Its syntax is as follows -

Date.toTimeString()

Return Value
Returns the time portion of a Date object in a human readable form.

Example
Try the following example below.

<html>
     <head>
          <title> JavaScript toTimeString Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dateobject =  new Date(1993,  6,  28,  14,  39,  7);
               document.write(dateobject.toTimeString() );          
          </script>
     </body>
</html>

Output
Below is the output of the above example.

14:39:07  GMT+0530

Date toUTCString() Method
Date toUTCString() method converts a date to a string, using the universal time convention.

Syntax
Its syntax is as follows -

Date.toUTCString()

Return Value
Returns the converted date to string, using the universal time convention.

Example
Try the following example below.

<html>
     <head>
          <title> JavaScript toUTCString Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dateobject =  new Date(1993,  6,  28,  14,  39,  7);
               document.write(dateobject.toUTCString() );          
          </script>
     </body>
</html>

Output
Below is the output of the above example.

Wed,  28  Jul  1993  09:09:07  GMT 


Date valueOf() Method
Date valueOf()  method returns the primitive value of a Date object as a number data type, the number of milliseconds since midnight 01 of January,  1970 UTC. 

Syntax
Its syntax is as follows -

Date.valueOf()

Return Value
Returns the primitive value of a Date object.

Example
Try the following example below.

<html>
     <head>
          <title> JavaScript valueOf Method </title>
     </head>

     <body>
          <script  type = "text/javascript">
               var  dateobject =  new Date(1993,  6,  28,  14,  39,  7);
               document.write(dateobject.valueOf() );          
          </script>
     </body>
</html>

Output
Below is the output of the above example.

743850547000

Alright guys, we have come to the end of this in-depth tutorial on JavaScript Date methods. In my next tutorial, i will be discussing Date Static Methods which i already introduced in the previous tutorial.

Feel free to ask your questions via the comment box,  it will be a pleasure to help you out with the challenges you facing.

Follow us on our various social media platforms available in other to stay updated with our latest tutorials. Also subscribe to our newsletter to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain