JavaScript

coding learning websites codepractice

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?


JavaScript

online coding class codepractice

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

Go Back Top