Date and Time in JavaScript — Easy Guide

Himmat Kumar Jun 1, 2025, 1:17 AM
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)!

Related Posts

Himmat Kumar May 31, 2025, 7:47 PM

JavaScript Variables: var vs let vs const

Himmat Kumar May 31, 2025, 7:47 PM

JavaScript Data Types Explained with Examples

Himmat Kumar May 31, 2025, 7:47 PM

JavaScript Loops — for, while, do...while

Himmat Kumar May 31, 2025, 7:47 PM

JavaScript Functions & Scope

Himmat Kumar May 31, 2025, 7:47 PM

Introduction JavaScript

112 viewsLaravel
Himmat Kumar Jun 1, 2025, 12:53 AM

Master Laravel: Basic System Requirement,Installation a...