What To Know of the New Temporal API for JS

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@simplestack·
0.000 HBD
What To Know of the New Temporal API for JS
<center>
![image.png](https://files.peakd.com/file/peakd-hive/simplestack/23vsBiE7g4mymxRePrRD8Bp65UGurkJCdEejC513xaxfGFcdzSmq1eW58fosdExZWEvXA.png)
</center>

The Temporal object provides a comprehensive way to work with dates and times. It handles everything from time zones and calendars to time conversions, calculations, and formatting. Temporal is intended to completely replace the older Date object.

# Description

<center>
![image.png](https://files.peakd.com/file/peakd-hive/simplestack/AJpiZbPkEYtC8g9JM2grhebfj1wDmLCUWmPJGij2xCxM9zV9QaN5f1JnM91TwvD.png)
</center>

Imagine Temporal is like a toolbox full of specialized tools for working with dates and times. It’s not a tool itself, so you can’t make a Temporal. Instead, you use the tools inside the toolbox.

Just like the Math toolbox has tools like **Math.sqrt()** (for square roots), the **Temporal** toolbox has tools like **Temporal.Now()** (for getting the current time).

There are a lot of tools in there, so it might seem complicated at first, but we'll show you how they all fit together.

# Background and concepts

<center>
![image.png](https://files.peakd.com/file/peakd-hive/simplestack/AJec1Pc6u9Ej1h96E9i274GzY3V9nuy3gU3GdYXhsJk6AHi5RpYmnEJxmVcBwJu.png)
</center>

Imagine JavaScript’s old way of dealing with dates and times, the Date object, like a toolbox that's been around since the very beginning.

It's like that old, rusty toolbox your grandpa gave you – it works sometimes, but it's based on an even older, not-so-great toolbox from Java (think of it like a tool design that everyone realized was flawed).

Even though Java fixed their tool design a long time ago, JavaScript's stuck with the old one to avoid breaking any old code.

Now, everyone knows working with dates and times is hard. It’s easy to make mistakes! The Date toolbox has lots of tools, but that's actually the problem.

It's so cluttered and messy that it's hard to know which tool to use, and you end up accidentally using the wrong one and making a mess. A good toolbox isn't just about having lots of tools; it's about having the right tools, clearly organized, so you don't accidentally grab the wrong wrench.

That's why even though JavaScript could have just added more tools to the old **Date** toolbox, they realized they needed a whole new, better-designed toolbox – that's where the Temporal API comes in.

Imagine a date and time like a point on a timeline. The old **Date** object in JavaScript tries to do two things at once with that point:

- **It’s like a stopwatch:** It measures how much time has passed since a specific starting point (like the very beginning of time for computers). This measurement is in tiny units called milliseconds.
- **It’s like a calendar and clock:** It also breaks down that point in time into familiar pieces: year, month, day, hour, minute, second, etc. But these pieces only make sense if you know which calendar you’re using (like the Gregorian calendar we use every day) and which time zone you’re in (because 3 PM in New York is different than 3 PM in London!). The **Date** object lets you look at and change these pieces.

# Old Shortcomings

<center>
![image.png](https://files.peakd.com/file/peakd-hive/simplestack/AK9NDvtc6N3skisfJ6HSNKhj48fAQxQqkYNBFew9dDqJXcgTRj2Z2XMAZqgFB4k.png)
</center>

Imagine you’re trying to schedule a meeting with someone in another country. Time zones can be a real headache! The old **Date** object in JavaScript only lets you work with two time zones: the standard "worldwide" time (UTC) and the time on your computer. That's it! It's like having a clock that can only show two times – not very helpful.

What if you want to set an alarm for 8 AM? You don’t care if it’s daylight saving time or if you’re traveling — you just want it to go off at 8 AM local time, no matter what. The old **Date** object can't handle this "wall-clock time" – a time you just read off a clock or calendar without worrying about time zones. It's like trying to set your alarm with a clock that's constantly changing time zones.

Another problem is calendars. We usually use the Gregorian calendar (with BC and AD, 12 months, leap years, etc.). But other cultures have different calendars, like the Hebrew or Chinese calendars. The old **Date** object only knows about the Gregorian calendar. It's like having a calendar that only shows holidays for one specific country – not very useful if you need to know holidays from another culture. So, the old **Date** object struggles with both time zones and different calendar systems, making it difficult to accurately handle dates and times in many real-world situations.

The old **Date** object in JavaScript has even more problems. Imagine you have a toy car, and you can change its color. With the old **Date** object, it's like every time you change any part of the date (like the day), it also changes other parts unexpectedly, like the time or even the year! These unwanted changes are called "side effects," and they can be very frustrating.

Another issue is how dates and times are written as text. It’s like trying to read a messy handwritten note — sometimes you can understand it, sometimes you can’t. The old **Date** object can't write dates and times in a way that's always clear and easy for computers to understand. This makes it hard to reliably share dates and times between different programs or systems.

# Conclusion

<center>
![image.png](https://files.peakd.com/file/peakd-hive/simplestack/AKp8Y7SvhazuBmPxnWcZRH6GUDVmVfPrzz5twdJ59yekpNEpNzVUY7tFAcsAVtB.png)
</center>

Basically, the old **Date** object has so many built-in problems that it's like trying to fix a leaky, rusty old car. You could try patching it up, but eventually, it's easier and better to just build a new car from scratch. That's exactly what Temporal is – a brand new, well-designed way to handle dates and times in JavaScript, built from the ground up to avoid all the problems of the old **Date** object.

---

*If you liked this article I’d appreciate an upvote or a comment. That helps me improve the quality of my posts as well as getting to know more about you, my dear reader.*

*Muchas gracias!*

*Follow me for more content like this.*

*[X](https://twitter.com/edca3911) | [PeakD](https://peakd.com/@simplestack) | [Rumble](https://rumble.com/user/simplestack) | [YouTube](https://www.youtube.com/@simple-stack-by-ed) | [Linked In](https://www.linkedin.com/in/edwardcasanova/) | [GitHub](https://github.com/ed3899) | [PayPal.me](https://paypal.me/edca3899?country.x=MX&locale.x=es_XC) | [Medium](https://medium.com/@ed.wacc1995/subscribe)*

*Down below you can find other ways to tip my work.*

```
BankTransfer: "710969000019398639", // CLABE
BAT: "0x33CD7770d3235F97e5A8a96D5F21766DbB08c875",
ETH: "0x33CD7770d3235F97e5A8a96D5F21766DbB08c875",
BTC: "33xxUWU5kjcPk1Kr9ucn9tQXd2DbQ1b9tE",
ADA: "addr1q9l3y73e82hhwfr49eu0fkjw34w9s406wnln7rk9m4ky5fag8akgnwf3y4r2uzqf00rw0pvsucql0pqkzag5n450facq8vwr5e",
DOT: "1rRDzfMLPi88RixTeVc2beA5h2Q3z1K1Uk3kqqyej7nWPNf",
DOGE: "DRph8GEwGccvBWCe4wEQsWsTvQvsEH4QKH",
DAI: "0x33CD7770d3235F97e5A8a96D5F21766DbB08c875"
```
👍 , , , , ,