Skip to content

[JavaScript] How to Compare and Operate the Time of “Date()”

When we comparing two time point or operating the time, using Date() function as much as possible can make development much easier. The following I will follow:

  • Time initialization
  • Time to time comparison
  • Addition and subtraction of time

These three small topics are recorded. I hope that when I need to use JavaScript to manipulate time again, I can quickly wake up my memory by reading this note.


Time Initialization

First, let’s take a look for how to initialize the time. In JavaScript, we can use Date() to create a time that includes a date.

Get the current time

var now = new Date();
console.log(now);


Output:

2021-12-22T11:36:35.078Z


Initialize the specific time

you can use yyyy-mm-dd format to initialize the time, or input the specific year, month and day.

可以使用 yyyy-mm-dd 的格式去初始化、或是輸入指定的月份、日期、年份。

var date_01 = new Date("December 02, 1994");
var date_02 = new Date("1994-12-02");
var date_03 = new Date("1994/12/02");

console.log(date_01);
console.log(date_02);
console.log(date_03);


Output:

1994-12-02T00:00:00.000Z
1994-12-02T00:00:00.000Z
1994-12-02T00:00:00.000Z


All of these methods are worked.


Set the specific time

We can use setHours() to set the specific time (or you want to use hh:mm:ss format).

// Init
var date_01 = new Date("1994-12-02");
var date_02 = new Date("1994/12/02");

// setHours(H, M, S)
date_01.setHours(12, 30, 0);
date_02.setHours(17, 45, 0);

// Print
console.log(date_01);
console.log(date_02);


Output:

1994-12-02T12:30:00.000Z
1994-12-02T17:45:00.000Z

Time to Time Comparison

As long as the data type that stores the time is Date(), we can easily use > or < to compare two different times. Conceptually, dates that are later in time will be larger.

// Init
var date_01 = new Date("1994-12-02");
var date_02 = new Date("1994/12/02");

// setHours(H, M, S)
date_01.setHours(12, 30, 0);
date_02.setHours(17, 45, 0);

// Compaison
if (date_01 < date_02) {
    console.log("date_01 is earlier.");
}
else {
    console.log("date_01 is earlier.");
}


Output:

date_01 is earlier.



Addition and Subtraction of Time

If the addition and subtraction of time is done by hand, it will be quite troublesome. After all, you may need to consider whether the date, month, and year are correspondingly changed. But in fact, we can first convert the Date() time into timestamp, add or subtract the corresponding seconds, and then read the new timestamp in the data type of Date().

Which will be our new time.

So, what exactly is a Unix timestamp? It is actually the total number of seconds since 1970-01-01 00:00:00 GMT until the specified time.

Even in different time zones, the same timestamp means they are the same time, which means that we can easily manipulate the time as long as it is converted into a timestamp, which is very convenient.

// Init
var date = new Date("1994-12-02 12:30:00");
var date_ts = date.getTime();
console.log("raw:     ", date)

// -15 minutes
date.setTime(date_ts - 15 * 1000 * 60);
console.log("+15 min: ", date);


// + 2 hours
date.setTime(date_ts + 2 * 1000 * 60 * 60);
console.log("+2 hours:", date);

// -3 Days
date.setTime(date_ts - 3 * 1000 * 60 * 60 * 24);
console.log("-3 Days: ", date);


Output:

raw:      1994-12-02T12:30:00.000Z
+15 min:  1994-12-02T12:15:00.000Z
+2 hours: 1994-12-02T14:30:00.000Z
-3 Days:  1994-11-29T12:30:00.000Z

References


Read More

Leave a Reply