Core PHP, How To

How to Get Current Date and Time In JavaScript

Before we start the tutorial on How to Get Current Date and Time In JavaScript, we have to know the brief of...

Written by Bikash · 1 min read >

Before we start the tutorial on How to Get Current Date and Time In JavaScript, we have to know the brief of JavaScript and Date method in JS.

Scroll to bottom, to check live view

Introduction

JavaScript is lightweight a client-side scripting language used to build client-side validations and much more. Today we use it to display current time in JavaScript in the HTML input box.

We use JavaScript Date object to get current time.

How to Get Current Date and Time In JavaScript
How to Get Current Date and Time In JavaScript

Also Read: Migrate WordPress Site To New Host Manually

Creating JS Date Object

JavaScript date object help to getting the current date and time which we also used for getting the visitor’s current time and date to store on the database also.

Get Current Date in JavaScript

By using the below source code we get current date in JavaScript in the HTML input box, with year-month-date “Y-m-d” format.

Explanation:

  • getFullYear() – Get current year like 2020.
  • getMonth() – Get current month values 0-11. Where 0 for Jan and 11 for Dec. So added +1 to get result.
  • getDate() – Get day of the month values 1-31.

Get Current Time in JavaScript

We use JS get method to get current time in JavaScript and also display it on the HTML text box.
Format “H:i:s

Explanation:

  • getHours() – Get current hour between 0-23.
  • getMinutes() – Get current minutes between 0-59.
  • getSeconds() – Get current seconds between 0-59.

Get Current Date and Current Time in JavaScript

See the Pen Get Current Date and Time Using JS by Bikash Panda (@phpcodertech) on CodePen.

Above is the complete explanation of the source code, you can copy and paste it to work the same as shown in live on your project.

Also Check:

Happy Coding..!

Was this article helpful?
YesNo
Written by Bikash
My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP. Profile

5 Replies to “How to Get Current Date and Time In JavaScript”

Leave a Reply

Your email address will not be published. Required fields are marked *