---
title: Date
project: jComponent V20
version: 20.015
section: reference
language: it
license: MIT
url: https://totaljs.es/docs/it/jcomponent-v20/20-015/reference/date/
updated: 2026-11-18T13:58:19.462Z
---

# Date

> I metodi che jComponent aggiunge a Date.prototype per l'aritmetica delle date, la formattazione e la conversione in UTC.

jComponent aggiunge questi metodi a ogni data. Il formato predefinito si imposta in [DEF](../def/), il testo viene convertito in data con [parseDate()](../string/), e i nomi dei mesi e dei giorni provengono dagli array globali `MONTHS` e `DAYS`.

## API

### Date.add(value)

Type: `method`

Restituisce una nuova data spostata di una durata.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | yes |  | La durata. |

Returns: `Date` Una nuova data. La data originale non cambia.

Il valore è composto da un numero, uno spazio e un'unità. Un numero negativo sposta la data indietro, e il numero può essere un'espressione `[key]` impostata con [ENV()](../globals/). Un'unità sconosciuta restituisce una copia invariata.

| Unità | Nomi accettati |
| --- | --- |
| Secondi | `seconds`, `second`, `sec`, `ss`, `s` |
| Minuti | `minutes`, `minute`, `min`, `mm`, `m` |
| Ore | `hours`, `hour`, `hh`, `h` |
| Giorni | `days`, `day`, `dd`, `d` |
| Settimane | `weeks`, `week`, `ww`, `w` |
| Mesi | `months`, `month`, `MM`, `M` |
| Anni | `years`, `year`, `yyyy`, `yy`, `y` |

Example: Spostare una data
```js
var date = new Date(2026, 0, 15, 10, 0, 0);
console.log(date.add('1 day').format('yyyy-MM-dd')); // 2026-01-16
console.log(date.add('-1 month').format('yyyy-MM-dd')); // 2025-12-15
console.log(date.add('2 hours').format('HH:mm')); // 12:00
console.log(date.add('30 m').format('HH:mm')); // 10:30
console.log(date.format('yyyy-MM-dd HH:mm')); // 2026-01-15 10:00
```

### Date.format([format], [utc])

Type: `method`

Scrive la data come testo.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `format` | `String` | no | `DEF.dateformat` | Il layout del testo. |
| `utc` | `Boolean` | no | `false` | `true` scrive la data in UTC. |

Returns: `String` La data formattata.

Ogni token in `format` viene sostituito, quindi le altre lettere che corrispondono a un token non possono essere usate come testo letterale.

| Token | Risultato | Esempio |
| --- | --- | --- |
| `yyyy`, `YYYY` | Anno completo | `2026` |
| `yy`, `YY` | Anno a due cifre | `26` |
| `MMMM` | Nome del mese | `November` |
| `MMM` | Prime tre lettere del mese | `Nov` |
| `MM` | Mese a due cifre | `11` |
| `M` | Mese | `11` |
| `dddd`, `DDDD` | Nome del giorno | `Sunday` |
| `ddd`, `DDD` | Prime due lettere del giorno in maiuscolo | `SU` |
| `dd`, `DD` | Giorno a due cifre | `08` |
| `d`, `D` | Giorno | `8` |
| `HH`, `hh` | Ore a due cifre (da 0 a 23) | `15` |
| `H`, `h` | Ore (da 0 a 23) | `15` |
| `mm` | Minuti a due cifre | `04` |
| `m` | Minuti | `4` |
| `ss` | Secondi a due cifre | `05` |
| `s` | Secondi | `5` |
| `ww` | Settimana ISO a due cifre | `45` |
| `w` | Settimana ISO | `45` |
| `a` | `AM` o `PM` | `PM` |

Inizia il formato con `!` per scrivere le ore nel formato a 12 ore, da 1 a 12, ad esempio `!hh:mm a`. Un formato `[key]` usa il formato impostato con [ENV()](../globals/).

`format('iso')` restituisce la data ISO 8601 in UTC, come `toISOString()`, e una stringa vuota per una data non valida. Impostando `DEF.dateformatutc` su `true`, ogni chiamata usa UTC.

Example: Layout
```js
var date = new Date(2026, 10, 8, 15, 4, 5);
console.log(date.format()); // 2026-11-08
console.log(date.format('dd.MM.yyyy HH:mm')); // 08.11.2026 15:04
console.log(date.format('dddd, d MMMM yyyy')); // Sunday, 8 November 2026
console.log(date.format('MMM yy')); // Nov 26
console.log(date.format('ww')); // 45
```

Example: Output in UTC
```js
var date = new Date(Date.UTC(2026, 10, 8, 15, 4, 5));
console.log(date.format('yyyy-MM-dd HH:mm', true)); // 2026-11-08 15:04
console.log(date.format('iso')); // 2026-11-08T15:04:05.000Z
```

Example: Formato a 12 ore e formati dall'ambiente
```js
var date = new Date(2026, 10, 8, 12, 30);
console.log(date.format('!hh:mm a')); // 12:30 PM

ENV('short', 'dd.MM.');
console.log(date.format('[short]')); // 08.11.
ENV('short', 'MM/dd');
console.log(date.format('[short]')); // 11/08
```

### Date.toUTC([ticks])

Type: `method`

Sposta la data in base al fuso orario, in modo che i suoi campi locali mostrino l'ora UTC.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `ticks` | `Boolean` | no | `false` | `true` restituisce il timestamp in millisecondi al posto di un `Date`. |

Returns: `Date or Number` La data convertita, oppure il suo timestamp.

Example: Convertire in UTC
```js
var date = new Date(Date.UTC(2026, 10, 8, 15, 4));
console.log(date.toUTC().format('yyyy-MM-dd HH:mm')); // 2026-11-08 15:04
console.log(date.toUTC(true) === date.toUTC().getTime()); // true
```
