Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
karthikeya Boyini

The Date object is a data type built into the JavaScript language. Date objects are created with the new Date() as shown below.

Once a Date object is created, a number of methods allow you to operate on it. Most methods simply allow you to get and set the year, month, day, hour, minute, second, and millisecond fields of the object, using either local time or UTC (universal, or GMT) time.

The toLocaleTimeString() function of the date object returns the date of the current date (including time).

Syntax

Its Syntax is as follows

dateObj.toLocaleString()

Example

Live Demo

<html>
   <head>
      <title>JavaScript Example</title>
   </head>
   <body>
      <script type="text/javascript">
         var dateObj = new Date('September 26, 89 12:4:25:96');
         document.write("Current Date: "+dateObj.toLocaleString());         
      </script>
   </body>
</html>

Output

Current Date: 9/26/1989, 12:04:25 PM

Example

If the Date mentioned in the constructor if the date object is not in between 1 and 31 this function returns Invalid Date.

Live Demo

<html>
   <head>
      <title>JavaScript Example</title>
   </head>
   <body>
      <script type="text/javascript">
         var dateObj = new Date('September 467, 1989 12:4:25:96');
         document.write("Current Date: "+dateObj.toLocaleString());         
      </script>
   </body>
</html>

Output

Current Date: Invalid Date

Example

If you pass nothing to the date constructor this method will return the current time string.

Live Demo

<html>
   <head>
      <title>JavaScript Example</title>
   </head>
   <body>
      <script type="text/javascript">
         var dateObj = new Date();
         document.write("Current Date: "+dateObj.toLocaleString());         
      </script>
   </body>
</html>

Output

Current Date: 18/10/2018, 15:57:04 PM

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.