Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date and time #196

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
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.
Hàm tạo `new Date` sử dụng múi giờ địa phương. Vì vậy, điều quan trọng duy nhất cần nhớ là tháng bắt đầu từ con số không.

So February has number 1.
Vậy tháng 2 có số 1.

Here's an example with numbers as date components:
Đây là một ví dụ với các số là thành phần ngày:

```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:
Chúng ta cũng có thể tạo một ngày từ một chuỗi, như thế này:

```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
# Tạo một ngày

Create a `Date` object for the date: Feb 20, 2012, 3:12am. The time zone is local.
Tạo đối tượng `Date` cho ngày: 20 tháng 2 năm 2012, 3:12 sáng. Múi giờ là địa phương.

Show it using `alert`.
Hiển thị nó bằng cách sử dụng `alert`.
6 changes: 3 additions & 3 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,6 +1,6 @@
The method `date.getDay()` returns the number of the weekday, starting from sunday.
Phương thức `date.getDay()` trả về số ngày trong tuần, bắt đầu từ chủ nhật.

Let's make an array of weekdays, so that we can get the proper day name by its number:
Hãy tạo một array các ngày trong tuần, để chúng ta có thể lấy tên ngày thích hợp theo số của nó:

```js run demo
function getWeekDay(date) {
Expand All @@ -9,6 +9,6 @@ function getWeekDay(date) {
return days[date.getDay()];
}

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

---

# Show a weekday
# Hiển thị một ngày trong tuần

Write a function `getWeekDay(date)` to show the weekday in short format: 'MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU'.
Viết hàm `getWeekDay(date)` để hiển thị ngày trong tuần ở định dạng ngắn: 'MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU'.

For instance:
Ví dụ:

```js no-beautify
let date = new Date(2012, 0, 3); // 3 Jan 2012
alert( getWeekDay(date) ); // should output "TU"
let date = new Date(2012, 0, 3); // 3 Thg 1 2012
alert( getWeekDay(date) ); // nên xuất ra "TU"
```
2 changes: 1 addition & 1 deletion 1-js/05-data-types/11-date/3-weekday/_js.view/solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ function getLocalDay(date) {

let day = date.getDay();

if (day == 0) { // weekday 0 (sunday) is 7 in european
if (day == 0) { // ngày trong tuần 0 (chủ nhật) là 7 ở châu Âu
day = 7;
}

Expand Down
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
# Các ngày trong tuần của châu Âu

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`.
Các nước Châu Âu có các ngày trong tuần bắt đầu từ Thứ Hai (số 1), rồi Thứ Ba (số 2) và đến Chủ Nhật (số 7). Viết hàm `getLocalDay(date)` trả về ngày "Châu Âu" trong tuần cho `date`.

```js no-beautify
let date = new Date(2012, 0, 3); // 3 Jan 2012
alert( getLocalDay(date) ); // tuesday, should show 2
let date = new Date(2012, 0, 3); // 3 Thg 1 2012
alert( getLocalDay(date) ); // thứ ba, nên đưa ra 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`:
Ý tưởng rất đơn giản: trừ số ngày đã cho từ `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.
...Nhưng chức năng không nên thay đổi `date`. Đó là một điều quan trọng, bởi vì mã bên ngoài cung cấp cho chúng ta ngày không mong đợi nó thay đổi.

To implement it let's clone the date, like this:
Để thực hiện nó, hãy sao chép ngày, như thế này:

```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)
alert( getDateAgo(date, 1) ); // 1, (1 Thg 1 2015)
alert( getDateAgo(date, 2) ); // 31, (31 Thg 12 2014)
alert( getDateAgo(date, 365) ); // 2, (2 Thg 1 2014)
```
16 changes: 8 additions & 8 deletions 1-js/05-data-types/11-date/4-get-date-ago/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ importance: 4

---

# Which day of month was many days ago?
# Ngày nào trong tháng cách đây nhiều ngày?

Create a function `getDateAgo(date, days)` to return the day of month `days` ago from the `date`.
Tạo một hàm `getDateAgo(date, days)` để trả về ngày trong tháng `days` trước từ `date`.

For instance, if today is 20th, then `getDateAgo(new Date(), 1)` should be 19th and `getDateAgo(new Date(), 2)` should be 18th.
Ví dụ: nếu hôm nay là ngày 20, thì `getDateAgo(new Date(), 1)` sẽ là ngày 19 và `getDateAgo(new Date(), 2)` sẽ là ngày 18.

Should work reliably for `days=365` or more:
Nên hoạt động đáng tin cậy trong `day=365` trở lên:

```js
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)
alert( getDateAgo(date, 1) ); // 1, (1 Thg 1 2015)
alert( getDateAgo(date, 2) ); // 31, (31 Thg 12 2014)
alert( getDateAgo(date, 365) ); // 2, (2 Thg 1 2014)
```

P.S. The function should not modify the given `date`.
Tái bút: Hàm này nên được sửa đổi `date` đã cho.
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:
Hãy tạo một ngày bằng cách sử dụng tháng tiếp theo, nhưng chuyển số 0 thành ngày:
```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".
Thông thường, ngày bắt đầu từ 1, nhưng về mặt kỹ thuật, chúng ta có thể chuyển bất kỳ số nào, ngày sẽ tự động điều chỉnh. Vì vậy, khi chúng ta vượt qua 0, thì nó có nghĩa là "một ngày trước ngày đầu tiên của tháng", nói cách khác: "ngày cuối cùng của tháng trước".
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?
# Ngày cuối cùng của tháng?

Write a function `getLastDayOfMonth(year, month)` that returns the last day of month. Sometimes it is 30th, 31st or even 28/29th for Feb.
Viết hàm `getLastDayOfMonth(năm, tháng)` trả về ngày cuối cùng của tháng. Đôi khi đó là ngày 30, 31 hoặc thậm chí là 28/29 cho tháng Hai.

Parameters:
Thông số:

- `year` -- four-digits year, for instance 2012.
- `month` -- month, from 0 to 11.
- `year` -- năm có bốn chữ số, ví dụ 2012.
- `month` -- tháng, từ 0 đến 11.

For instance, `getLastDayOfMonth(2012, 1) = 29` (leap year, Feb).
Ví dụ: `getLastDayOfMonth(2012, 1) = 29` (năm nhuận, tháng 2).
12 changes: 6 additions & 6 deletions 1-js/05-data-types/11-date/6-get-seconds-today/solution.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
To get the number of seconds, we can generate a date using the current day and time 00:00:00, then substract it from "now".
Để có được số giây, chúng ta có thể tạo một ngày bằng cách sử dụng ngày và giờ hiện tại 00:00:00, sau đó trừ nó từ "bây giờ".

The difference is the number of milliseconds from the beginning of the day, that we should divide by 1000 to get seconds:
Sự khác biệt là số mili giây từ đầu ngày, mà chúng ta nên chia cho 1000 để có giây:

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

// create an object using the current day/month/year
// tạo một đối tượng sử dụng ngày/tháng/năm hiện tại
let today = new Date(now.getFullYear(), now.getMonth(), now.getDate());

let diff = now - today; // ms difference
return Math.round(diff / 1000); // make seconds
let diff = now - today; // chênh lệch ms
return Math.round(diff / 1000); // làm cho giây
}

alert( getSecondsToday() );
```

An alternative solution would be to get hours/minutes/seconds and convert them to seconds:
Một giải pháp thay thế sẽ là lấy giờ/phút/giây và chuyển đổi chúng thành giây:

```js run
function getSecondsToday() {
Expand Down
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?
# Hôm nay đã trôi qua bao nhiêu giây?

Write a function `getSecondsToday()` that returns the number of seconds from the beginning of today.
Viết một hàm `getSecondsToday()` để trả về số giây tính từ đầu ngày hôm nay.

For instance, if now were `10:00 am`, and there was no daylight savings shift, then:
Ví dụ: nếu bây giờ là `10:00 sáng` và không có ca làm tiết kiệm ánh sáng ban ngày, thì:

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

The function should work in any day. That is, it should not have a hard-coded value of "today".
Chức năng sẽ hoạt động trong bất kỳ ngày nào. Nghĩa là, nó không nên có giá trị mã hóa cứng là "hôm nay".
14 changes: 7 additions & 7 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,20 +1,20 @@
To get the number of milliseconds till tomorrow, we can from "tomorrow 00:00:00" substract the current date.
Để có được số mili giây cho đến ngày mai, chúng ta có thể từ "ngày mai 00:00:00" trừ đi ngày hiện tại.

First, we generate that "tomorrow", and then do it:
Đầu tiên, chúng ta tạo "ngày mai" đó, rồi thực hiện:

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

// tomorrow date
// ngày mai
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
let diff = tomorrow - now; // chênh lệch mili giây
return Math.round(diff / 1000); // chuyển đổi thành giây
}
```

Alternative solution:
Giải pháp thay thế:

```js run
function getSecondsToTomorrow() {
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.
Hãy lưu ý rằng nhiều quốc gia có Giờ tiết kiệm ánh sáng ban ngày (DST), vì vậy có thể có những ngày có 23 hoặc 25 giờ. Chúng ta có thể muốn xử lý những ngày như vậy một cách riêng biệt.
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?
# Còn bao nhiêu giây cho đến ngày mai?

Create a function `getSecondsToTomorrow()` that returns the number of seconds till tomorrow.
Tạo một hàm `getSecondsToTomorrow()` trả về số giây cho đến ngày mai.

For instance, if now is `23:00`, then:
Chẳng hạn, nếu bây giờ là `23:00`, thì:

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

P.S. The function should work at any day, the "today" is not hardcoded.
Tái bút: Hàm sẽ hoạt động vào bất kỳ ngày nào, "hôm nay" không được mã hóa cứng.
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@

function formatDate(date) {
let diff = new Date() - date; // the difference in milliseconds
let diff = new Date() - date; // sự khác biệt tính bằng mili giây

if (diff < 1000) { // less than 1 second
if (diff < 1000) { // chưa đầy 1 giây
return 'right now';
}

let sec = Math.floor(diff / 1000); // convert diff to seconds
let sec = Math.floor(diff / 1000); // chuyển đổi chênh lệch thành giây

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

let min = Math.floor(diff / 60000); // convert diff to minutes
let min = Math.floor(diff / 60000); // chuyển đổi chênh lệch thành phút
if (min < 60) {
return min + ' min. ago';
}

// format the date
// add leading zeroes to single-digit day/month/hours/minutes
// định dạng ngày
// thêm các số 0 đứng đầu vào ngày/tháng/giờ/phút có một chữ số
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
].map(component => component.slice(-2)); // lấy 2 chữ số cuối của mỗi thành phần

// join the components into date
// nối các thành phần vào ngày
return d.slice(0, 3).join('.') + ' ' + d.slice(3).join(':');
}
24 changes: 12 additions & 12 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,36 +1,36 @@
To get the time from `date` till now -- let's substract the dates.
Để lấy thời gian từ `date` cho đến bây giờ -- hãy trừ các ngày.

```js run demo
function formatDate(date) {
let diff = new Date() - date; // the difference in milliseconds
let diff = new Date() - date; // sự khác biệt tính bằng mili giây

if (diff < 1000) { // less than 1 second
if (diff < 1000) { // ít hơn 1 giây
return 'right now';
}

let sec = Math.floor(diff / 1000); // convert diff to seconds
let sec = Math.floor(diff / 1000); // chuyển đổi chênh lệch thành giây

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

let min = Math.floor(diff / 60000); // convert diff to minutes
let min = Math.floor(diff / 60000); // chuyển đổi chênh lệch thành phút
if (min < 60) {
return min + ' min. ago';
}

// format the date
// add leading zeroes to single-digit day/month/hours/minutes
// định dạng ngày
// thêm các số 0 đứng đầu vào ngày/tháng/giờ/phút có một chữ số
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
].map(component => component.slice(-2)); // lấy 2 chữ số cuối của mỗi thành phần

// join the components into date
// nối các thành phần vào ngày
return d.slice(0, 3).join('.') + ' ' + d.slice(3).join(':');
}

Expand All @@ -40,11 +40,11 @@ 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
// ngày hôm qua như 31.12.2016 20:00
alert( formatDate(new Date(new Date - 86400 * 1000)) );
```

Alternative solution:
Giải pháp thay thế:

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

// formatting
// định dạng
year = year.toString().slice(-2);
month = month < 10 ? '0' + month : month;
dayOfMonth = dayOfMonth < 10 ? '0' + dayOfMonth : dayOfMonth;
Expand Down
Loading