日付と時刻を表示するJavaScriptプログラム

Javascript

例:日付と時刻を表示する

// program to display the date and time
// get date and time
const date = new Date(2017, 2, 12, 9, 25, 30);

// get the date as a string
const n = date.toDateString();

// get the time as a string
const time = date.toLocaleTimeString();

// display date
console.log('Date: ' + n);

// display time
console.log('Time: ' + time);

出力

Date: Sun Mar 12 2017
Time: 9:25:30 AM

上記の例では、 new Date() コンストラクターは、日付オブジェクトを作成するために使用されます。 与えられた引数に従って日付と時刻を示します。

const date = new Date(2017, 2, 12, 9, 25, 30);
console.log(date); // Sun Mar 12 2017 09:25:30 GMT+0545 (+0545)

注意:の6つの数字 new Date() 年、月、日、時、分、秒をそれぞれ指定します。 また、月はから始まります 0。 したがって、1月は 0 そして12月は 11

ザ・ toDateString() メソッドは、の日付部分を返します Date オブジェクト。

ザ・ toLocaleTimeString() メソッドは、の時間部分を返します Date オブジェクト。



Hope this helps!

Source link

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です