2021-05-07 02:47:48 +00:00
|
|
|
// Wow, JS, davvero?
|
|
|
|
// Davvero tutte le date.toISOString() sono considerate UTC?
|
|
|
|
// Wow.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a {@link Date} object to a timezone aware ISO String, using the user's local timezone.
|
|
|
|
*
|
|
|
|
* @param date
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
export default function convertToLocalISODate(date) {
|
|
|
|
if(date.toString() === "Invalid Date") {
|
|
|
|
throw new Error("Received an Invalid Date as parameter.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a timezone naive ISO string
|
|
|
|
const naive = date.toISOString()
|
|
|
|
|
|
|
|
// Find the local timezone
|
2021-05-11 14:37:15 +00:00
|
|
|
const tz = -new Date().getTimezoneOffset()
|
2021-05-07 02:47:48 +00:00
|
|
|
|
|
|
|
// Convert the timezone to hours
|
|
|
|
const tzHours = Math.abs(Math.floor(tz / 60)).toString().padStart(2, "0")
|
|
|
|
|
|
|
|
// Find the minutes
|
2021-05-11 14:37:15 +00:00
|
|
|
const tzMinutes = (
|
|
|
|
tz % 60
|
|
|
|
).toString().padStart(2, "0")
|
2021-05-07 02:47:48 +00:00
|
|
|
|
|
|
|
// Replace the naive part with the aware part
|
|
|
|
return naive.replace("Z", `${tz < 0 ? "-" : "+"}${tzHours}${tzMinutes}`)
|
|
|
|
}
|