The source code for this blog is available on GitHub.
Note
Top

Javascript miscellaneous date object

Cover Image for Javascript miscellaneous date object
Chen Han
Chen Han

Does timestamp in taiwan will be the same as the timestamp in log angeles

Conclusion: Timestamp can be a medium. The same timestamp has corresponding Taiwan time and los angeles time

Q: Can I use medium here if i say there is a connection between time in different timezone

A: You can use Medium to write about the connection between time in different timezones. You can explore how different timezones affect the way people work and communicate, and discuss the challenges and solutions related to managing time in a globalized world. You can also dive deeper into the technical aspects of timezones, such as how they are determined and how they have evolved over time.

If the time is the same but the timezone is different

The timestamp in Taiwan will not be the same as the timestamp in Los Angeles. This is because Taiwan and Los Angeles are in different time zones. Taiwan is in the GMT+8 time zone, while Los Angeles is in the GMT-7 time zone. Therefore, the time in Los Angeles is 15 hours behind the time in Taiwan. If you want to convert the time in one time zone to another, you will need to use a time zone conversion tool or library.

Let's assume we want to compare the timestamp for January 1st, 2022 in Taiwan and Los Angeles. Here's an example code snippet in JavaScript:

const taiwanDate = new Date('2022-01-01T00:00:00+08:00');
const laDate = new Date('2022-01-01T00:00:00-08:00');

console.log('Taiwan timestamp:', taiwanDate.getTime());
console.log('Los Angeles timestamp:', laDate.getTime());

// Taiwan timestamp: 1640966400000
// Los Angeles timestamp: 1640980800000

This code creates two date objects, one for Taiwan and one for Los Angeles, both with the same date and time (January 1st, 2022 at midnight). We use the getTime() method to get the timestamp in milliseconds since January 1st, 1970 (also known as Unix time).

As we can see, the timestamps are different, because they represent the same moment in time in different time zones. The timestamp for Taiwan is 1640966400000 (which corresponds to 12:00am on January 1st, 2022 in Taiwan), while the timestamp for Los Angeles is 1640980800000 (which corresponds to 12:00am on January 1st, 2022 in Los Angeles).

If the time in the same but the timezone is different

If we don't consider the milliseconds and only consider the timezone and date/time values, then the timestamps for the same date and time in different timezones will be the same.

const taiwanDate = new Date('2022-01-01T16:00:00+08:00');
const laDate = new Date('2022-01-01T00:00:00-08:00');

console.log('Taiwan timestamp:', taiwanDate.getTime());
console.log('Los Angeles timestamp:', laDate.getTime());

The timestamps are the same because both dates represent the same moment in time, even though they are in different timezones.

Casting Time in Los Angeles

const date = new Date();
const options = {
  timeZone: "America/Los_Angeles",
};
const losAngelesTime = date.toLocaleString("en-US", options);

console.log(losAngelesTime);  // 3/26/2023, 10:35:08 PM

Content from Wei

顯示當地時間

const time = "Fri Mar 24 2023 08:32:55+08:00"; 

有沒有後面這個GMT + 0800不影響時間(瀏覽器顯示),但有沒有+07:00 就差很多

let localTimeObj = new Date(time); // Fri Mar 24 2023 07:57:00 GMT+0800

特別說明這個time,要解釋的話可以拆開成兩部分,(1):Fri Mar 24 2023 08:32:55 (2):+08:00

這個(1)就是我得到的時間是基於,(2)時區(經過+或-)所得到的時間,換句話說就是UTC時間加上

或減少(2)時間所得到的,只是得到(1)後會在後面附上(2)讓你知道現在所在的時區為何

換個範例假設

time 換成"Fri Mar 24 2023 08:32:55+07:00"並且在我的電腦上(時區是+8時區),那麼在做

new Date(time) 時會得到什麼時間呢?

會得到 Fri Mar 24 2023 09:32:55 GMT+0800 (台北標準時間) ,因為new Date()會將我們傳進去的

時間自動轉換成當地的時間特性 可以自己先將時間換成UTC的形式等於

Fri Mar 24 2023 01:32:55Z (先減7,因為是基於+7所得到的時間),再來本地是+8時區再配合

new Date(),就會變成 Fri Mar 24 2023 09:32:55 GMT+0800 (台北標準時間)

let localISOString = localTimeObj.toISOString(); 

2023-03-24T00:32:55.000Z 沒有帶有時間偏移的UTC時間

練習範例,分別會顯示出什麼的時間為什麼,假設都在 + 8 的時區

const isoStringWithOffset = "2023-03-22T13:00:00+08:00"; 

const isoStringWithoutOffset = "2023-03-22T05:00:00.000Z"; 

console.log("1", new Date(isoStringWithOffset));    // Wed Mar 22 2023 13:00:00 GMT+0800
console.log("2", new Date(isoStringWithoutOffset)); // Wed Mar 22 2023 13:00:00 GMT+0800 

萬用公式,計算當地時區偏移量

const offset = new Date().getTimezoneOffset(); 

const timeZoneOffset = (offset > 0 ? "-" : "+") + ("00" + Math.abs(offset / 60)).slice(-2) + ":" + ("00" + Math.abs(offset % 60)).slice(-2); 

console.log("當地時間/OBJ型式 :", localTimeObj); //Fri Mar 24 2023 08:32:53 GMT+0800 (台北標準時間) 
console.log("當地時間/OBJ型式的localString :", localTimeObj.toLocaleString()); 
console.log("當地時間/toISOString", localISOString); //2023-03-24T00:32:55.000Z 
console.log(" offset:", offset); console.log(" timeZoneOffset:", timeZoneOffset); 
console.log(new Date()); // 當下的時間,格式為 Fri Mar 24 2023 10:24:10 GMT+0800 (台北標準時間) 

console.log(new Date().toLocaleString()); //當下時間,格式為2023/3/24 上午10:24:30 console.log(new Date().toISOString()); //當下時間的UTC,格式為2023-03-24T02:24:42.947Z 

以下的寫法解釋為,基於本地電腦端的時間,顯示美國洛杉磯的當地時間,與ISOS

ISOS 合理知道不管到哪一區,只要是顯示UTC都會是一樣的

// toLocaleString會顯示當地美國的時間 

const date = new Date("2023-03-24 11:20"); 
const losString = date.toLocaleString("en-US", { timeZone: "America/Los_Angeles", }); 
const dateISOS = date.toISOString(); const losISOS = date.toISOString("en-US", { timeZone: "America/Los_Angeles", }); 

console.log("losString", losString); // 3/23/2023, 8:35:00 PM 
console.log("losISOS", losISOS); // 2023-03-24T03:35:00.000Z 
console.log("dateISOS", dateISOS); //2023-03-24T03:35:00.000Z 

當我想固定美國洛杉磯的時間,然後要得到各個本地端時區的時間 。還記得new Date()會根據自身電腦的時區做轉換嗎,所以localDate得到的就是本地端的時間date

// los 2023-03-23 20:20 
// tai 2023-03-24 11:20 

const losTime = "2023-03-23T20:20:00-07:00"; 
const losTime2 = "2023-03-23T20:20:00"; 

const localDate = new Date(losTime); 
const localTimeString = localDate.toLocaleString(); 

// 特別獲得基於美國時間的tokyo時間 

const tokyoTime = new Date( localDate.toLocaleString("en-US", { timeZone: "Asia/Tokyo" }) ); 
console.log("localDate", localDate); 
console.log("localTimeString", localTimeString); 
console.log("tokyoTime", tokyoTime); 

防踩雷部分如果要計算時間,有沒有在時間上後面加上Z結果會完全不一樣喔

範例一 : 這兩個顯示的時間會相等,是同樣的東西

console.log(new Date("2020-06-26 09:19:00Z")); 

代表的是UTC + 0 ,在本地電腦會自動+8時區換算

console.log(new Date("2020-06-26 17:19:00+08:00")); //代表的是UTC + 8 

範例二 : 兩個個log不會相等

console.log(new Date("2020-06-26 09:19:00"));// 就是真的是拿到這個時間的stamp,不會做任何的轉換 
console.log(new Date("2020-06-26 17:19:00+08:00")); 
© 2024 WOOTHINK. All Rights Reserved.
Site MapTerms and ConditionsPrivacy PolicyCookie Policy