現在の日付を表示するJavascriptプログラム

Javascript

例:現在の日付を表示する

// program to display the date
// get local machine date time
const date = new Date();

// 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: Wed Aug 26 2020
Time: 1:13:12 PM

上記の例では、 new Date() コンストラクターは、日付オブジェクトを作成するために使用されます。 次に、

1。 new Date() 現在の日付と時刻を示します。

const date = new Date();
console.log(date);
// Sun Aug 23 2020 10:46:38 GMT+0545 (+0545)

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

const n = date.toDateString();
console.log(n); // Wed Aug 26 2020

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

const time = date.toLocaleTimeString();
console.log(time); // 1:13:12 PM



Hope this helps!

Source link

コメントを残す

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