반응형
자바스크립트(Javascript) 기초 #2 - 날짜 출력: Date, 숫자 앞자리를 0으로 채우기
Date 활용
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<script>
// Print date
function date() {
var date = new Date();
return (date.getFullYear() + "-" + (date.getMonth() + 1) + "-"
+ date.getDate() + " " + date.getHours() + ":"
+ date.getMinutes() + ":" + date.getSeconds());
}
</script>
</head>
<body>
<script>
document.write("<p>Date: " + date() + "</p>");
</script>
</body>
</html>
|
cs |
Date 활용, 숫자 앞자리 0으로 채우기
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
|
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<script>
// Print date
function date() {
var date = new Date();
return (date.getFullYear() + "-" + (date.getMonth() + 1) + "-"
+ date.getDate() + " " + date.getHours() + ":"
+ date.getMinutes() + ":" + date.getSeconds());
}
// 0 front of number
function leadZero(num, n) {
var leadZero = "";
num = num.toString();
if (num.length < n) { for (var i = 0; i < n - num.length; i++) leadZero += "0"; }
return leadZero + num;
}
// Print date with zero
function dateWithZero() {
var d = new Date();
return (d.getFullYear() + "-" + leadZero((d.getMonth() + 1), 2) + "-"
+ leadZero(d.getDate(), 2) + " " + leadZero(d.getHours(), 2) + ":"
+ leadZero(d.getMinutes(), 2) + ":" + leadZero(d.getSeconds(), 2));
}
</script>
</head>
<body>
<script>
document.write("<p>Date: " + date() + "</p>");
document.write("<p>Date with Zero: " + dateWithZero() + "</p>");
</script>
</body>
</html>
|
cs |
반응형
'Javascript (자바스크립트 ) > Javascript (자바스크립트) 기초' 카테고리의 다른 글
[자바스크립트 기초 #1] 성적표 만들기 (0) | 2020.11.04 |
---|