◐ Shell
reader mode source ↗
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
8 changes: 4 additions & 4 deletions 1-js/05-data-types/11-date/1-new-date/solution.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
The `new Date` constructor uses the local time zone. So the only important thing to remember is that months start from zero.

So February has number 1.

Here's an example with numbers as date components:

```js run
//new Date(year, month, date, hour, minute, second, millisecond)
let d1 = new Date(2012, 1, 20, 3, 12);
alert( d1 );
```
We could also create a date from a string, like this:

```js run
//new Date(datastring)
Expand Down
6 changes: 3 additions & 3 deletions 1-js/05-data-types/11-date/1-new-date/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ importance: 5

---

# Create a date

Create a `Date` object for the date: Feb 20, 2012, 3:12am. The time zone is local.

Show it using `alert`.
10 changes: 5 additions & 5 deletions 1-js/05-data-types/11-date/2-get-week-day/solution.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
The method `date.getDay()` returns the number of the weekday, starting from sunday.

Let's make an array of weekdays, so that we can get the proper day name by its number:

```js run demo
function getWeekDay(date) {
let days = ['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'];

return days[date.getDay()];
}

let date = new Date(2014, 0, 3); // 3 Jan 2014
alert( getWeekDay(date) ); // FR
```
8 changes: 4 additions & 4 deletions 1-js/05-data-types/11-date/3-weekday/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ importance: 5

---

# European weekday

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`.

```js no-beautify
let date = new Date(2012, 0, 3); // 3 Jan 2012
alert( getLocalDay(date) ); // tuesday, should show 2
```
12 changes: 6 additions & 6 deletions 1-js/05-data-types/11-date/4-get-date-ago/solution.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The idea is simple: to substract given number of days from `date`:

```js
function getDateAgo(date, days) {
Expand All @@ -7,9 +7,9 @@ function getDateAgo(date, days) {
}
```

...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.

To implement it let's clone the date, like this:

```js run demo
function getDateAgo(date, days) {
Expand All @@ -21,7 +21,7 @@ function getDateAgo(date, days) {

let date = 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)
```
4 changes: 2 additions & 2 deletions 1-js/05-data-types/11-date/5-last-day-of-month/solution.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Let's create a date using the next month, but pass zero as the day:
```js run demo
function getLastDayOfMonth(year, month) {
let date = new Date(year, month + 1, 0);
Expand All @@ -10,4 +10,4 @@ alert( getLastDayOfMonth(2012, 1) ); // 29
alert( getLastDayOfMonth(2013, 1) ); // 28
```

Normally, dates start from 1, but technically we can pass any number, the date will autoadjust itself. So when we pass 0, then it means "one day before 1st day of the month", in other words: "the last day of the previous month".
12 changes: 6 additions & 6 deletions 1-js/05-data-types/11-date/5-last-day-of-month/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ importance: 5

---

# Last day of month?

Write a function `getLastDayOfMonth(year, month)` that returns the last day of month. Sometimes it is 30th, 31st or even 28/29th for Feb.

Parameters:

- `year` -- four-digits year, for instance 2012.
- `month` -- month, from 0 to 11.

For instance, `getLastDayOfMonth(2012, 1) = 29` (leap year, Feb).
8 changes: 4 additions & 4 deletions 1-js/05-data-types/11-date/6-get-seconds-today/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ importance: 5

---

# How many seconds have passed today?

Write a function `getSecondsToday()` that returns the number of seconds from the beginning of today.

For instance, if now were `10:00 am`, and there was no daylight savings shift, then:

```js
getSecondsToday() == 36000 // (3600 * 10)
```

The function should work in any day. That is, it should not have a hard-coded value of "today".
12 changes: 6 additions & 6 deletions 1-js/05-data-types/11-date/7-get-seconds-to-tomorrow/solution.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
To get the number of milliseconds till tomorrow, we can from "tomorrow 00:00:00" substract the current date.

First, we generate that "tomorrow", and then do it:

```js run
function getSecondsToTomorrow() {
let now = new Date();

// tomorrow date
let tomorrow = new Date(now.getFullYear(), now.getMonth(), *!*now.getDate()+1*/!*);

let diff = tomorrow - now; // difference in ms
return Math.round(diff / 1000); // convert to seconds
}
```

Expand All @@ -29,4 +29,4 @@ function getSecondsToTomorrow() {
}
```

Please note that many countries have Daylight Savings Time (DST), so there may be days with 23 or 25 hours. We may want to treat such days separately.
8 changes: 4 additions & 4 deletions 1-js/05-data-types/11-date/7-get-seconds-to-tomorrow/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ importance: 5

---

# How many seconds till tomorrow?

Create a function `getSecondsToTomorrow()` that returns the number of seconds till tomorrow.

For instance, if now is `23:00`, then:

```js
getSecondsToTomorrow() == 3600
```

P.S. The function should work at any day, the "today" is not hardcoded.
42 changes: 21 additions & 21 deletions 1-js/05-data-types/11-date/8-format-date-relative/solution.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
To get the time from `date` till now -- let's substract the dates.

```js run demo
function formatDate(date) {
let diff = new Date() - date; // the difference in milliseconds

if (diff < 1000) { // less than 1 second
return 'right now';
}

let sec = Math.floor(diff / 1000); // convert diff to seconds

if (sec < 60) {
return sec + ' sec. ago';
}

let min = Math.floor(diff / 60000); // convert diff to minutes
if (min < 60) {
return min + ' min. ago';
}

// format the date
// add leading zeroes to single-digit day/month/hours/minutes
let d = date;
d = [
'0' + d.getDate(),
'0' + (d.getMonth() + 1),
'' + d.getFullYear(),
'0' + d.getHours(),
'0' + d.getMinutes()
].map(component => component.slice(-2)); // take last 2 digits of every component

// join the components into date
return d.slice(0, 3).join('.') + ' ' + d.slice(3).join(':');
}

alert( formatDate(new Date(new Date - 1)) ); // "right now"

alert( formatDate(new Date(new Date - 30 * 1000)) ); // "30 sec. ago"

alert( formatDate(new Date(new Date - 5 * 60 * 1000)) ); // "5 min. ago"

// yesterday's date like 31.12.2016 20:00
alert( formatDate(new Date(new Date - 86400 * 1000)) );
```

Alternative solution:

```js run
function formatDate(date) {
Expand All @@ -58,19 +58,19 @@ function formatDate(date) {
let diffMin = diffSec / 60;
let diffHour = diffMin / 60;

// formatting
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`
} else {
return `${dayOfMonth}.${month}.${year} ${hour}:${minutes}`
}
Expand Down
Loading
Toggle all file notes Toggle all file annotations