How to Get Current Date & Time in JavaScript



https://tecadmin.net/get-current-date-time-javascript/

Question – How do I get the current date and time in JavaScript? How to get the date in Y-m-d format in JavaScript? How do I get time in H:i:s format in JavaScript?
This tutorial will help you to get the current date and time in JavaScript. You can also get date and time in your formats like Y-m-d and H:i:s formats.

Get Current Date & Time in JavaScript

Use Date() function to create an object in JavaScript with current date and time. This provides output in UTC timezone.

1. Current Date in JavaScript

Use the following script to get the current date using JavaScript in “Y-m-d” format.
  • getFullYear() – Provides current year like 2018.
  • getMonth() – Provides current month values 0-11. Where 0 for Jan and 11 for Dec. So added +1 to get result.
  • getDate() – Provides day of the month values 1-31.

2. Current Time in JavaScript

Use the following script to get the current time using JavaScript in “H:i:s” format.
  • getHours() – Provides current hour between 0-23.
  • getMinutes() – Provides current minutes between 0-59.
  • getSeconds() – Provides current seconds between 0-59.

3. Current Date & Time Both in JavaScript

Use the following script to get the current date and time using JavaScript in “Y-m-d H:i:s” format. You can simply combine the output of the above JavaScript code in one variable as below:
The dateTime variable contains result as:
2018-8-3 11:12:40

No comments

Powered by Blogger.