Date object in JavaScript

Sourav.Kayal
Posted by in JavaScript category on for Beginner level | Points: 250 | Views : 2341 red flag

In this article we will understand various date handling function in JavaScript.

Date object in JavaScript

In this article we will work with date object in JavaScript. We know that in JavaScript each and everything is represented as object. Date is also one type of object in it. Point to remember that when we will print date using JavaScript it will always give client date means date of client system. So, if the date is wrong in client system then obviously it will give wrong date. It is recommended not to save date produce by JavaScript code in database.

Print today’s date using Date object

In this example, we will print today’s date using JavaScript. As we have discussed earlier , JavaScript is client side language and it always give client computer’s date, so if the date setting is wrong in your computer ,it will show wrong date.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <script>
        var today = new Date();
        console.log(today);
 
    </script>
</body>
</html>


getFullYear() function to print current year

we can get current year by calling getFullYear() function of date object. Have a look on below example.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <script>
        var today = new Date();
        console.log("Year is:- " +  today.getFullYear());
    </script>
</body>
</html>


getMonth() and getDay() function

getMonth function will return the month in number, please keep in mind that number indexing starts from 0, So, if the month is January then it will show 0th month. getDay() function returns day number in current week.  Have a look on below code.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <script>
        var today = new Date();
        console.log("Month :- " + today.getMonth());
        console.log("Day of week :- " + today.getDay());
 
    </script>
</body>
</html>


gethours() , getMunites(), getSecond() , getMillisecond() function

In this example we will see four useful date functions in JavaScript. It will show current Hours, minutes, second and Millisecond of current day. Try to understand below code.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <script>
        var today = new Date();
        console.log("Hours:-" + today.getHours());
        console.log("Minutes:-" + today.getMinutes());
        console.log("Second:-" + today.getSeconds());
        console.log("Milisecode:-" + today.getMilliseconds());
    </script>
</body>
</html>

Here is sample output.


Get local date and time in JavaScript

In this example we will show local date and time, the local date and time means the date and time format will be according to user computer’s time zone.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <script>
        var today = new Date();
        console.log("Local Time format:- "+  today.toLocaleTimeString());
        console.log("Local Date String:- " + today.toLocaleDateString());
    </script>
</body>
</html>


 

Conclusion:-

In this article we have seen various date handling functions in JavaScript. Hope you have understood them. 

 

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)