mirror of
https://github.com/pds-nest/nest.git
synced 2024-11-26 23:14:19 +00:00
32 lines
No EOL
912 B
JavaScript
32 lines
No EOL
912 B
JavaScript
// 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("Data non valida ricevuta come parametro.")
|
|
}
|
|
|
|
// Create a timezone naive ISO string
|
|
const naive = date.toISOString()
|
|
|
|
// Find the local timezone
|
|
const tz = -new Date().getTimezoneOffset()
|
|
|
|
// Convert the timezone to hours
|
|
const tzHours = Math.abs(Math.floor(tz / 60)).toString().padStart(2, "0")
|
|
|
|
// Find the minutes
|
|
const tzMinutes = (
|
|
tz % 60
|
|
).toString().padStart(2, "0")
|
|
|
|
// Replace the naive part with the aware part
|
|
return naive.replace("Z", `${tz < 0 ? "-" : "+"}${tzHours}${tzMinutes}`)
|
|
} |