Date and Time in JavaScript β€” Easy Guide

Himmat Kumar Mar 30, 2025, 12:16 PM
Blog Thumbnail

🧠 JavaScript Date and Time β€” The Easy Way

Working with dates and times is super important in JavaScript β€” whether you're showing the current time, scheduling something, or formatting a birthday. JavaScript has a built-in Date object to help you out.

πŸ“… Creating a Date

let now = new Date();
console.log(now); // current date and time
let specificDate = new Date("2023-10-01");
console.log(specificDate); // Sun Oct 01 2023

πŸ•΅οΈβ€β™‚οΈ Getting Date Parts

Use these methods to grab specific parts of the date:

let today = new Date();
console.log(today.getFullYear());  // 2025
console.log(today.getMonth());     // 0-11 (0 = Jan)
console.log(today.getDate());      // 1-31
console.log(today.getDay());       // 0-6 (0 = Sunday)
console.log(today.getHours());     // 0-23
console.log(today.getMinutes());   // 0-59
console.log(today.getSeconds());   // 0-59

πŸ› οΈ Setting Date Values

let birthday = new Date();
birthday.setFullYear(1995);
birthday.setMonth(11); // December
birthday.setDate(25);
console.log(birthday);

πŸ“¦ Formatting a Date

Turn it into something readable for your users:

let date = new Date();
console.log(date.toDateString()); // "Sat Mar 29 2025"
console.log(date.toLocaleDateString()); // formatted by locale
console.log(date.toLocaleTimeString()); // only time

⏱️ Timestamps

let now = new Date();
console.log(now.getTime()); // milliseconds since Jan 1, 1970

let later = new Date();
console.log(later - now); // difference in milliseconds

βœ… TL;DR β€” Date Method Cheatsheet

  • new Date() β€” create a new date object
  • getFullYear() β€” get the year
  • getMonth() β€” get month (0–11)
  • getDate() β€” get day of month
  • getDay() β€” get day of week
  • setFullYear(), setMonth(), etc. β€” set date parts
  • toDateString(), toLocaleDateString() β€” format dates
  • getTime() β€” get timestamp (ms)
πŸ’‘ Pro Tip: Always double-check months β€” JavaScript counts them from 0 (January) to 11 (December)!

Comments

Please login to leave a comment.

No comments yet.

Related Posts

introduction-to-javascript
Himmat Kumar β€’ Oct 27, 2023, 11:36 AM

Introduction JavaScript

difference-beetween-javascript-and-jquery-with-example
Himmat Kumar β€’ Oct 21, 2024, 12:05 PM

Difference between javascript and jquery with examples

javascript-variables-var-let-const-explained
Himmat Kumar β€’ Mar 25, 2025, 12:03 PM

JavaScript Variables: var vs let vs const

javascript-data-types-explained
Himmat Kumar β€’ Mar 25, 2025, 12:17 PM

JavaScript Data Types Explained with Examples

javascript-operators-arithmetic-comparison-logical
Himmat Kumar β€’ Mar 25, 2025, 12:31 PM

JavaScript Operators Explained | Arithmetic, Comparison...

javascript-loops-for-while-do-while
Himmat Kumar β€’ Mar 29, 2025, 5:43 AM

JavaScript Loops β€” for, while, do...while

javascript-functions-and-scope-guide
Himmat Kumar β€’ Mar 30, 2025, 5:26 AM

JavaScript Functions & Scope

nextjs-vs-react-differences
6050 viewsnextjs
Himmat Regar β€’ Jun 27, 2025, 11:09 AM

Next.js vs React: What’s the Difference and When to Use...