Moment.js

Moment.js で日時データを操作する

JavaScript で日時データを操作するためのライブラリーを試用したメモ。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>HTML 5 complete</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>

<![endif]-->
<style>
article, aside, dialog, figure, footer, header,
hgroup, menu, nav, section { display: block; }
</style>
<script src="./moment.js"></script>
</head>
<body>
<dl>
<dt>今日</dt><dd><span id="today"></span></dd>
<dt>昨日</dt><dd><span id="yesterday"></span></dd>
<dt>先週の土曜日</dt><dd><span id="last_saturday"></span></dd>
<dt>先週の日曜日</dt><dd><span id="last_sunday"></span></dd>
<dt>今週の土曜日</dt><dd><span id="next_saturday"></span></dd>
<dt>今週の日曜日</dt><dd><span id="next_sunday"></span></dd>
</dl>
<script>
(function() {
moment.locale('ja')
const today = document.querySelector('span#today');
today.textContent = moment().format('YYYY-MM-DD');

const yesterday = document.querySelector('span#yesterday');
yesterday.textContent = moment().subtract(1, 'days').format('YYYY-MM-DD');

const next_saturday = document.querySelector('span#next_saturday');
next_saturday.textContent = moment().day(6).format('YYYY-MM-DD');
const next_sunday = document.querySelector('span#next_sunday');
next_sunday.textContent = moment().day(7).format('YYYY-MM-DD');

const last_saturday = document.querySelector('span#last_saturday');
last_saturday.textContent = moment().subtract(7, 'days').day(6).format('YYYY-MM-DD');
const last_sunday = document.querySelector('span#last_sunday');
last_sunday.textContent = moment().subtract(7, 'days').day(7).format('YYYY-MM-DD');
})();

</script>
</body>
</html>