DEV Community

Kathirvel S
Kathirvel S

Posted on

JavaScript Dates Finally Make Sense (Here’s How)

Okay, quick question…

Have you ever written some JavaScript code, checked the date… and thought:

“Wait… WHY is it showing the wrong day?? ”

Yeah. We’ve all been there.

Dates in JavaScript have a reputation. They confuse beginners, annoy intermediate devs, and even make experienced developers double-check their code twice.

But today? We’re flipping the script.

By the end of this, you won’t just understand JavaScript dates—you’ll actually enjoy working with them (no kidding).


First Thing You Need to Know (This Changes Everything)

JavaScript doesn’t see dates like “March 25, 2026”.

Instead, it thinks like this:

“How many milliseconds have passed since Jan 1, 1970?”

Yep. That’s how your computer sees time.

Weird? A little.
Powerful? Absolutely.

But wait… why 1970? Why not 2000? Or 1900?


Why JavaScript Counts Time from 1970 (The Real Reason)

Alright, this is where things get interesting.

That starting point—January 1, 1970—is called the Unix Epoch.

Back in the early days of computing, engineers needed a simple, consistent way to represent time across different systems.

Instead of dealing with:

  • months (which have different lengths)
  • leap years (extra chaos )
  • time zones (total madness )

They thought:

“Let’s just count time as a number.”

So they picked a fixed starting point:

📍 Jan 1, 1970, 00:00:00 (UTC)

From there, every moment is just:

number of seconds (or milliseconds) since that point


Why 1970 Specifically?

Good question. It wasn’t random.

  • That’s around the time Unix systems were being developed
  • It was a “modern enough” date for computers back then
  • It made calculations easier within system limits

And since JavaScript was created later, it simply adopted the same system.


What Does This Look Like in JavaScript?

Let’s see it in action:

let now = new Date();
console.log(now.getTime());
Enter fullscreen mode Exit fullscreen mode

Output:

1763788215000
Enter fullscreen mode Exit fullscreen mode

That big scary number?

That’s the number of milliseconds since Jan 1, 1970


What About Dates Before 1970?

Yep, JavaScript handles those too.

let oldDate = new Date("1960-01-01");
console.log(oldDate.getTime());
Enter fullscreen mode Exit fullscreen mode

Output:

-315619200000
Enter fullscreen mode Exit fullscreen mode

Negative value = time before 1970.

So yes… JavaScript can technically time travel to the past too 😄


Why This System Is Actually Genius

At first glance, it feels unnecessary. But this approach makes things:

  • Easy to compare dates
  • Fast for computers to calculate
  • Consistent across systems
  • Less error-prone than manual date math

Instead of comparing messy strings, JavaScript compares clean numbers.

And machines LOVE that.


Let’s Create Your First Date

Try this in your console:

let now = new Date();
console.log(now);
Enter fullscreen mode Exit fullscreen mode

Output:

Wed Mar 25 2026 10:30:15 GMT+0530 (India Standard Time)
Enter fullscreen mode Exit fullscreen mode

Now You just created a real-time date.

Feels like magic, right?


Want to Control Time? Let’s Do It

You’re not stuck with the current time. You can create your own:

let myDate = new Date("2026-03-25");
console.log(myDate);
Enter fullscreen mode Exit fullscreen mode

Output:

Wed Mar 25 2026 05:30:00 GMT+0530
Enter fullscreen mode Exit fullscreen mode

Now let’s get a bit more specific:

let detailedDate = new Date(2026, 2, 25, 10, 30, 0);
console.log(detailedDate);
Enter fullscreen mode Exit fullscreen mode

Output:

Wed Mar 25 2026 10:30:00 GMT+0530
Enter fullscreen mode Exit fullscreen mode

Hold on… why is March = 2?

Because JavaScript decided:

  • 0 = January
  • 1 = February
  • 2 = March

Not logical. But hey… we adapt and move on 😄


Let’s Break the Date Open

Now the fun part—pulling data out:

let today = new Date();

console.log(today.getFullYear());
console.log(today.getMonth());
console.log(today.getDate());
console.log(today.getDay());
Enter fullscreen mode Exit fullscreen mode

Output:

2026
2
25
3
Enter fullscreen mode Exit fullscreen mode

Let’s decode that like detectives

  • 2026 → year
  • 2 → March
  • 25 → actual date
  • 3 → Wednesday

Quick trap (don’t fall into it):

  • getDate() → gives the date
  • getDay() → gives the weekday

Mix these up once… and you’ll never forget again 😅


Making Dates Look Good (Because Ugly Outputs Hurt)

Let’s fix it:

let date = new Date();

console.log(date.toDateString());
console.log(date.toLocaleDateString());
Enter fullscreen mode Exit fullscreen mode

Output:

Wed Mar 25 2026
25/3/2026
Enter fullscreen mode Exit fullscreen mode

Better… but we can go premium:

let options = { year: 'numeric', month: 'long', day: 'numeric' };
console.log(date.toLocaleDateString('en-IN', options));
Enter fullscreen mode Exit fullscreen mode

Output:

March 25, 2026
Enter fullscreen mode Exit fullscreen mode

Now THAT looks like production code


Time to Play with Time

Let’s grab live time values:

let time = new Date();

console.log(time.getHours());
console.log(time.getMinutes());
console.log(time.getSeconds());
Enter fullscreen mode Exit fullscreen mode

Output:

10
30
15
Enter fullscreen mode Exit fullscreen mode

You’re basically building your own clock now.


Date Battle: Which One Comes First?

let date1 = new Date("2026-03-25");
let date2 = new Date("2026-04-01");

console.log(date1 < date2);
Enter fullscreen mode Exit fullscreen mode

Output:

true
Enter fullscreen mode Exit fullscreen mode

JavaScript secretly converts both into numbers and compares them.

Sneaky… but useful.


Time Travel (Yes, You Can)

Let’s jump to tomorrow:

let today = new Date();
today.setDate(today.getDate() + 1);

console.log(today);
Enter fullscreen mode Exit fullscreen mode

Output:

Thu Mar 26 2026 ...
Enter fullscreen mode Exit fullscreen mode

Go back a week:

today.setDate(today.getDate() - 7);
console.log(today);
Enter fullscreen mode Exit fullscreen mode

Output:

Thu Mar 19 2026 ...
Enter fullscreen mode Exit fullscreen mode

You’re officially a time traveler now 🧭


Let’s Build Something Cool: Live Digital Clock

Now we stop learning… and start building


Step 1: Create the UI

<h1 id="clock">00:00:00</h1>
Enter fullscreen mode Exit fullscreen mode

Right now, it’s just a boring static text.

But not for long…


Step 2: Write the Brain

function updateClock() {
  let now = new Date();

  let hours = now.getHours();
  let minutes = now.getMinutes();
  let seconds = now.getSeconds();

  document.getElementById("clock").innerText =
    hours + ":" + minutes + ":" + seconds;
}
Enter fullscreen mode Exit fullscreen mode

This function grabs the current time and injects it into your HTML.


Step 3: Make It Alive

setInterval(updateClock, 1000);
Enter fullscreen mode Exit fullscreen mode

Now your function runs every second.

Which means…

Your clock is LIVE


But Wait… Something Looks Off

You might see:

10:3:5
Enter fullscreen mode Exit fullscreen mode

That’s… not pretty.

Let’s fix it


Step 4: Make It Look Professional

function formatTime(value) {
  return value < 10 ? "0" + value : value;
}

function updateClock() {
  let now = new Date();

  let hours = formatTime(now.getHours());
  let minutes = formatTime(now.getMinutes());
  let seconds = formatTime(now.getSeconds());

  document.getElementById("clock").innerText =
    hours + ":" + minutes + ":" + seconds;
}

setInterval(updateClock, 1000);
Enter fullscreen mode Exit fullscreen mode

Output:

10:03:05
Enter fullscreen mode Exit fullscreen mode

Now THAT looks clean


You Made It (Seriously, That’s Big)

Look at what you just did:

✔ Created dates
✔ Controlled time
✔ Extracted values
✔ Formatted output
✔ Compared dates
✔ Traveled through time
✔ Built a live project

That’s not beginner stuff anymore.


Before You Go…

Next time JavaScript messes up your date…

Don’t panic.

Just smile and say:

“Ahhh… I know your tricks now 😏”

And fix it like a pro.


If this made things click for you even a little…
you’re already ahead of a LOT of developers.

Keep building. Keep breaking things. That’s how you grow!

Top comments (0)