Reputation: 265
I'm using date-fns ("date-fns": "^2.0.0-beta.5") and date-fnz-tz ("date-fns-tz": "^1.0.9") to format user given dates. My application has start dates and end dates. Start dates have their hours minutes and seconds set to 00.00.00. End dates are set to 23.59.59. Timezone offset is appended to the end of the date:
timezone: Asia/Hong_Kong
ex. startDate: 2020-02-10T00:00:00+08:00 endDate: 2020-02-10T23:59:59+08:00
To achieve the 00.00.00 and 23.59.59 I am using startOfDay and endOfDay respectively
The rest is handled by the format function
The issue I am having is when I attempt to format the startDate, the timezone value is incorrect, but not when I try to format the endDate, it is correct.
date = Mon Feb 10 2020 17:55:43 GMT+0800 (Hong Kong Standard Time) {}, timeZone = "Asia/Hong_Kong"
import { startOfDay } from "date-fns"
import { format } from "date-fns-tz"
function getStartDate(date: Date, timeZone: string) {
const startDate = startOfDay(new Date())
// startDate = Mon Feb 10 2020 00:00:00 GMT+0800 (Hong Kong Standard Time) {}
const formatted = format(startDate, "yyyy-MM-dd'T'HH:mm:ssXXX", { timeZone })
// formatted = "2020-02-10T00:00:00+32:00"
return formatted
}
Similar process for endOfDay
import { endOfDay } from "date-fns"
import { format } from "date-fns-tz"
function getEndDate(date: Date, timeZone: string) {
const endDate = endOfDay(new Date())
// endDate = Mon Feb 10 2020 23:59:59 GMT+0800 (Hong Kong Standard Time) {}
const formatted = format(endDate, "yyyy-MM-dd'T'HH:mm:ssXXX", { timeZone })
// formatted = "2020-02-10T23:59:59+08:00"
return formatted
}
Can anyone shed some light as to why Im getting the +32:00 on the end of my startDate?
Upvotes: 0
Views: 524
Reputation: 265
Solved by updating date-fns-tz to the latest version. At the time of writing this answer that is 1.0.10
Upvotes: 0