Skip to content

Commit 9d1898f

Browse files
authored
Dateクラスの演習を書き換えました (#755)
1 parent d768667 commit 9d1898f

File tree

4 files changed

+33
-61
lines changed

4 files changed

+33
-61
lines changed

docs/2-browser-apps/03-class/_samples/count-down/script.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

docs/2-browser-apps/03-class/_samples/count-down/index.html renamed to docs/2-browser-apps/03-class/_samples/current-time/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
<html lang="ja">
33
<head>
44
<meta charset="utf-8" />
5-
<title>カウントダウン</title>
5+
<title>現在時刻</title>
66
</head>
77
<body>
8-
<div id="countdown-box"></div>
8+
<div id="current-time"></div>
99
<script src="./script.js"></script>
1010
</body>
1111
</html>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const currentTime = document.getElementById("current-time");
2+
3+
function getCurrentTime() {
4+
const now = new Date();
5+
const currentYear = now.getFullYear();
6+
const currentMonth = now.getMonth() + 1;
7+
const currentDate = now.getDate();
8+
const currentHour = now.getHours();
9+
const currentMinute = now.getMinutes();
10+
const currentSecond = now.getSeconds();
11+
12+
return `今は${currentYear}${currentMonth}${currentDate}${currentHour}${currentMinute}${currentSecond}秒です。`;
13+
}
14+
15+
currentTime.textContent = getCurrentTime();

docs/2-browser-apps/03-class/index.mdx

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ tanaka.introduceSelf(); // 私の名前は田中です。18歳です。ドイツ
298298

299299
`Student` クラスを継承して `SeniorStudent` クラスを作ってみましょう。`SeniorStudent` クラスのインスタンスは `researchQuestion` プロパティを持ち、`introduceSelf` メソッドを実行すると自分の名前を出力した後に自分の研究内容を紹介するようにしてみましょう。
300300

301-
<Answer title="学生のClassの定義">
301+
<Answer title="高学年の学生の自己紹介">
302302

303303
```javascript
304304
class Student {
@@ -393,50 +393,30 @@ document.write(false.toString()); // false
393393

394394
### `Date` クラス
395395

396-
ドキュメントを読み、[`Date` クラス](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Date)を使って、その日の残り時間を表示してみましょう。
397-
398-
例:
399-
400-
21時25分40秒に実行されたとき、
401-
402-
```
403-
今日は残り2時間34分20秒です。
404-
```
405-
406-
と表示する
396+
[`Date` クラス](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Date)を使って、現在時刻を表示してみましょう。`Date` クラスのドキュメントを読んで、現在時刻を表示するのに必要なメソッドを探してみましょう。
407397

408398
:::tip
409399
`Date` クラスには、時間、分、秒などを取得するためのメソッドが定義されています。
410400
:::
411401

412-
<Answer title="その日の残り時間">
413-
414-
`Date#getTime` メソッドを使います。
402+
<Answer title="現在時刻の表示">
415403

416-
```js
417-
const countdownBox = document.getElementById("countdown-box");
404+
```javascript
405+
const currentTime = document.getElementById("current-time");
418406

419-
function getRemainingTime() {
407+
function getCurrentTime() {
420408
const now = new Date();
421-
const endOfDay = new Date(
422-
now.getFullYear(),
423-
now.getMonth(),
424-
now.getDate(),
425-
23,
426-
59,
427-
59,
428-
999,
429-
);
430-
const remainingTime = endOfDay.getTime() - now.getTime();
431-
432-
const hours = Math.floor(remainingTime / (1000 * 60 * 60));
433-
const minutes = Math.floor(remainingTime / (1000 * 60)) % 60;
434-
const seconds = Math.floor(remainingTime / 1000) % 60;
435-
436-
return `今日の残り時間: ${hours}時間 ${minutes}${seconds}秒です。`;
409+
const currentYear = now.getFullYear();
410+
const currentMonth = now.getMonth() + 1;
411+
const currentDate = now.getDate();
412+
const currentHour = now.getHours();
413+
const currentMinute = now.getMinutes();
414+
const currentSecond = now.getSeconds();
415+
416+
return `今は${currentYear}${currentMonth}${currentDate}${currentHour}${currentMinute}${currentSecond}秒です。`;
437417
}
438418

439-
countdownBox.textContent = getRemainingTime();
419+
currentTime.textContent = getCurrentTime();
440420
```
441421

442422
<ViewSource url={import.meta.url} path="_samples/count-down" />
@@ -451,7 +431,7 @@ countdownBox.textContent = getRemainingTime();
451431

452432
<Answer title="図形クラス">
453433

454-
```js
434+
```javascript
455435
class Shape {
456436
color;
457437
constructor(color) {

0 commit comments

Comments
 (0)