Date Object in JavaScript

Introduction

 
This article explains the date object in JavaScript. When we print a date using JavaScript it will always provide a client date, in other words the date of the client system. So, if the date is incorrect in the client system then obviously it will provide the wrong date. It is recommended not to save the date produced by JavaScript code into the database. 
 
Print today's date using Date object
 
In this example, we will print today's date using JavaScript.  
  1. <!DOCTYPE html>    
  2. <html xmlns="http://www.w3.org/1999/xhtml">    
  3. <head>    
  4. </head>    
  5. <body>    
  6.     <script>    
  7.         var today = new Date();    
  8.         console.log(today);    
  9.      
  10.     </script>    
  11. </body>    
  12. </html>   
 
date using Date object
 
Print current year using getFullYear()
 
We can get the current year by calling the getFullYear() function of the date object.
  1. <!DOCTYPE html>    
  2. <html xmlns="http://www.w3.org/1999/xhtml">    
  3. <head>    
  4. </head>    
  5. <body>    
  6.   <script>    
  7.         var today = new Date();    
  8.         console.log("Year is:- " +  today.getFullYear());    
  9.     </script>    
  10. </body>    
  11. </html>   
 
Print current year using getFullYear
 
Print current year using getFullYear()
 
The getMonth function will return the month as a number. The number indexing starts from 0, so if the month is January then it will return 0. The getDay() function returns the day number in the current week. 
  1. <!DOCTYPE html>    
  2. <html xmlns="http://www.w3.org/1999/xhtml">    
  3. <head>    
  4. </head>    
  5. <body>    
  6.   <script>    
  7.         var today = new Date();    
  8.         console.log("Month :- " + today.getMonth());    
  9.         console.log("Day of week :- " + today.getDay());    
  10.      
  11.     </script>    
  12. </body>    
  13. </html>   
 
getFullYear
 
In the same way we can use gethours() , getMunites(), getSecond() , getMillisecond() functio
 

Summary

 
This article explained the date object in JavaScript. In a future article, we will learn more basic concepts of JavaScript.