Deprecated: Creation of dynamic property Timber\Site::$pingback is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Site.php on line 180

Deprecated: Creation of dynamic property Timber\User::$name is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/User.php on line 118

Deprecated: Creation of dynamic property Timber\Post::$has_disclaimer is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_edit_lock is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_edit_last is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_meta_box_present is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_send_notification is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_modify_title_and_content is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_notification_custom_heading is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_notification_custom_content is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$tipologia is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_tipologia is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$attributi is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_attributi is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$in_evidenza_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_in_evidenza_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$inviato_in_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_inviato_in_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$fonte is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_fonte is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$fonte_url is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_fonte_url is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_category is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_blz_affiliation_attributes is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_content_type is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_attributes is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_speciali is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_focuskw is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_metadesc is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_linkdex is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_content_score is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_focuskeywords is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_keywordsynonyms is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_estimated-reading-time-minutes is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_thumbnail_id is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$response_body is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$status is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_indexnow_last_ping is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_date_gmt is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$comment_status is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$ping_status is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_password is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_name is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$to_ping is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$pinged is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_modified is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_modified_gmt is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_content_filtered is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$guid is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$menu_order is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_mime_type is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$comment_count is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$filter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71
JavaScript Temporal: come cambia la gestione del tempo

JavaScript Temporal: come cambia la gestione del tempo

JavaScript Temporal è la nuova API che rivoluziona la gestione di date e orari, eliminando le limitazioni dell’oggetto Date. Offre supporto avanzato per fusi orari, precisione sub-millisecondo e gestione intuitiva delle date.

La gestione di date e orari utilizzando codice JavaScript è sempre stata una sfida per gli sviluppatori. L’oggetto Date, introdotto nel 1995, è obsoleto e limitato, con problemi ben noti come il supporto insufficiente ai fusi orari e un’API incoerente. Dopo quasi 30 anni di lavoro, passando per workaround e librerie di terze parti, il nuovo oggetto JavaScript Temporal promette finalmente di rivoluzionare il modo in cui JavaScript gestisce il tempo.

Grazie al supporto per fusi orari, calendari non gregoriani, precisione sub-millisecondo e un’API moderna, Temporal si candida a diventare lo standard per applicazioni che fanno uso intensivo di date, dagli strumenti finanziari alle piattaforme di prenotazione, fino ai sistemi di logging e sincronizzazione.

JavaScript Temporal

Quali sono i vantaggi di JavaScript Temporal

L’oggetto Date di JavaScript ha ereditato un design problematico dall’implementazione java.util.Date di Java, poi accantonata già nel 1997. Tuttavia, JavaScript è rimasto bloccato con questa soluzione imperfetta, per usare un eufemismo, per quasi tre decenni.

Tra i principali limiti dell’oggetto Date, il fatto che non supporta correttamente i fusi orari (solo UTC e ora locale), i formati di data e ora sono interpretati in modo diverso dai browser (parsing incoerente), modificare un oggetto Date può generare bug difficili da individuare, operazioni come “aggiungi un giorno” possono produrre risultati inaspettati (in particolare con l’ora legale e i calendari storici).

Per anni, gli sviluppatori hanno usato librerie come Moment.js e date-fns per aggirare i vari problemi. Ora, con Temporal, JavaScript offre finalmente una soluzione nativa, potente e affidabile.

Un approccio rivoluzionario

A differenza di Date, JavaScript Temporal è progettato per essere immutabile, preciso e privo di ambiguità. Ecco alcune delle sue caratteristiche chiave:

Supporto avanzato per i fusi orari

È possibile lavorare, da codice JavaScript, con qualsiasi fuso orario senza dover installare librerie aggiuntive:

const dateTimeInRome = Temporal.Now.plainDateTimeISO("Europe/Rome");
console.log(dateTimeInRome); // Esempio: 2025-01-22T11:47:02.555

Calendari non gregoriani

Temporal supporta calendari lunari, ebraici, islamici e molti altri, semplificando la localizzazione delle applicazioni:

const chineseNewYear = Temporal.PlainMonthDay.from({
monthCode: "M01",
day: 1,
calendar: "chinese",
});
const nextCNY = chineseNewYear.toPlainDate({ year: 2025 });
console.log(`Capodanno Cinese: ${nextCNY.toString()}`);

Precisione e affidabilità

A differenza di Date, che calcola i millisecondi trascorsi a partire da inizio 1970, Temporal supporta la precisione sub-millisecondo e gestisce correttamente i salti temporali (leap second), altrimenti conosciuti come “secondi negativi”.

Gestione intuitiva delle date

Quando si utilizza Date, calcolare la differenza tra date richiede conversioni complesse. Con Temporal, tutto è più semplice:

const start = Temporal.Now.instant();
const end = start.add({ hours: 5, minutes: 30 });
const duration = start.until(end);
console.log(`Durata: ${duration.toString()}`);

Struttura di JavaScript Temporal: un’API potente e modulare

Temporal introduce diverse classi specializzate, ognuna con un ruolo ben definito:

Tipo di operazione Classe Temporal Descrizione
Momento esatto nel tempo Temporal.Instant Equivalente al timestamp Unix.
Data e ora con fuso orario Temporal.ZonedDateTime Unione di data, ora e fuso orario.
Data e ora senza fuso orario Temporal.PlainDateTime Come un classico DateTime, ma senza ambiguità.
Solo data Temporal.PlainDate Rappresenta solo una data (senza ora).
Solo ora Temporal.PlainTime Rappresenta solo un orario (senza data).
Durata temporale Temporal.Duration Differenza tra due momenti nel tempo.

Questa modularità rende Temporal estremamente flessibile e adatto a qualsiasi scenario.

Supporto nei browser e futuro di Temporal

Temporal è attualmente in fase di implementazione sperimentale nei browser:

  • Firefox: Supporto avanzato nelle build Nightly.
  • Safari e Chrome: Implementazione in corso.
  • Polyfill: Temporal può essere provato sin da oggi servendosi di Polyfill, un “ponte” che permette agli sviluppatori di usare le nuove funzionalità oggi, senza dover aspettare il supporto completo nei browser Web (@js-temporal/polyfill).

Ti consigliamo anche

Link copiato negli appunti