LEARNING JAVASCRIPT - Trang 243

CHAPTER 15

Date and Time

Most real-world applications involve working with date and time data. Unfortunately,

JavaScript’s

Date

object (which also stores time data) is not one of the language’s best-

designed features. Because of the limited utility of this built-in object, I will be intro‐

ducing Moment.js, which extends the functionality of the

Date

object to cover

commonly needed functionality.
It’s an interesting bit of history that JavaScript’s

Date

object was originally imple‐

mented by Netscape programmer Ken Smith—who essentially ported Java’s

java.util.Date

implementation into JavaScript. So it’s not entirely true that Java‐

Script has nothing to do with Java: if anyone ever asks you what they have to do with

each other, you can say, “Well, aside from the

Date

object and a common syntactic

ancestor, very little.”
Because it gets tedious to keep repeating “date and time,” I will use “date” to implicitly

mean “date and time.” A date without a time is implicitly 12:00 A.M. on that day.

Dates, Time Zones, Timestamps, and the Unix Epoch

Let’s face it: our modern Gregorian calendar is a fussy, overcomplicated thing, with 1-

based numbering, odd divisions of time, and leap years. Time zones add even more

complexity. However, it’s (mostly) universal, and we have to live with it.
We’ll start with something simple: the second. Unlike the complicated division of

time in the Gregorian calendar, seconds are easy. Dates and times—as represented by

seconds—are a single number, neatly ordered on a number line. Representing dates

and time in seconds is therefore ideal for computation. However, it doesn’t work so

well for human communication: “Hey, Byron, want to have lunch at 1437595200?”

(1437595200 is Wednesday, July 22, 2015, at 1 P.M. Pacific Standard Time). If dates

219

Liên Kết Chia Sẽ

** Đây là liên kết chia sẻ bới cộng đồng người dùng, chúng tôi không chịu trách nhiệm gì về nội dung của các thông tin này. Nếu có liên kết nào không phù hợp xin hãy báo cho admin.