Skip to content

Commit a7571a1

Browse files
committed
Add JavaScript code and add buttons to set current date and time in local time and in UTC time
1 parent 415d0d3 commit a7571a1

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

datetime-jsp/datetime.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
<input type="number" name="minute" id="minute" min="0" max="59" required><br>
1919
<label for="second">Second:</label><br>
2020
<input type="number" name="second" id="second" min="0" max="59" required><br>
21+
<button type="button" onclick="setCurrentDate()">Set Current Local Date and Time</button>
22+
<button type="button" onclick="setCurrentUtcDate()">Set Current UTC Date and Time</button>
2123
<input type="submit" value="Submit">
2224
</form>
25+
<script src="datetime.js"></script>
2326
</body>
2427

2528
</html>

datetime-jsp/datetime.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
var year = document.querySelector("#year");
2+
var month = document.querySelector("#month");
3+
var day = document.querySelector("#day");
4+
var hour = document.querySelector("#hour");
5+
var minute = document.querySelector("#minute");
6+
var second = document.querySelector("#second");
7+
function setCurrentDate() {
8+
var currentDate = new Date();
9+
var currentYear = currentDate.getFullYear();
10+
var currentMonth = currentDate.getMonth() + 1;
11+
var currentDay = currentDate.getDate();
12+
var currentHour = currentDate.getHours();
13+
var currentMinute = currentDate.getMinutes();
14+
var currentSecond = currentDate.getSeconds();
15+
year.value = currentYear;
16+
month.value = currentMonth;
17+
day.value = currentDay;
18+
hour.value = currentHour;
19+
minute.value = currentMinute;
20+
second.value = currentSecond;
21+
}
22+
function setCurrentUtcDate() {
23+
var currentDate = new Date();
24+
var currentUtcYear = currentDate.getUTCFullYear();
25+
var currentUtcMonth = currentDate.getUTCMonth() + 1;
26+
var currentUtcDay = currentDate.getUTCDate();
27+
var currentUtcHour = currentDate.getUTCHours();
28+
var currentUtcMinute = currentDate.getUTCMinutes();
29+
var currentUtcSecond = currentDate.getUTCSeconds();
30+
year.value = currentUtcYear;
31+
month.value = currentUtcMonth;
32+
day.value = currentUtcDay;
33+
hour.value = currentUtcHour;
34+
minute.value = currentUtcMinute;
35+
second.value = currentUtcSecond;
36+
}

0 commit comments

Comments
 (0)