Ćwiczenia z dat, dodanie timera do sapera i poprawienie kolizji id pól w saperze
This commit is contained in:
1 parent
cb6e9f8739
commit
9790eb5b02
2 files changed
+166
-11
No files matched your search
@@ -0,0 +1,111 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="pl">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Ćwiczenia daty</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
Otwórz konsolę
|
||||
<script>
|
||||
function format_date_to_text(date_object) {
|
||||
const t = date_object.toString().split(' ');
|
||||
let s = '';
|
||||
for (let i = 0; i < 4; i++) {
|
||||
i == 3 ? s += t[i] : s += t[i] + ' ';
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
console.log(format_date_to_text(new Date()));
|
||||
|
||||
function get_current_period(date_object) {
|
||||
return [date_object.getFullYear(), date_object.getMonth()];
|
||||
}
|
||||
|
||||
console.log(get_current_period(new Date()));
|
||||
|
||||
function is_weekend(date_object) {
|
||||
return date_object.getDay() == 0 || date_object.getDay() == 6;
|
||||
}
|
||||
|
||||
console.log(is_weekend(new Date()))
|
||||
console.log(is_weekend(new Date("2026-10-18")));
|
||||
|
||||
function create_christmas_2026() {
|
||||
return new Date("2026-12-24 15:30:00");
|
||||
}
|
||||
|
||||
console.log(create_christmas_2026().toString());
|
||||
|
||||
function get_timestamp_with_new_date(date_str) {
|
||||
const d = new Date(date_str);
|
||||
return d.getTime();
|
||||
}
|
||||
function get_timestamp_with_parse(date_str) {
|
||||
return Date.parse(date_str);
|
||||
}
|
||||
|
||||
console.log(get_timestamp_with_new_date("2026-10-07"));
|
||||
console.log(get_timestamp_with_parse("2026-10-07"));
|
||||
|
||||
function set_as_midnight(date_object) {
|
||||
date_object.setUTCHours("0");
|
||||
date_object.setUTCMinutes("0");
|
||||
date_object.setUTCSeconds("0");
|
||||
date_object.setUTCMilliseconds("0");
|
||||
}
|
||||
|
||||
let myDate = new Date("2026-10-07T14:45:30");
|
||||
set_as_midnight(myDate);
|
||||
console.log(myDate.toISOString());
|
||||
|
||||
function has_expired(exp, today) {
|
||||
const d = exp.getTime() - today.getTime();
|
||||
return d < 0;
|
||||
}
|
||||
|
||||
let exp = new Date("2026-05-01");
|
||||
let today = new Date("2026-10-07");
|
||||
console.log(has_expired(exp, today));
|
||||
|
||||
function measure_loop(a) {
|
||||
const przed = new Date().getTime();
|
||||
for (let i = 0; i < a; i++) {
|
||||
let whata;
|
||||
i % 2 == 0 ? whata = 'frika' : whata = 'afrika';
|
||||
}
|
||||
const po = new Date().getTime();
|
||||
|
||||
return "Operacja trwała: " + (po - przed) + " ms";
|
||||
}
|
||||
|
||||
console.log(measure_loop(1000000));
|
||||
|
||||
function add_days(date_object, days) {
|
||||
const ms = days * 86400000;
|
||||
|
||||
date_object.setTime(date_object.getTime() + ms);
|
||||
|
||||
return date_object;
|
||||
}
|
||||
|
||||
let d = new Date("2026-10-20");
|
||||
console.log(d.toString());
|
||||
console.log(add_days(d, 5).toString());
|
||||
|
||||
function days_until_new_year(date_object) {
|
||||
let jan = new Date(`${date_object.getFullYear() + 1}-01-01`);
|
||||
|
||||
let days = Math.ceil((jan.getTime() - date_object.getTime()) / 86400000);
|
||||
|
||||
return days;
|
||||
}
|
||||
|
||||
console.log(days_until_new_year(new Date("2026-12-26 15:00:00")));
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Reference in new issue
Block a user