How to ISO 8601 format a Date with Timezone Offset in JavaScript?

前端 未结 11 1319
时光说笑
时光说笑 2020-11-22 09:32

Goal: Find the local time and UTC time offset then construct the URL in following format.

Example URL: /Actions/Sleep?dura

11条回答
  •  死守一世寂寞
    2020-11-22 09:53

    My answer is a slight variation for those who just want today's date in the local timezone in the YYYY-MM-DD format.

    Let me be clear:

    My Goal: get today's date in the user's timezone but formatted as ISO8601 (YYYY-MM-DD)

    Here is the code:

    new Date().toLocaleDateString("sv") // "2020-02-23" // 
    

    This works because the Sweden locale uses the ISO 8601 format.

提交回复
热议问题