Date (Objeto de JScript)

Actualización: noviembre 2007

El objeto Date de JScript puede utilizarse para representar fechas y horas arbitrarias, para obtener la fecha actual del sistema y para calcular las diferencias entre fechas. Tiene varias propiedades y métodos predefinidos. El objeto Date almacena el día de la semana, el mes, el día y el año, así como la hora en horas, minutos, segundos y milisegundos. Esta información se basa en el número de milisegundos transcurridos desde el 1 de enero de 1970, 00:00:00.000 del Horario universal coordinado (UTC), anteriormente conocido como Hora media de Greenwich. JScript puede controlar fechas que van aproximadamente desde el año 250000 a.C. al 255000 d.C., aunque únicamente se admite funcionalidad de formato para las fechas que van del año 0 d.C. al 9999 d.C.

Crear un objeto Date

Para crear un nuevo objeto Date, use el operador new. En el ejemplo siguiente se calcula el número de días transcurridos y el número de días que quedan del año en curso:

// 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));

El resultado de este programa es parecido a:

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

Vea también

Referencia

Date (Objeto)

Otros recursos

Objetos intrínsecos