Lesson 12
Working with Dates
Handle dates and times in JS
1What is JavaScript Date?✨
JavaScript has a built-in Date object that allows you to work with dates and times. This is essential for applications that require time tracking, scheduling, or any time-related functionality!
2Why Learn About Dates?🎯
- ✓ Manage time effectively in your applications
- ✓ Create calendars or countdowns
- ✓ Display formatted dates and times
- ✓ Handle user inputs for date-related tasks
3Creating a Date💡
You can create a new date object using the Date constructor. By default, it captures the current date and time:
let now = new Date();
console.log(now);This code will show you the exact date and time when the program runs.
4Getting Date Components🚀
You can extract specific parts of a date using methods like getFullYear(), getMonth(), and getDate(). Here's how:let birthday = new Date('2005-06-15');
console.log(birthday.getFullYear()); // 2005
console.log(birthday.getMonth()); // 5 (months are zero-indexed)
console.log(birthday.getDate()); // 15This will print the year, month, and date of your birthday!
5Formatting Dates🔍
You might want to display dates in a more readable format. You can use toLocaleDateString() for this:let today = new Date();
console.log(today.toLocaleDateString('en-US')); // MM/DD/YYYY formatThis gives you a user-friendly way to present dates.
6Calculating Date Differences⭐
You can calculate the difference between two dates by subtracting them. Here's an example:
let startDate = new Date('2023-01-01');
let endDate = new Date('2023-12-31');
let difference = endDate - startDate; // difference in milliseconds
console.log(difference / (1000 * 60 * 60 * 24)); // convert to daysThis shows you how many days are between two dates!
7What You'll Build This Grade🧩
- ✓ A countdown timer for events
- ✓ A simple calendar application
- ✓ A birthday reminder app
- ✓ A time tracking tool for projects
🎯 Practice Challenges
Master Working with Dates by completing all 3 difficulty levels
Working with Dates - Easy Challenge
Start with the basics. Perfect for beginners!
Working with Dates - Medium Challenge
Get more challenging. Build your confidence!
Working with Dates - Hard Challenge
Master this concept. Prove your skills!
🏆 Study a lesson for 2+ minutes to earn +10 TalentJR Points a day. Badges, certificates & prizes →
Ready to Master Working with Dates? 🚀
Complete the challenges, take the quiz and keep your learning streak going!
Keep learning
- ← Previous lessonLesson 11: ES6 ModulesSplit code across import/export files
- Next lesson →Lesson 13: Async/Await Deep DiveWrite cleaner asynchronous code
- Grade 10 courseC Programming - System ProgrammingC coding course for kids.
- Student ToolsFree calculators for studentsPercentage, fraction, ratio and marks calculators with steps.
- CBSEClass 9 CBSE & NCERT study materialMaths, Science and Computer chapters with solutions.
- Future SkillsExplore future skillsAI, robotics and new-age skills explained for students.