LoginSignup
4
4

More than 5 years have passed since last update.

Dateオブジェクトの勉強

Posted at

昨日のJavaScriptで作る「あなたの誕生日診断」 (全5回)を復習。

birthdayを表すデータオブジェクト?

var birthday = document.getElementById('birthday').value;
var d1 = new Date(birthday);

2行目のnew Date()の中に、なぜbirthdayと入るのかがわからない。これはnameのbirthday、idのbirthday、変数のbirthdayのどれにあたるのか。引き続き調べてみるとnew Date();は引数を指定しないと現在の日付でDateオブジェクトを作るらしい。引数は入門動画でも何度か出てきたがわかっていない。MDNで調べてもピンとこなかったので入門動画の#12 関数を使ってみようを再度確認。

function hello(name) {
console.log("hello " + name);
}
hello("Tom");
hello("Ken");

こちらから与えたい値があるときは引数を利用するということだろうか。ということはnew Date(birthday);の引数はid=birthdayの中に入力された値ということかもしれない。ユーザーが入力した値(どんな値かはわからない)でDateオブジェクトを作りたいときはnew Date(ここに入力欄のid)と書けばいいのだろうか?そして、それを変数に代入する。

モヤモヤしていたものが少し晴れた気がした。が、合っているかはわからない。引き続き勉強しよう。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4