Wednesday, April 15, 2020

Static Methods in JavaScript - Learn with well explained examples



Hello guys! welcome to another section of my tutorial on JavaScript. In my previous tutorial i talked about the various Date methods we have with series of examples, i also informed you guys that i will be discussing about the Date Static methods in details in my next tutorial.

So in this tutorial, we will be looking at the two Date Static methods available in JavaScript. Make sure to read through carefully and ask your questions where necessary. Am going to be starting with the Date parse method.


Date parse() Method

Date parse() method takes a date string and returns the number of milliseconds since the midnight of January 1, 1970.

Syntax

Its syntax is as follows -

Date.parse(datestring)

Note - Parameters in the bracket are always optional.

Parameter Details

datestring - A string representing a date.

Return Value

Returns the number of milliseconds since midnight of January 1, 1970.

Example

Try the following example below.

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

     <body>
          <script  type = "text/javascript">
               var  msecs =  Date.parse("April  05,  1993  23:30:00");
               document.write("Number of milliseconds from 1970 :  "  +  msecs  );          
          </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 UTC Method

JavaScript Date UTC() method takes a date and returns the number of milliseconds  since  the midnight of January 1, 1970 according to (UTC) universal time.

Syntax

Its syntax is as follows -

Date.UTC(year, month, [day, [hours, [minutes, [seconds, [ms] ]]]] )  

Parameter Details
  • year - A four digit number representing the year.
  • month - An integer between 0 and 11.
  • day - An integer between 1 and 31 representing the date.
  • hours - An integer that is between 0 and 23, representing the hours.
  • minutes - An integer that is between 0 and 59, representing the minutes. 
  • seconds - An integer that is between 0 and 59, representing the seconds.
  • ms - An integer that is between 0 and 999, representing the milliseconds.

Return Value
Number of milliseconds since the midnight of January 1, 1970.

Example
Try the following example below.

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

     <body>
          <script  type = "text/javascript">
               var  msecs =  Date.UTC( 2008, 9, 6 );
               document.write("Number of milliseconds from 1970 :  "  +  msecs );          
          </script>
     </body>
</html>

Output
Below is the output of the above example.



Alright guys, this is where we are wrapping it up with JavaScript Date Object. In my next tutorial post, i will start discussing about the Javascript Math Object, so stay tuned.

Always feel free to ask your questions via the comment box and i will try my possible best to attend to them as soon as possible.

Follow us on our various social media platforms available to stay updated with our latest posts and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment