JavaScript Date and Time Handling: A Practical Guide

![[JS 강좌] 2강 - 개발 환경 구축하기 (VS Code, Chrome 개발자 도구)](/uploads/editor/JS-%EA%B0%95%EC%A2%8C-2%EA%B0%95-%EA%B0%9C%EB%B0%9C-%ED%99%98%EA%B2%BD-%EA%B5%AC%EC%B6%95%ED%95%98%EA%B8%B0-VS-Code-Chrome-%EA%B0%9C%EB%B0%9C%EC%9E%90-%EB%8F%84%EA%B5%AC-20260706-0dfe9b.webp)
Why Work With Dates and Times?
You'll frequently encounter date and time functionality when using websites.
Examples include:
Displaying the current date
Distinguishing posts written today
Event end times
Showing remaining time
Automatic page refreshes
Auto-rotating slideshows
Login session timeouts
Date and time features are used in many different functionalities.
JavaScript provides the Date object and timer functions to handle these features.
What Is a Date Object?
The Date object is JavaScript's built-in object for working with dates and times.
The most basic way to get the current time is as follows:
let now = new Date();
console.log(now);
Running this will output the current date and time.
Getting the Current Year
let now = new Date();
console.log(now.getFullYear());
For example, it will output:
2026
as shown.
Getting the Current Month
let now = new Date();
console.log(now.getMonth());
Note that months start from 0.
Result | Actual Month |
|---|---|
0 | January |
1 | February |
11 | December |
So to display the actual month, you'll write:
console.log(now.getMonth() + 1);
.
Getting Today's Date
let now = new Date();
console.log(now.getDate());
Result:
15
This gets the current date.
Getting the Current Day of the Week
You can also check the day of the week.
let now = new Date();
console.log(now.getDay());
The returned values are as follows:
Value | Day of Week |
|---|---|
0 | Sunday |
1 | Monday |
2 | Tuesday |
3 | Wednesday |
4 | Thursday |
5 | Friday |
6 | Saturday |
Getting the Current Time
Hours:
console.log(now.getHours());
Minutes:
console.log(now.getMinutes());
Seconds:
console.log(now.getSeconds());
Creating a Specific Date
You can create specific dates, not just the current time.
let birthday = new Date(2026, 6, 15);
console.log(birthday);
Since months start from 0, 6 represents July.
Formatting Dates for Display
In real-world development, you'll often use formats like:
2026-07-06
You can create this format in JavaScript as follows:
let now = new Date();
let date =
now.getFullYear() + "-" +
(now.getMonth() + 1) + "-" +
now.getDate();
console.log(date);
setTimeout()
The setTimeout() function runs once after a specified time period.
setTimeout(function(){
alert("3 seconds have passed.");
}, 3000);
The value 3000 represents 3000 milliseconds, or 3 seconds.
Using Arrow Functions
setTimeout(() => {
console.log("executed");
}, 2000);
This syntax is commonly used in professional development.
setInterval()
The setInterval() function runs repeatedly at specified time intervals.
setInterval(function(){
console.log("executed every second");
}, 1000);
This runs every second continuously.
clearInterval()
You can stop repeated execution as well.
let timer = setInterval(function(){
console.log("executed");
},1000);
clearInterval(timer);
You can stop the repetition at any point you need.
clearTimeout()
You can also cancel scheduled execution.
let timer = setTimeout(function(){
console.log("executed");
},5000);
clearTimeout(timer);
If canceled before 5 seconds, the function won't execute.
Building a Digital Clock
Let's display the current time continuously.
setInterval(function(){
let now = new Date();
console.log(
now.getHours() + ":" +
now.getMinutes() + ":" +
now.getSeconds()
);
},1000);
This outputs the current time every second.
Creating a Countdown Timer
let count = 10;
let timer = setInterval(function(){
console.log(count);
count--;
if(count < 0){
clearInterval(timer);
}
},1000);
This counts down from 10 to 0 every second.
Implementing Auto-Scrolling Slides
Many websites use auto-rotating banner slides.
setInterval(function(){
nextSlide();
},3000);
This moves to the next slide every 3 seconds.
Navigating After a Set Time
setTimeout(function(){
location.href = "index.html";
},5000);
This redirects to another page after 5 seconds.
Where Are These Used in Practice?
Date and timer functions are used in many features.
Examples include:
Login session timeouts
Event countdown timers
Automatic logout
Auto-save functionality
Slide banners
Real-time clocks
Chat auto-refresh
Reservation systems
These date and timer features are used in almost all time-related functionality.
Key Takeaways From This Lesson
Today, you've learned how to handle dates and times in JavaScript.
The essential points to remember are:
new Date()creates the current date and time.You can retrieve date information using methods like
getFullYear(),getMonth(), andgetDate().getMonth()starts counting from 0.setTimeout()runs once after a delay.setInterval()repeats execution at set intervals.You can cancel execution using
clearTimeout()andclearInterval().Date and timer functionality is frequently used in professional development.
Conclusion
Handling dates and times isn't limited to creating simple clock features.
In real-world development, it's a core skill used for scheduled execution, auto-saving, event end times, automatic logout, slide banners, and many other time-related functions.
Try running the examples from this lesson to display the current time, and observe how setTimeout() and setInterval() differ in operation. Running these examples will help you better understand their purposes.
In the next lesson, we'll cover the concept and usage of JSON (JavaScript Object Notation), which is essential before handling data exchange with servers.
FAQ
Why does getMonth() start from 0?
JavaScript's Date object uses a zero-based index for months (January = 0, December = 11), which is common in programming languages to simplify calculations.
How do I format dates as YYYY-MM-DD in JavaScript?
Combine getFullYear(), getMonth()+1, and getDate() with hyphens: year + '-' + (month+1) + '-' + date.
Korean Source
This article is an English localization of a Korean-language report from BlogScore. Original: [JS 강좌] 10강 - 날짜(Date), 시간, 타이머(setTimeout, setInterval) 활용하기