-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdate-time.html
124 lines (100 loc) · 3.63 KB
/
date-time.html
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja" dir="ltr" xmlns:og="http://ogp.me/ns#" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>Date Time</title>
</head>
<body>
<!-- Slideshow container -->
<h2>Date Time</h2>
<script>
// strign
let date = new Date(0)
console.log('date: ', date)
console.log('date: ', date.getHours())
console.log('date UTC: ', date.getUTCHours())
console.log('date timestamp: ', date.getTime())
console.log('date +: ', +date)
date.setFullYear(2021, 2, 20)
console.log('getFullYear: ', date.getFullYear())
// oblgatory
let oblgatory = new Date(2021, 8, 4, 1, 30, 30)
console.log('oblgatory: ', oblgatory, oblgatory.getDay())
// measuring date
function diffSubtract(date1, date2) {
return date2 - date1;
}
function diffGetTime(date1, date2) {
return date2.getTime() - date1.getTime();
}
function bench(f) {
let date1 = new Date(0);
let date2 = new Date();
let start = Date.now();
for (let i = 0; i < 100000; i++) f(date1, date2);
return Date.now() - start;
}
console.log( 'Time of diffSubtract: ' + bench(diffSubtract) + 'ms' );
console.log( 'Time of diffGetTime: ' + bench(diffGetTime) + 'ms' );
// exerices
console.log('=== tasks date time ===')
console.log('create a date: ', new Date('Feb 20, 2012, 03:12:00'))
// show a weekday
function getWeekDay(date) {
const days = ['SU', 'M0', 'TU', 'WE', 'TH', 'FR', 'SA'];
return days[date.getDay()]
}
const res = getWeekDay(new Date(2012, 0, 3))
console.log('show a weekday: ', res)
// show european day
function getEuropeanDay(date) {
let day = date.getDay();
if(day === 0) day = 7;
return day;
}
const resEuro = getEuropeanDay(new Date(2012, 0, 8))
console.log('show european weekday: ', resEuro)
// show european day
function getDateAgo(date, days) {
const dateCopy = new Date(date);
dateCopy.setDate(date.getDate() - days);
return dateCopy.getDate();
}
const resDateAgo= getDateAgo(new Date(2015, 0, 2), 1)
console.log('getDateAgo: ', resEuro)
// format relative date
function formatDate(date) {
let dayOfMonth = date.getDate();
let month = date.getMonth() + 1;
let year = date.getFullYear();
let hour = date.getHours();
let minutes = date.getMinutes();
const diffMs = new Date() - date;
const diffSec = Math.round(diffMs / 1000);
const diffMin = diffSec / 60;
const diffHour = diffMin / 60;
// foramtting
year = year.toString().slice(-2)
month = month < 10 ? `0${month}` : month;
dayOfMonth = dayOfMonth < 10 ? `0${dayOfMonth}` : dayOfMonth;
hour = hour < 10 ? `0${hour}` : hour;
minutes = minutes < 10 ? `0${minutes}` : minutes;
if(diffSec < 1) {
return 'right now';
} else if (diffMin < 1) {
return `${diffSec} sec. ago`
} else if (diffHour < 1) {
return `${diffMin} min. ago`
}
return `${dayOfMonth}.${month}.${year} ${hour}:${minutes}`
}
const resFormat= formatDate(new Date(new Date - 86400 * 1000))
console.log('formatDate: ', resFormat)
let meetUp = {
title: "Conference",
participants: [{name: "John"}, {name: "Alice"}],
age: 19
}
console.log('json stingify: ', JSON.stringify(meetUp, ['title', 'age']))
</script>
</body>
</html>