Questions: How do I check the current date and time in JavaScript? What’s the way to format the date as YYYY-MM-DD in JavaScript? How can I display the time as H:i:s in JavaScript?
Knowing the current date and time is essential in many aspects of daily life, and JavaScript can help you with this. The Date object in JavaScript allows you to access the current date and time, but it usually shows the time based on your computerβs settings, not the global standard time. To get more precise timings, you might need to use specific JavaScript tools. Additionally, JavaScript lets you format the date and time in popular formats like YYYY-MM-DD for date and HH:MM:SS for time.
This guide will simplify how you can get and format the current date and time in JavaScript using clear examples.
Getting Current Date & Time in JavaScript
Use the Date()
function to create an object in JavaScript with the current date and time. This provides output in the UTC timezone.
var today = new Date();
1. Current Date in JavaScript
Use the following script to get the current date using JavaScript in βY-m-dβ format.
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
- getFullYear() β Provides current year like 2022.
- getMonth() β Provides current month values 0-11. Where 0 for Jan and 11 for Dec. So added +1 to get the 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.
var today = new Date();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
- 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 the “Y-m-d H:i:s” format. You can simply combine the output of the above JavaScript code in one variable as below:
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;
console.log(dateTime)
Output2024-4-18 9:24:46
Conclusion
By following the examples above, you can easily retrieve and format the current date and time in JavaScript. This allows you to use these timestamps effectively in your projects or daily tasks, ensuring that you have precise control over how dates and times are presented and utilized in your scripts.
32 Comments
You’re grandpa rather.. new JS is not allowed use var, today we use let or const !!!!!
ok but how can i get this dynamicl i mean to change this value on the website second by seconds.. need i use a API? π
Actually No.
You can update date & time with setTimeout() method every secound.
Thanks
Thanks for sharing, works veery well but how do i make the time count automatically without having to refresh
thanks
i want to put the time hrs mins in a graphs x axis.. how do i do it
IRAM SANEGO:
Try Chartjs
useful
How to get ddmmyyhhmmss?
Never mind. I figured it out. Thanks to all of you !!
How to get Day, not in value like actual day Mon, Tues from Date object?
C00L
Works, but doesn’t update (time doesn’t change)
Also how can time be expressed in 12 hour, AM PM format?
Works well and update on page refresh or if you have used a button to display it like
Time
function showTime()
{
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;
var show=document.getElementById(“show”);
show.innerHTML=input;
}
//Now if you will click the button time will update
var date = new Date().toJSON().slice(0,10);
var time = new Date().toJSON().slice(11,19)
var dateTime = date+’ ‘+time;
new Date().toJSON().slice(0,10)
This was very clear and helped me a lot. Much appreciated.
Also, you can use String(today.getMinutes()).padStart(2, ‘0’);
Thank you Sir!
This way when the minute is < 10 it will be displayed as e.g. '10:03' and not '10:3'
if(today.getMinutes() < 10){
var time = today.getHours() + ':0' + today.getMinutes();
}
enjoy..
Hi Filip~ Where do I insert this additional code?
Thanks. Linda (coding newbie)
There is just one issue when printing a time < 10. e.g. '11:3' instead of '11:03'.
Thaks
thanks
Very informative. Thanks to share knowledge with us
Nice explanation
how to display the am and pm of the time in your time code?
var currentTime= new Date().toLocaleTimeString();
Very Helpful. Thanks!
thnx
This helped me. Thanks!