Javascript date format like ISO but local

前端 未结 7 1468
广开言路
广开言路 2020-12-11 00:06

how do I format a javascript date like ISO format, but in local time?

with myDate.toISOString() I am getting the time as: \"2012-09-13T19:12:23.826Z\"

相关标签:
7条回答
  • 2020-12-11 00:56

    No library required! For some Date object, e.g. t = new Date()

    • convert the local time zone offset from minutes to milliseconds

      z = t.getTimezoneOffset() * 60 * 1000

    • subtract the offset from t

      tLocal = t-z

    • create shifted Date object

      tLocal = new Date(tLocal)

    • convert to ISO format string

      iso = tLocal.toISOString()

    • drop the milliseconds and zone

      iso = iso.slice(0, 19)

    • replace the ugly 'T' with a space

      iso = iso.replace('T', ' ')

    Result is a nice ISO-ish format date-time string like "2018-08-01 22:45:50" in the local time zone.

    0 讨论(0)
提交回复
热议问题