次の方法で共有


JScript の Date オブジェクト

JScript の Date オブジェクトは、任意の日付や時間を表したり、現在のシステムの日付を取得したり、日付の差を計算したりするときに使用できます。 このオブジェクトには、定義済みのプロパティとメソッドがあります。 Date オブジェクトは、曜日、年、月、日、時、分、秒、およびミリ秒を格納します。 この情報は、世界協定時刻 (UTC) を基準にしており、1970 年 1 月 1 日 00:00:00.000 から経過したミリ秒の数値を基に算出されます。世界協定時刻は、これまでグリニッジ標準時と呼ばれていました。 JScript では、紀元前 250,000 年から西暦 255,000 年までの日付を処理できます。 ただし、一部の書式機能は、西暦 0 ~ 9999 年の範囲の日付でしかサポート されていません。

Date オブジェクトの作成

新しい Date オブジェクトを作成するには、new 演算子を使用します。 現在の年の経過日数、および残りの日数を計算する例を次に示します。

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

このプログラムの出力は次のようになります。

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

参照

参照

Date オブジェクト

その他の技術情報

組み込みオブジェクト