Huli
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 淺談時間 ## 重點一:timestamp 的意義 timestamp 基本上有兩種,差在精確度 JS 的 timestamp 是最詳細的,一共有 13 位數,代表著:「從 UTC +0 1970-01-01 開始,過了多少`毫秒`」 ``` 1593755426950 ``` 另外一種則是少了 3 位數,只有 10 位數,代表著:「從 UTC +0 1970-01-01 開始,過了多少`秒`」 ``` 1593755426 ``` 所以你會發現有些地方需要把東西 / 1000 或者是 * 1000,就是在做毫秒的轉換。 ## 重點二:timestamp 是中立的,跟時區無關 這句話的意思是,你今天拿到一個 timestamp 之後,你不會知道應該要顯示在什麼時區,因為同一個 timestamp 可以代表不同地區的時間。 舉例來說,1593163158 這個 timestamp 代表的是: UTC +0 時區的 `2020-06-26 09:19:00` 同時也代表著: UTC +8 時區的 `2020-06-26 17:19:00` 所以同一個 timestamp,在不同時區會被轉譯成不同的時間。 ## 重點三:ISO 標準格式 在實務上不一定會直接傳 timestamp,有可能會用另外一些可讀性比較高的格式字串,常看到的可能會有: 1. `2020-02-02` 2. `2020-02-02 13:00:00` 看起來都十分合理,但其實用的時候都要小心,因為這些不一定是標準的 ISO 格式,如果不是的話,被解析時可能就會有些問題。 以第一種為例,可以猜猜看下面這兩行的結果是什麼: ``` js new Date('2020-02-02') new Date('2020/02/02') ``` . . . . . . . 答案是: ``` js new Date('2020-02-02') // Sun Feb 02 2020 08:00:00 GMT+0800 (台北標準時間) new Date('2020/02/02') // Sun Feb 02 2020 00:00:00 GMT+0800 (台北標準時間) ``` 如果是用 `-` 隔開,會被當作是「UTC +0 時區的 2 月 2 號 00:00:00」 如果是用 `/` 隔開,會被當作是「本地時區的 2 月 2 號 00:00 :00」 因為我們在 +8 時區,所以這兩種的結果會差 8 個小時。 再來看看第二種格式:`2020-02-02 13:00:00` 這種格式的問題在於,在 Safari 上面會直接報錯給你看,回傳一個 `Invalid date`,不過這也滿合理的,因為它不是一個標準的格式。 你必須加一個 `T` 才能正確解析: ``` js new Date('2020-02-02 13:00:00') // invalid date new Date('2020-02-02T13:00:00') // Sun Feb 02 2020 21:00:00 GMT+0800 (CST) ``` 那這樣就沒了嗎?不是。以上面那個字串為例,它在 Safari 上面與 Chrome 上面解析的結果不一樣: ``` js // Chrome new Date('2020-02-02T13:00:00') // Sun Feb 02 2020 13:00:00 GMT+0800 (台北標準時間) // Safari new Date('2020-02-02T13:00:00') // Sun Feb 02 2020 21:00:00 GMT+0800 (CST) ``` 就跟前面講的 `-` 與 `/` 類似,如果沒有在 date string 裡面指定時區,會有兩種情況: 1. Chrome 自動解析為:「本地時區」的 13:00 2. Safari 自動解析為:「UTC +0 時區」的 13:00,所以換到 +8 就變成 21:00 了 而這上面這麼多坑,其實都是因為沒有正確利用 ISO 標準格式所導致,如果用了 ISO 的格式:`YYYY-MM-DDTHH:mm:ssZ` 就沒這問題了。 只要在最後面加個 Z,就代表 UTC+0 的時區,所以解析就會正常。當然,你也可以傳不同時區進去。 ``` js new Date('2020-02-02T13:00:00Z') // Sun Feb 02 2020 21:00:00 GMT+0800 (台北標準時間) new Date('2020-02-02T13:00:00+08:00') // Sun Feb 02 2020 13:00:00 GMT+0800 (台北標準時間) ``` ## 重點四:date string 的時區小陷阱 請問以下程式碼的結果會是什麼? ``` js luxon.DateTime .fromISO('2020-02-02T13:00:00+03:00') .toFormat('HH:mm:ss') ``` . . . . . . . 答案: ``` 18:00:00 ``` date string 帶時區,就會讓你誤以為 format 出來的結果會跟著這時區,但實際上卻不是這樣。toFormat 的結果會是本地時區。 可以簡單想成任何的 date string 在做處理時,內部都會先轉成 timestamp,然後再用「本地時區」來處理。 所以: `fromISO('2020-02-02T13:00:00+03:00').toFormat('HH:mm:ss')` 其實就跟 `fromMills(1580637600000).toFormat('HH:mm:ss')` 是一樣的 ## 那到底應該怎麼做? 如果你今天拿到一個 date string,你在 format 的時候想用原來的時間來做 format,可以這樣: ``` js luxon.DateTime .fromISO('2020-02-02T13:00:00+03:00', { setZone: true }) .toFormat('HH:mm:ss') // 13:00:00 ``` 如果你今天拿到一個 date string,你想要用「固定時區」來轉換,可以這樣: ``` js luxon.DateTime .fromISO('2020-02-02T13:00:00+03:00', { zone: 'Asia/Tokyo' }) .toFormat('HH:mm:ss') ``` 或者是: ``` js luxon.DateTime .fromISO('2020-02-02T13:00:00+03:00') .setZone('Asia/Tokyo') .toFormat('HH:mm:ss') ``` 總結一下在多個時區的時候的處理: 1. Backend 永遠回傳 ISO 標準格式:`YYYY-MM-DDTHH:mm:ssZ` 2. 前端在「顯示」時,轉成需要的時區

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully