-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime.js
More file actions
99 lines (64 loc) · 2.22 KB
/
time.js
File metadata and controls
99 lines (64 loc) · 2.22 KB
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
/**
* Time function in JavaScript
*/
// let date = new Date()
// // console.log(time.getDate())
// // console.log(time.getDay())
// // console.log(time.getFullYear())
// // console.log(time.toLocaleDateString())
// // console.log(time.getUTCMonth())
// // console.log(time.toDateString())
// // Getting time and date component
// let year = date.getFullYear()
// // console.log(year)
// let month = date.getMonth()
// // console.log(++month)
// ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
// /**
// * Note : JavaScript treated January as 0
// * When ever we try to print month number kindly increment the month number by one
// * 0 - based: January is 0, December is 11
// */
// let today = date.getDate()
// // console.log(today)
// let day = date.getDay()
// // console.log(day)
// /**
// * Note: Day method also returns the number of day
// * based on Sunday is 0, and Saturday is 6
// */
// let hours = date.getHours()
// // console.log(hours)
// // Formatting date and time
// let now = new Date()
// let isoString = now.toISOString();
// console.log(isoString)
// let localeString = now.toLocaleString();
// console.log(localeString)
// let localeDateString = now.toLocaleDateString();
// console.log(localeDateString)
// let localeTimeString = now.toLocaleTimeString();
// console.log(localeTimeString)
// let timestamp = Date.now();
// console.log(timestamp)
// Get the current date and time
let now = new Date();
console.log(`Current Date and Time: ${now}`);
// Get specific components of the date and time
let year = now.getFullYear();
let month = now.getMonth() + 1; // Add 1 since months are 0-based
let date = now.getDate();
let hours = now.getHours();
let minutes = now.getMinutes();
let seconds = now.getSeconds();
console.log(`Year: ${year}`);
console.log(`Month: ${month}`);
console.log(`Date: ${date}`);
console.log(`Hours: ${hours}`);
console.log(`Minutes: ${minutes}`);
console.log(`Seconds: ${seconds}`);
// Format the date and time
let isoString = now.toISOString();
let localeString = now.toLocaleString();
console.log(`ISO String: ${isoString}`);
console.log(`Locale String: ${localeString}`);