Share via


Oggetto Date di JScript

L'oggetto Date di JScript consente di rappresentare date e orari arbitrari, di ottenere la data di sistema corrente e di calcolare le differenze tra date. Dispone di numerose proprietà e metodi predefiniti. L'oggetto Date memorizza un giorno della settimana, quindi il giorno del mese, il mese e l'anno, infine l'orario espresso in ore, minuti e secondi. Queste informazioni sono basate sul numero di millisecondi trascorsi dalle 00.00.00.000 UTC del 1 gennaio 1970. Prima di UTC (Universal Coordinated Time, tempo universale coordinato) veniva utilizzato l'acronimo GMT (Greenwich Mean Time), che rappresenta l'ora media di Greenwich. JScript può gestire date comprese in un intervallo approssimativo dal 250.000 a.C. al 255.000 d.C., nonostante alcune funzionalità di formattazione siano supportate solo nell'intervallo di date compreso tra l'anno 0 e il 9999 d.C.

Creazione di un oggetto Date

Per creare un nuovo oggetto Date, utilizzare l'operatore new. Nell'esempio che segue viene calcolato il numero di giorni trascorsi nell'anno corrente e il numero di giorni rimanenti.

// Get the current date and read the year.
var today : Date = new Date();
// The getYear method should not be used. Always use getFullYear.
var thisYear : int = today.getFullYear();

// Create two new dates, one for January first of the current year,
// and one for January first of next year. The months are numbered
// starting with zero.
var firstOfYear : Date = new Date(thisYear,0,1);
var firstOfNextYear : Date = new Date(thisYear+1,0,1);

// Calculate the time difference (in milliseconds) and 
// convert the differnce to days.
const millisecondsToDays = 1/(1000*60*60*24);
var daysPast : double = (today - firstOfYear)*millisecondsToDays;
var daysToGo : double = (firstOfNextYear - today)*millisecondsToDays;

// Display the information.
print("Today is: "+today+".");
print("Days since first of the year: "+Math.floor(daysPast));
print("Days until the end of the year: "+Math.ceil(daysToGo));

L'output del programma è analogo al seguente:

Today is: Sun Apr 1 09:00:00 PDT 2001.
Days since first of the year: 90
Days until the end of the year: 275

Vedere anche

Riferimenti

Oggetto Date

Altre risorse

Oggetti intrinseci