πŸ’‘ Key Takeaways

⏰ Creating Dates: new Date() creates a date object from string, timestamp, or components.
⚑ Modify Dates: Use setFullYear(), setMonth(), etc., to change parts of a date.
🌎 Timestamps & UTC: Date.now() gives milliseconds since 1970-01-01; use getUTC*() for UTC times.
🎯 Formatting: toLocaleDateString(), toISOString(), and toTimeString() make dates readable.

πŸ”Ή Creating Dates

  • Create current date/time with new Date()
  • Use timestamps (ms since Jan 1 1970)
  • Build custom dates with year, month, day, etc.
const now = new Date(); // current
const fromMs = new Date(1697040000000);
const custom = new Date(2025, 9, 11, 15, 30);

πŸ”’ Accessing Date Components

  • Use getters like getFullYear(), getMonth(), getDate()
  • getDay() returns weekday (0 = Sun)
  • getHours(), getMinutes(), getSeconds() for time
const now = new Date();
console.log(now.getFullYear(), now.getMonth(), now.getDate());
console.log(now.getDay(), now.getHours(), now.getMinutes());

✏️ Modifying Dates

  • Use setters like setFullYear(), setMonth(), setDate()
  • Change hours, minutes, or seconds directly
  • Great for scheduling or adjustments
const date = new Date();
date.setFullYear(2026);
date.setMonth(0); // January
date.setDate(1);
date.setHours(12, 0, 0);

🌎 Timestamps and UTC

  • Date.now() β†’ current timestamp (ms)
  • Use getUTC*() for universal time
  • Convert timestamps back to Date
const ts = Date.now();
const now = new Date(ts);
console.log(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate());

πŸ“ Formatting Dates

  • toISOString() β†’ standardized output
  • toLocaleDateString() β†’ user’s region format
  • Combine parts manually for custom strings
const now = new Date();
console.log(now.toISOString());
console.log(now.toLocaleString());
const custom = `${now.getFullYear()}-${now.getMonth()+1}-${now.getDate()}`;

πŸ”„ Date Math

  • Add or subtract days with setDate()
  • Subtract two dates to get difference in ms
  • Convert milliseconds β†’ days/hours easily
const today = new Date();
today.setDate(today.getDate() + 5);

const d1 = new Date("2025-10-11");
const d2 = new Date("2025-10-01");
const diffDays = (d1 - d2) / (1000 * 60 * 60 * 24);

Disclaimer: The information provided on this website is for educational and informational purposes only. Health-related content is not intended to serve as medical advice, diagnosis, or treatment recommendations and should not replace consultation with qualified healthcare professionals. Financial content is for educational purposes only and does not constitute financial advice, investment recommendations, or professional financial planning services. Always consult with licensed healthcare providers for medical concerns and qualified financial advisors for personalized financial guidance.