Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Date and time#161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
otmon76 merged 1 commit intojavascript-tutorial:masterfromotmon76:1.5.11
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions1-js/05-data-types/11-date/1-new-date/solution.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
The `new Date`constructor uses the local time zone. So the only important thing to remember is that months start from zero.
Konstruktor `new Date`používá místní časové pásmo, takže jediná důležitá věc, kterou si musíme pamatovat, je, že měsíce se počítají od nuly.

So February has number 1.
Únor má tedy číslo 1.

Here's an example with numbers as date components:
Zde je příklad s čísly jako složkami data:

```js run
//new Date(year, month, date, hour, minute, second, millisecond)
//new Date(rok, měsíc, den, hodiny, minuty, sekundy, milisekundy)
let d1 = new Date(2012, 1, 20, 3, 12);
alert( d1 );
```
We could also create a date from a string, like this:
Můžeme vytvořit datum i z řetězce, např. takto:

```js run
//new Date(datastring)
//new Date(řetězecSDatem)
let d2 = new Date("2012-02-20T03:12");
alert( d2 );
```
6 changes: 3 additions & 3 deletions1-js/05-data-types/11-date/1-new-date/task.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,8 +2,8 @@ importance: 5

---

#Create a date
#Vytvoření data

Create a `Date`object for the date: Feb 20,2012, 3:12am. The time zone is local.
Vytvořte objekt `Date`pro datum: 20. února2012, 3.12 hodin. Časové pásmo je místní.

Show it using `alert`.
Zobrazte jej pomocí `alert`.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
functiongetWeekDay(date) {
letdays = ['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'];
functionvraťDenVTýdnu(datum) {
letdny = ['NE', 'PO', 'ÚT', 'ST', 'ČT', '', 'SO'];

returndays[date.getDay()];
returndny[datum.getDay()];
}
30 changes: 15 additions & 15 deletions1-js/05-data-types/11-date/2-get-week-day/_js.view/test.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
describe("getWeekDay", function() {
it("3 January 2014 -friday", function() {
assert.equal(getWeekDay(new Date(2014, 0, 3)), 'FR');
describe("vraťDenVTýdnu", function() {
it("3. leden 2014 -pátek", function() {
assert.equal(vraťDenVTýdnu(new Date(2014, 0, 3)), '');
});

it("4 January 2014 -saturday", function() {
assert.equal(getWeekDay(new Date(2014, 0, 4)), 'SA');
it("4. leden 2014 -sobota", function() {
assert.equal(vraťDenVTýdnu(new Date(2014, 0, 4)), 'SO');
});

it("5 January 2014 -sunday", function() {
assert.equal(getWeekDay(new Date(2014, 0, 5)), 'SU');
it("5. leden 2014 -neděle", function() {
assert.equal(vraťDenVTýdnu(new Date(2014, 0, 5)), 'NE');
});

it("6 January 2014 -monday", function() {
assert.equal(getWeekDay(new Date(2014, 0, 6)), 'MO');
it("6. leden 2014 -pondělí", function() {
assert.equal(vraťDenVTýdnu(new Date(2014, 0, 6)), 'PO');
});

it("7 January 2014 -tuesday", function() {
assert.equal(getWeekDay(new Date(2014, 0, 7)), 'TU');
it("7. leden 2014 -úterý", function() {
assert.equal(vraťDenVTýdnu(new Date(2014, 0, 7)), 'ÚT');
});

it("8 January 2014 -wednesday", function() {
assert.equal(getWeekDay(new Date(2014, 0, 8)), 'WE');
it("8. leden 2014 -středa", function() {
assert.equal(vraťDenVTýdnu(new Date(2014, 0, 8)), 'ST');
});

it("9 January 2014 -thursday", function() {
assert.equal(getWeekDay(new Date(2014, 0, 9)), 'TH');
it("9. leden 2014 -čtvrtek", function() {
assert.equal(vraťDenVTýdnu(new Date(2014, 0, 9)), 'ČT');
});
});
14 changes: 7 additions & 7 deletions1-js/05-data-types/11-date/2-get-week-day/solution.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
The method `date.getDay()`returns the number of the weekday, starting from sunday.
Metoda `datum.getDay()`vrací číslo dne v týdnu počínajíc nedělí.

Let's make an array of weekdays, so that we can get the proper day name by its number:
Vytvoříme pole dnů v týdnu, abychom mohli získat název příslušného dne podle jeho čísla:

```js run demo
functiongetWeekDay(date) {
letdays = ['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'];
functionvraťDenVTýdnu(datum) {
letdny = ['NE', 'PO', 'ÚT', 'ST', 'ČT', '', 'SO'];

returndays[date.getDay()];
returndny[datum.getDay()];
}

letdate = new Date(2014, 0, 3); // 3 Jan 2014
alert(getWeekDay(date) ); //FR
letdatum = new Date(2014, 0, 3); // 3. leden 2014
alert(vraťDenVTýdnu(datum) ); //
```
10 changes: 5 additions & 5 deletions1-js/05-data-types/11-date/2-get-week-day/task.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,13 +2,13 @@ importance: 5

---

#Show a weekday
#Zobrazení dne v týdnu

Write a function `getWeekDay(date)` to show the weekday in short format: 'MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU'.
Napište funkci `vraťDenVTýdnu(datum)`, která zobrazí den v týdnu ve zkráceném tvaru: „PO“, „ÚT“, „ST“, „ČT“, „PÁ“, „SO“, „NE“.

For instance:
Příklad:

```js no-beautify
letdate = new Date(2012, 0, 3); // 3 Jan 2012
alert(getWeekDay(date) );//should output "TU"
letdatum = new Date(2012, 0, 3); // 3. leden 2012
alert(vraťDenVTýdnu(datum) ); //měla by vypsat „ÚT“
```
10 changes: 5 additions & 5 deletions1-js/05-data-types/11-date/3-weekday/_js.view/solution.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
functiongetLocalDay(date) {
functionvraťMístníDenVTýdnu(datum) {

letday =date.getDay();
letden =datum.getDay();

if (day == 0) { //weekday 0 (sunday) is 7 in european
day = 7;
if (den == 0) { //0. den v týdnu (neděle) je v Evropě 7.
den = 7;
}

returnday;
returnden;
}
30 changes: 15 additions & 15 deletions1-js/05-data-types/11-date/3-weekday/_js.view/test.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
describe("getLocalDay returns the \"european\"weekday", function() {
it("3 January 2014 -friday", function() {
assert.equal(getLocalDay(new Date(2014, 0, 3)), 5);
describe("vraťMístníDenVTýdnu vrací \"evropský\"den v týdnu", function() {
it("3. leden 2014 -pátek", function() {
assert.equal(vraťMístníDenVTýdnu(new Date(2014, 0, 3)), 5);
});

it("4 January 2014 -saturday", function() {
assert.equal(getLocalDay(new Date(2014, 0, 4)), 6);
it("4. leden 2014 -sobota", function() {
assert.equal(vraťMístníDenVTýdnu(new Date(2014, 0, 4)), 6);
});

it("5 January 2014 -sunday", function() {
assert.equal(getLocalDay(new Date(2014, 0, 5)), 7);
it("5. leden 2014 -neděle", function() {
assert.equal(vraťMístníDenVTýdnu(new Date(2014, 0, 5)), 7);
});

it("6 January 2014 -monday", function() {
assert.equal(getLocalDay(new Date(2014, 0, 6)), 1);
it("6. leden 2014 -pondělí", function() {
assert.equal(vraťMístníDenVTýdnu(new Date(2014, 0, 6)), 1);
});

it("7 January 2014 -tuesday", function() {
assert.equal(getLocalDay(new Date(2014, 0, 7)), 2);
it("7. leden 2014 -úterý", function() {
assert.equal(vraťMístníDenVTýdnu(new Date(2014, 0, 7)), 2);
});

it("8 January 2014 -wednesday", function() {
assert.equal(getLocalDay(new Date(2014, 0, 8)), 3);
it("8. leden 2014 -středa", function() {
assert.equal(vraťMístníDenVTýdnu(new Date(2014, 0, 8)), 3);
});

it("9 January 2014 -thursday", function() {
assert.equal(getLocalDay(new Date(2014, 0, 9)), 4);
it("9. leden 2014 -čtvrtek", function() {
assert.equal(vraťMístníDenVTýdnu(new Date(2014, 0, 9)), 4);
});
});
8 changes: 4 additions & 4 deletions1-js/05-data-types/11-date/3-weekday/task.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,11 +2,11 @@ importance: 5

---

#European weekday
#Evropský den v týdnu

European countries have days of week starting with Monday (number 1),then Tuesday (number 2)and till Sunday (number 7).Write a function `getLocalDay(date)` that returns the "European" day of week for `date`.
V evropských zemích začíná týden pondělím (číslo 1),pak je úterý (číslo 2)a tak dále, až nakonec je neděle (číslo 7).Napište funkci `vraťMístníDenVTýdnu(datum)`, která vrátí „evropský“ den v týdnu pro `datum`.

```js no-beautify
letdate = new Date(2012, 0, 3); // 3 Jan 2012
alert(getLocalDay(date) );//tuesday, should show 2
letdatum = new Date(2012, 0, 3);// 3. leden 2012
alert(vraťMístníDenVTýdnu(datum) ); //úterý, měla by zobrazit 2
```
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
functiongetDateAgo(date, days) {
letdateCopy = new Date(date);
functionvraťDenPřed(datum, dny) {
letkopieData = new Date(datum);

dateCopy.setDate(date.getDate() -days);
returndateCopy.getDate();
kopieData.setDate(datum.getDate() -dny);
returnkopieData.getDate();
}
28 changes: 14 additions & 14 deletions1-js/05-data-types/11-date/4-get-date-ago/_js.view/test.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
describe("getDateAgo", function() {
describe("vraťDenPřed", function() {

it("1day before 02.01.2015 ->day 1", function() {
assert.equal(getDateAgo(new Date(2015, 0, 2), 1), 1);
it("1den před 02.01.2015 ->den 1", function() {
assert.equal(vraťDenPřed(new Date(2015, 0, 2), 1), 1);
});


it("2days before 02.01.2015 ->day 31", function() {
assert.equal(getDateAgo(new Date(2015, 0, 2), 2), 31);
it("2dny před 02.01.2015 ->den 31", function() {
assert.equal(vraťDenPřed(new Date(2015, 0, 2), 2), 31);
});

it("100days before 02.01.2015 ->day 24", function() {
assert.equal(getDateAgo(new Date(2015, 0, 2), 100), 24);
it("100dní před 02.01.2015 ->den 24", function() {
assert.equal(vraťDenPřed(new Date(2015, 0, 2), 100), 24);
});

it("365days before 02.01.2015 ->day 2", function() {
assert.equal(getDateAgo(new Date(2015, 0, 2), 365), 2);
it("365dní před 02.01.2015 ->den 2", function() {
assert.equal(vraťDenPřed(new Date(2015, 0, 2), 365), 2);
});

it("does not modify the given date", function() {
letdate = new Date(2015, 0, 2);
letdateCopy = new Date(date);
getDateAgo(dateCopy, 100);
assert.equal(date.getTime(),dateCopy.getTime());
it("nemění zadané datum", function() {
letdatum = new Date(2015, 0, 2);
letkopieData = new Date(datum);
vraťDenPřed(kopieData, 100);
assert.equal(datum.getTime(),kopieData.getTime());
});

});
28 changes: 14 additions & 14 deletions1-js/05-data-types/11-date/4-get-date-ago/solution.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
The idea is simple: to substract given number of days from `date`:
Myšlenka je jednoduchá: odečíst zadaný počet dní od `datum`:

```js
functiongetDateAgo(date, days) {
date.setDate(date.getDate() -days);
returndate.getDate();
functionvraťDenPřed(datum, dny) {
datum.setDate(datum.getDate() -dny);
returndatum.getDate();
}
```

...But the function should not change `date`.That's an important thing, because the outer code which gives us the date does not expect it to change.
...Funkce by však neměla měnit `datum`.To je důležité, jelikož vnější kód, který nám datum předává, neočekává, že bude změněno.

To implement it let's clone the date, like this:
Abychom to implementovali, naklonujeme datum, např. takto:

```js run demo
functiongetDateAgo(date, days) {
letdateCopy = new Date(date);
functionvraťDenPřed(datum, dny) {
letkopieData = new Date(datum);

dateCopy.setDate(date.getDate() -days);
returndateCopy.getDate();
kopieData.setDate(datum.getDate() -dny);
returnkopieData.getDate();
}

letdate = new Date(2015, 0, 2);
letdatum = new Date(2015, 0, 2);

alert(getDateAgo(date, 1) ); // 1, (1 Jan 2015)
alert(getDateAgo(date, 2) ); // 31, (31 Dec 2014)
alert(getDateAgo(date, 365) ); // 2, (2 Jan 2014)
alert(vraťDenPřed(datum, 1) ); // 1, (1. leden 2015)
alert(vraťDenPřed(datum, 2) ); // 31, (31. prosinec 2014)
alert(vraťDenPřed(datum, 365) ); // 2, (2. leden 2014)
```
18 changes: 9 additions & 9 deletions1-js/05-data-types/11-date/4-get-date-ago/task.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,20 +2,20 @@ importance: 4

---

#Which day of month was many days ago?
#Který den v měsíci byl před mnoha dny?

Create a function `getDateAgo(date, days)` to return the day of month `days` ago from the `date`.
Vytvořte funkci `vraťDenPřed(datum, dny)`, která vrátí den v měsíci, který byl `dny` dnů přede dnem `datum`.

For instance, if today is 20th, then `getDateAgo(new Date(), 1)`should be 19th and `getDateAgo(new Date(), 2)`should be 18th.
Například jestliže dnes je 20., pak `vraťDenPřed(new Date(), 1)`by měla vrátit 19 a `vraťDenPřed(new Date(), 2)`by měla vrátit 18.

Should work reliably for `days=365`or more:
Měla by spolehlivě fungovat i pro `dny=365`nebo více:

```js
letdate = new Date(2015, 0, 2);
letdatum = new Date(2015, 0, 2);

alert(getDateAgo(date, 1) ); // 1, (1 Jan 2015)
alert(getDateAgo(date, 2) ); // 31, (31 Dec 2014)
alert(getDateAgo(date, 365) ); // 2, (2 Jan 2014)
alert(vraťDenPřed(datum, 1) ); // 1, (1. leden 2015)
alert(vraťDenPřed(datum, 2) ); // 31, (31. prosinec 2014)
alert(vraťDenPřed(datum, 365) ); // 2, (2. leden 2014)
```

P.S.The function should not modify the given `date`.
P.S.Funkce by neměla modifikovat zadané `datum`.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
functiongetLastDayOfMonth(year, month) {
letdate = new Date(year, month + 1, 0);
returndate.getDate();
functionvraťPosledníDenVMěsíci(rok, měsíc) {
letdatum = new Date(rok, měsíc + 1, 0);
returndatum.getDate();
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
describe("getLastDayOfMonth", function() {
it("last day of 01.01.2012 - 31", function() {
assert.equal(getLastDayOfMonth(2012, 0), 31);
describe("vraťPosledníDenVMěsíci", function() {
it("poslední den 01.01.2012 - 31", function() {
assert.equal(vraťPosledníDenVMěsíci(2012, 0), 31);
});

it("last day of01.02.2012 - 29 (leap year)", function() {
assert.equal(getLastDayOfMonth(2012, 1), 29);
it("poslední den01.02.2012 - 29 (přestupný rok)", function() {
assert.equal(vraťPosledníDenVMěsíci(2012, 1), 29);
});

it("last day of 01.02.2013 - 28", function() {
assert.equal(getLastDayOfMonth(2013, 1), 28);
it("poslední den 01.02.2013 - 28", function() {
assert.equal(vraťPosledníDenVMěsíci(2013, 1), 28);
});
});
16 changes: 8 additions & 8 deletions1-js/05-data-types/11-date/5-last-day-of-month/solution.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
Let's create a date using the next month, but pass zero as the day:
Vytvořme datum z následujícího měsíce, ale jako den předáme nulu:
```js run demo
functiongetLastDayOfMonth(year, month) {
letdate = new Date(year, month + 1, 0);
returndate.getDate();
functionvraťPosledníDenVMěsíci(rok, měsíc) {
letdatum = new Date(rok, měsíc + 1, 0);
returndatum.getDate();
}

alert(getLastDayOfMonth(2012, 0) ); // 31
alert(getLastDayOfMonth(2012, 1) ); // 29
alert(getLastDayOfMonth(2013, 1) ); // 28
alert(vraťPosledníDenVMěsíci(2012, 0) ); // 31
alert(vraťPosledníDenVMěsíci(2012, 1) ); // 29
alert(vraťPosledníDenVMěsíci(2013, 1) ); // 28
```

Normally, dates start from 1,but technically we can pass any number, the date will autoadjust itself. So when we pass0,then it means "one day before 1st day of the month", in other words: "the last day of the previous month".
Normálně dny začínají od 1,ale technicky můžeme předat jakékoli číslo a datum se samo přizpůsobí. Když tedy předáme0,bude to znamenat „jeden den před 1. dnem v měsíci“, jinými slovy: „poslední den předcházejícího měsíce“.
Loading

[8]ページ先頭

©2009-2025 Movatter.jp