TalentJR

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:

💻 code
let now = new Date();
console.log(now);

This code will show you the exact date and time when the program runs.

4Getting Date Components🚀

💻 code
You can extract specific parts of a date using methods like getFullYear(), getMonth(), and getDate(). Here's how:
💻 code
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());      // 15

This will print the year, month, and date of your birthday!

5Formatting Dates🔍

💻 code
You might want to display dates in a more readable format. You can use toLocaleDateString() for this:
💻 code
let today = new Date();
console.log(today.toLocaleDateString('en-US')); // MM/DD/YYYY format

This 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:

💻 code
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 days

This 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

🟢 EASY

Working with Dates - Easy Challenge

Start with the basics. Perfect for beginners!

🟡 MEDIUM

Working with Dates - Medium Challenge

Get more challenging. Build your confidence!

🔴 HARD

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!