get current date and time in Javascript | Javascript 2020

 How to get current date and time in Javascript

We can access date and time most of the language. So how we will get the current date and time? What are the statements use for it? So lets begin.

1. You must know about javascript 

To get the current date and time through javascript you must have a little knowledge about javascript language. Basically javascript is a side scripting language use to make websites more dynamic. So you must know about it. 

2. Lets start with date 

Date inside the javascript is an object which returns a lot of information about date and time such as seconds, minutes etc. To use it we must create an object of date and we will store it in the Variable. See below to understand it clearly. 

var date = new Date() 

3. What we can access from Date() object? 

We can access the following things from Date() object. 

  • Milliseconds 
  • Seconds 
  • Minutes 
  • Hours 
  • Days 
  • Weeks 
  • Months 
  • Years 

4. How to convert date into milliseconds?


We can convert date into milliseconds by simply using getTime() function. So watch steps to understand easily. 

var date = new Date(); //assign object to date
date.getTime() // it will convert date. 

5. How to get seconds, minutes and hours? 


Seconds, minutes and hours can be access by using javascript built-in functions. getSeconds() functions is use to get the current time seconds. It will only return the current seconds in the minutes not of whole time. 

var date = new Date();
var sec = date.getSeconds();

getMinutes() functions is use to get the current minutes in the current hour. It will return minutes in the range between 0 to 59.

var date = new Date();
var min = date.getMinutes();

getHours() function is use to get the hour of the current day. It willl return hours value. 

var date = new Date();
var hour = date.getHours();

6. How to get day, month and year?

Date object provide an easy way to get the current day, month and year. 

var date =new Date();
var day= date.getDate();
var month = date.getMonth() +1;
var year = date.getFullYear() ;
  • getDate() : is use to get the current day.
  • getMonth() : function is use to get the current month. We have added one to the month variable because getMonth() function will return value from 0 to 11. 0 means January and 11 means December.
  • getFullYear() : function is use to get  full year.

7. Time format 

You can format date according to your need. 
  • HH:MM:SS
  • SS:MM:HH
  • MM:HH:SS

Eamples:


7.1. HH:MM:SS

var date = new Date();
var hh = date.getHours();
var mm = date.getMinutes();
var ss = date.getSeconds();
var format = hh+":" +mm+":" +ss;
console.log(format);

7.2. SS:MM:HH

var date = new Date();
var hh = date.getHours();
var mm = date.getMinutes();
var ss = date.getSeconds();
var format = ss+":" +mm+":" +hh;
console.log(format);

7.3. MM:HH:SS

var date = new Date();
var hh = date.getHours();
var mm = date.getMinutes();
var ss = date.getSeconds();
var format = mm+":" +hh+":" +ss;
console.log(format);


8. Date format

You can format date according to your needs. 
  • DD:MM:YY
  • MM:DD:YY
  • YY:MM:DD

Examples


8.1 DD:MM:YY

var date = new Date();
var dd = date.getDate();
var mm = date.getMonth()+1;
var yy = date.getFullYear();
var format = dd+":" +mm+":" +yy;
console.log(format);

8.2 MM:DD:YY

var date = new Date();
var dd = date.getDate();
var mm = date.getMonth()+1;
var yy = date.getFullYear();
var format = mm+":" +dd+":" +yy;
console.log(format);

8.3 YY:MM:DD

var date = new Date();
var dd = date.getDate();
var mm = date.getMonth()+1;
var yy = date.getFullYear();
var format = yy+":" +mm+":" +dd;
console.log(format);

Comments

Popular posts from this blog

How to create a list group with css

Getting Started with JavaScript

JavaScript fundamentals