MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Working with Date Object0%
Study Materials
JavaScript for BeginnersProgress 0%
1.1What is JavaScript?1.2How JavaScript Works in Browser1.3Setting up Environment: VSCode, Live Server
9.1Working with Date Object9.2Math Object Basics9.3Random Numbers Example
HomeTutorialsJavaScript for BeginnersWorking with Date Object
Math Object Basics

Working with Date Object

Beginner12 min read•Updated: 2026-09-15•By Er. Sumit Kumar (MSK Institute)
Study Materials

Working with the Date Object in JavaScript

Handling timestamps, calculating elapsed time, and formatting dates are essential skills for building web applications (booking engines, analytics dashboards, expiration timers). JavaScript provides the built-in Date object to manage dates and times.


1. Creating Date Instances

A JavaScript Date object represents a single moment in time, measured in milliseconds since the Unix Epoch (January 1, 1970, 00:00:00 UTC).

1// 1. Current date and time:
2const now = new Date();
3console.log(now); // e.g. Mon Sep 15 2026 10:45:00 GMT+0530
4 
5// 2. From an ISO 8601 date string:
6const eventDate = new Date("2026-09-15T09:00:00");
7 
8// 3. From individual arguments: (Year, MonthIndex, Day, Hours, Minutes, Seconds)
9// ⚠️ WARNING: Month is 0-indexed! 0 = January, 8 = September, 11 = December!
10const customDate = new Date(2026, 8, 15, 10, 30);
11 
12// 4. From Epoch Milliseconds:
13const fromEpoch = new Date(0); // Jan 01 1970

2. Getting Date & Time Components

1const d = new Date();
2 
3console.log(d.getFullYear()); // 2026
4console.log(d.getMonth()); // 8 (September, 0-indexed!)
5console.log(d.getDate()); // 15 (Day of the month, 1-31)
6console.log(d.getDay()); // 1 (Day of week: 0=Sunday, 1=Monday... 6=Saturday)
7console.log(d.getHours()); // Current hour (0-23)
8console.log(d.getMinutes()); // Current minute (0-59)
9console.log(d.getSeconds()); // Current second (0-59)
10console.log(d.getTime()); // Milliseconds since Jan 1, 1970

3. Calculating Time Differences (Elapsed Time)

Because subtracting dates yields the difference in milliseconds, time calculations are straightforward:

1const startDate = new Date("2026-01-01");
2const endDate = new Date("2026-01-11");
3 
4const diffInMs = endDate - startDate; // Milliseconds difference
5const diffInDays = diffInMs / (1000 * 60 * 60 * 24);
6 
7console.log(`Course duration: ${diffInDays} days`); // 10 days

4. Modern Formatting: Intl.DateTimeFormat

Instead of clumsy manual string assembly, modern browsers provide the powerful Internationalization API (Intl):

1const today = new Date();
2 
3// Format for India (English):
4const formatterIN = new Intl.DateTimeFormat("en-IN", {
5 dateStyle: "full",
6 timeStyle: "short"
7});
8console.log(formatterIN.format(today));
9// "Tuesday, 15 September, 2026 at 10:45 AM"
10 
11// Format for United States:
12const formatterUS = new Intl.DateTimeFormat("en-US", {
13 month: "short",
14 day: "numeric",
15 year: "numeric"
16});
17console.log(formatterUS.format(today));
18// "Sep 15, 2026"

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
In the JavaScript Date object, what number represents the month of January?
Question 2
What does date.getDay() return?
Question 3
What is the "Unix Epoch"?
Question 4
How do you get the current timestamp in milliseconds without creating a full new Date() object?
Question 5
What modern built-in API is recommended for locale-sensitive, human-readable date and time formatting?
Next Lesson

Math Object Basics

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Iterating over Arrays and ObjectsMath Object Basics

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Beginners with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext