Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to format Date with specific timezone? #798

Open
kyleduo opened this issue Aug 16, 2022 · 0 comments
Open

Is there a way to format Date with specific timezone? #798

kyleduo opened this issue Aug 16, 2022 · 0 comments

Comments

@kyleduo
Copy link

kyleduo commented Aug 16, 2022

As mentioned in the doc's date formatting section, we can specific a locale to affect the style of formatting. But can we also specific a timezone used when formatting?

// Maybe feasible solution

let date = Date() // 2022-08-16 16:06:26 +0000
let formattedInCurrentTimeZone = date.toFormat("yyyy-MM-dd HH:mm:ssZZZZZ") // current time zone, CST. 2022-08-17 00:06:26+0800
let formattedInEST = date.toFormat("yyyy-MM-dd HH:mm:ssZZZZZ", timezone: .init("EST")) // 2022-08-16 12:06:26-0400

When formatting a Date, timezone is a required arguments since Date is a wrapper of timestamp which is an absolute value, however if we format the Date to a String, we need consider the timezone since a String representing a Date is a relative value. The locale and timezone could be different on a device. So we can not infer timezone from locale.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant