- 日付 -
Dateオブジェクト
new Data();
new Data(); で現在の日時を取得する。()に指定すると、その日時を取得する事が出来る。
new Date(2008,1,1,0,0,0); 2008年1月1日0時0分0秒 となる。
<<日付を取得>>
| new Date() | 現在の日時を表示 document.write((new Date())); |
|---|---|
| now.getFullYear() | 西暦年 |
| now.getMonth()+1 | 月を1小さい数字で取得するので、現在の月を取得するのには1をプラスする。 |
| now.getDate() | 日 |
| now.getDay() | 日曜日を0として、0から6までの数値で曜日を取得する。 |
<<時刻を取得>>
| now.getHours() | 時間 |
|---|---|
| now.getMinutes() | 分 |
| now.getSeconds() | 秒 |
| now.getTime() | 1970年1月1日0時からのミリ秒を取得する。 |
<<使用例>>
■現在の日時を new Date()で所得します。※現在の時刻を取得しないと個別に取り出せない!!
now = new Date();
■それから曜日や時間を個別に取り出す
<script type="text/javascript">
now = new Date();
FY = now.getFullYear();
M = now.getMonth()+1;
Dt = now.getDate();
Dy = now.getDay();
Hour = now.getHours();
Min = now.getMinutes();
Sec = now.getSeconds();
time = now.getTime();
document.write(FY);
document.write(M);
document.write(Dt);
document.write(Dy);
document.write(Hour);
document.write(Min);
document.write(Sec);
document.write(time);
</script>
■満期までの残り日数を表示する
<script type="text/javascript">
y=2009; m=7; d=15; //予定日を変数に格納する
today = new Date(); //現在の日時を取得
xday = new Date(y,m-1,d); //知りたい日時を取得
n = Math.floor((xday.getTime()-today.getTime())/86400000)+1;
document.write(n);
</script>
■曜日を取得する
<script type="text/javascript">
now=new Date(); //現在の日時を取得
day=now.getDay(); //曜日を取得
You=new String("日月火水木金土"); //配列Stringに文字列を収納
document.write((You.charAt(day))); //charAtで()番目の文字を取得する
</script>
■一度のに時間やその他を取り出す
h=new Date().getHours();