Date is weird in JavaScript. It gets on our nerves so much that we reach for libraries (lượt thích Date-fns and Moment) the moment (ha!) we need khổng lồ work with date và time.

Bạn đang xem: Javascript date

But we don’t always need khổng lồ use libraries. Date can actually be quite simple if you know what khổng lồ watch out for. In this article, I’ll walk you through everything you need khổng lồ know about the Date object.

First, let’s acknowledge the existence of timezones.


There are hundreds of timezones in our world. In JavaScript, we only care about two—Local Time & Coordinated Universal Time (UTC).

Local time refers lớn the timezone your computer is in.UTC is synonymous with Greenwich Mean Time (GMT) in practice.

By mặc định, almost every date method in JavaScript (except one) gives you a date/time in local time. You only get UTC if you specify UTC.

Xem thêm: sr là gì trên facebook

With this, we can talk about creating dates.

Creating a date

You can create a date with new Date(). There are four possible ways khổng lồ use new Date():

With a date-stringWith date argumentsWith a timestampWith no arguments

The date-string methodIn the date-string method, you create a date by passing a date-string into lớn new Date.

30 + 3 = 33. 33rd March gets converted to lớn 2nd April automatically.And that’s everything you need khổng lồ know about JavaScript’s native Date object.

Interested lớn learn more JavaScript?

If you found this intro khổng lồ Date useful, you might love sầu Learn JavaScript, a course I created to teach people everything they need to lớn know about JavaScript.

In the course, I cover the basic concepts of what you need to lớn know, then I show you how khổng lồ use the concepts you learned khổng lồ build real-world components.

Have a look. You might find it helpful.

In the meantime, if you have sầu any JavaScript questions, feel free khổng lồ liên hệ me. I’ll vị by best lớn create free articles khổng lồ answer your questions.