date-fns | How do I format to UTC

后端 未结 5 583
逝去的感伤
逝去的感伤 2020-12-17 10:42

Problem

It looks like when I use the format() function, it automatically convert the original UTC time into my timezone (UTC+8). I have been digging t

5条回答
  •  难免孤独
    2020-12-17 11:05

    I had the same problem. What I do is remove the timezone from the ISO string and then use that time with date-fns:

    let time = "2019-10-25T08:10:00Z".slice(0, -1)

    The above is a time with no time zone, and because there is no timezone date-fns assumes the local timezone, so when you do:

    format(parseISO(time), 'h:mm a')

    you get: 8:10 AM, or whatever format you prefer. You just have to be careful with the string that you are slicing. If its always the same format then it should work.

提交回复
热议问题