How to Get and Display Current Date and Time using JavaScript

In this tutorial, I am going to teach “How to get and display current date and time using JavaScript”. You will get date and time formats like Y-m-d and H:i:s. I have shared the JavaScript program to display current date and time.

get current date and time in javascript

Get and Display Current Date and Time using JavaScript

In JavaScript, Date() function is used to create an object with current date and time. The date() function will return output in UTC timezone.

var today = new Date(); 
MethodDescription
getFullYear()Get the year as a four digit number (yyyy)
getMonth()Get the month as a number (0-11)
getDate()Get the day as a number (1-31)
getHours()Get the hour (0-23)
getMinutes()Get the minute (0-59)
getSeconds()Get the second (0-59)
getMilliseconds()Get the millisecond (0-999)
getTime()Get the time (milliseconds since January 1, 1970)
getDay()Get the weekday as a number (0-6)
Date.now()Get the time. ECMAScript 5.

1. Get and Display Current Date in JavaScript

Use the below JavaScript code to get current date.

var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
document.write(date);
  • getFullYear() – Provides current year like 2021.
  • 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.
Read also:-

1. How to Get Current Date in Python
2. How to Get Current Time in PHP

2. Get Current Time in 24 Hour Format using JavaScript

Use the below JavaScript to get current time.

var today = new Date();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
document.write(time);
  • getHours() – Provides current hour between 0-23.
  • getMinutes() – Provides current minutes between 0-59.
  • getSeconds() – Provides current seconds between 0-59.

3. Get Current Date and Time in 24 Hour Format using JavaScript

Use the below JavaScript to get current date and time in “Y-m-d H:i:s” format.

var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date+' '+time;
document.write(dateTime);

4. Get and Display Time in 12 Hour Format using JavaScript

var time = new Date().toLocaleTimeString(en-US);
document.write(time);

“.toLocaleString” Locales

ar-SA Arabic (Saudi Arabia)
bn-BD Bangla (Bangladesh)
bn-IN Bangla (India)
cs-CZ Czech (Czech Republic)
da-DK Danish (Denmark)
de-AT Austrian German
de-CH “Swiss” German
de-DE Standard German (as spoken in Germany)
el-GR Modern Greek
en-AU Australian English
en-CA Canadian English
en-GB British English
en-IE Irish English
en-IN Indian English
en-NZ New Zealand English
en-US US English
en-ZA English (South Africa)
es-AR Argentine Spanish
es-CL Chilean Spanish
es-CO Colombian Spanish
es-ES Castilian Spanish (as spoken in Central-Northern Spain)
es-MX Mexican Spanish
es-US American Spanish
fi-FI Finnish (Finland)
fr-BE Belgian French
fr-CA Canadian French
fr-CH “Swiss” French
fr-FR Standard French (especially in France)
he-IL Hebrew (Israel)
hi-IN Hindi (India)
hu-HU Hungarian (Hungary)
id-ID Indonesian (Indonesia)
it-CH “Swiss” Italian
it-IT Standard Italian (as spoken in Italy)
ja-JP Japanese (Japan)
ko-KR Korean (Republic of Korea)
nl-BE Belgian Dutch
nl-NL Standard Dutch (as spoken in The Netherlands)
no-NO Norwegian (Norway)
pl-PL Polish (Poland)
pt-BR Brazilian Portuguese
pt-PT European Portuguese (as written and spoken in Portugal)
ro-RO Romanian (Romania)
ru-RU Russian (Russian Federation)
sk-SK Slovak (Slovakia)
sv-SE Swedish (Sweden)
ta-IN Indian Tamil
ta-LK Sri Lankan Tamil
th-TH Thai (Thailand)
tr-TR Turkish (Turkey)
zh-CN Mainland China, simplified characters
zh-HK Hong Kong, traditional characters
zh-TW Taiwan, traditional characters

“.toLocaleString” Options

dateStyle"full"
"long"
"medium"
"short"
timeStyle"full"
"long"
"medium"
"short"
localeMatcher"best-fit" (default)
"lookup"
timeZone
hour12false
true
hourCycle"h11"
"h12"
"h23"
"h24"
formatMatcher"basic"
"best-fit"
 (default)
weekday"long"
"short"
"narrow
year"2-digit"
"numeric"
month"2-digit"
"long"
"narrow"
"numeric"
"short"
day"2-digit"
"numeric"
hour"2-digit"
"numeric"
minute"2-digit"
"numeric"
second"2-digit"
"numeric"
timeZoneName"long"
"short"

Final Words

I hope this article helps you to get and display current date and time using JavaScript. If you have any doubts regarding this article please let us know via the comment section. Share this article with other developers to help them.

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment