I needed to display a timestamp alongside a player’s match history in this format:
2023-11-29 10:08:04
The server returned the value as 2023-11-29T10:08:04.33+08:00, so I formatted it with the following TypeScript code:
1 | const timestamp = "2023-11-29T10:08:04.33+08:00"; |
new Date(timestamp) reads the offset in the ISO timestamp, while the local getter methods format the result in the runtime’s local time zone. If the output must always use a specific time zone regardless of the user’s device, use Intl.DateTimeFormat with an explicit timeZone option or format it on the server.