JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS Dates


📘 JavaScript Dates – Working with Time and Calendar

JavaScript uses the Date object to work with dates and times. It provides multiple methods to create, retrieve, modify, and format dates.

JavaScript counts time in milliseconds from January 1, 1970 (UTC) — called the Unix Epoch.


🔹 Creating Date Objects

new Date();                          // Current date and time
new Date("2025-07-10");              // Specific date (YYYY-MM-DD)
new Date(2025, 6, 10);               // Year, Month (0-based), Day
new Date(2025, 6, 10, 14, 30);       // Y, M, D, H, M

📝 Note: Months are zero-based (January = 0, December = 11)


🔹 Useful Date Methods

Method Description Example
getFullYear() Get 4-digit year date.getFullYear()2025
getMonth() Get month (0-11) date.getMonth()6 (July)
getDate() Get day of the month (1-31) date.getDate()10
getDay() Get day of week (0-6, Sunday = 0) date.getDay()4 (Thursday)
getHours() Get hour (0–23) date.getHours()
getMinutes() Get minutes date.getMinutes()
getSeconds() Get seconds date.getSeconds()
getMilliseconds() Get milliseconds date.getMilliseconds()
toDateString() Convert to readable date string date.toDateString()"Thu Jul 10 2025"
toTimeString() Time portion of date date.toTimeString()
toLocaleDateString() Localized date date.toLocaleDateString()
toISOString() ISO 8601 format date.toISOString()

🔹 Modifying Date Values

let d = new Date();
d.setFullYear(2030);             // Change year
d.setMonth(11);                  // December
d.setDate(25);                   // 25th
d.setHours(10, 45);              // 10:45 AM

Practice Questions

Q1. How do you create a date object for July 10, 2025, using the Date constructor?

Q2. How do you get the current year from a Date object?

Q3. Which method returns the name of the day as a number (0 for Sunday to 6 for Saturday)?

Q4. How do you convert a Date object into a human-readable string like "Thu Jul 10 2025"?

Q5. What does the getMonth() method return for the month of December?

Q6. How do you change the year of an existing Date object to 2035?

Q7. Which method gives the time portion (HH:MM:SS) of a Date object?

Q8. How do you get today’s date in a localized format for your system?

Q9. What does new Date().toISOString() return?

Q10. How can you create a date object that includes time as well: July 10, 2025, 15:30?


JS Dates Quiz

Q1: What will new Date().getFullYear() return in the year 2025?

A. 2024
B. 2025
C. 25
D. "2025"

Q2: Which method returns the month of a Date object?

A. getMonth()
B. getDate()
C. getYear()
D. getDay()

Q3: What is the output of new Date(2025, 6, 10).getMonth()?

A. 5
B. 6
C. 7
D. "June"

Q4: Which of the following creates a date for July 10, 2025, at 3:30 PM?

A. new Date(2025, 6, 10, 15, 30)
B. new Date(2025, 7, 10, 15, 30)
C. new Date("2025-07-10 15:30")
D. Both a and c

Q5: Which method would you use to get only the date as a string (e.g., "Thu Jul 10 2025")?

A. getDate()
B. toString()
C. toDateString()
D. toLocaleDateString()

Q6: What is returned by getDay() if the date is a Sunday?

A. 1
B. 6
C. 0
D. 7

Q7: What is the return type of new Date().toISOString()?

A. Number
B. Object
C. Date
D. String

Q8: How can you change the date to 25 using a method?

A. setDay(25)
B. setDate(25)
C. setMonth(25)
D. setFullDate(25)

Q9: What does getHours() return?

A. Number from 0 to 11
B. Number from 1 to 12
C. Number from 0 to 23
D. Time string

Q10: Which method returns localized date like "7/10/2025" (based on your system)?

A. toString()
B. toDateString()
C. toLocaleDateString()
D. toISOString()

Go Back Top