Skip to content

Date and time #175

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

Open
wants to merge 1 commit 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
4 changes: 2 additions & 2 deletions 1-js/05-data-types/11-date/1-new-date/solution.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The `new Date` constructor uses the local time zone. So the only important thing to remember is that months start from zero.
`new Date` 建構式使用的是本地時區。因此,唯一需要注意的事情是月份是從零開始。

So February has number 1.
因此二月的編號是 1。

```js run
let d = new Date(2012, 1, 20, 3, 12);
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.
建立一個 `Date` 物件來表示日期:2012 年 2 月 20 日,凌晨 3 點 12 分。時區為本地時區。

Show it using `alert`.
使用 `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.
方法 `date.getDay()` 返回從星期日開始的星期幾的編號。

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) {
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); // 2014 年 1 月 3 號
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
# 顯示星期幾

Write a function `getWeekDay(date)` to show the weekday in short format: 'MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU'.
寫一個函數 `getWeekDay(date)`,以簡短格式顯示星期幾:'MO''TU''WE''TH''FR''SA''SU'

For instance:
例如:

```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); // 2012 年 1 月 3 號
alert( getWeekDay(date) ); // 應該顯示 "TU"
```
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`.
歐洲國家的一周從星期一(號碼 1)開始,然後是星期二(號碼 2),一直到星期日(號碼 7)。寫一個函式 `getLocalDay(date)`,返回 `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); // 2012 年 1 月 3 號
alert( getLocalDay(date) ); // 星期二, 應該顯示 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`:
想法很簡單:從 `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.
...但是,函數不應該改變 `date`。這是很重要的一點,因為給我們日期的外部程式不希望它被改變。

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)
alert( getDateAgo(date, 1) ); // 1, (2015 年 1 月 1 號)
alert( getDateAgo(date, 2) ); // 31, (2014 年 12 月 31 號)
alert( getDateAgo(date, 365) ); // 2, (2014 年 1 月 2 號)
```
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?
# 多少天前是這個月的哪一天?

Create a function `getDateAgo(date, days)` to return the day of month `days` ago from the `date`.
創建一個函數 `getDateAgo(date, days)`,回傳從 `date` 開始算起 `days` 天前的那一天的日期。

For instance, if today is 20th, then `getDateAgo(new Date(), 1)` should be 19th and `getDateAgo(new Date(), 2)` should be 18th.
例如,如果今天是 20 號,那麼 `getDateAgo(new Date(), 1)` 應該是 19 號,`getDateAgo(new Date(), 2)` 應該是 18 號。

Should work reliably for `days=365` or more:
應該對 `days=365` 或更多天的情況也能正常運作:

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

P.S. The function should not modify the given `date`.
附註:該函數不應修改給定的 `date`
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".
通常,日期從 1 開始,但從技術上講,我們可以傳遞任何數字,日期將自動調整。所以當我們傳遞 0 時,它表示"月份第一天前的一天",換句話說:"上個月的最後一天"。
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.
撰寫一個函數 `getLastDayOfMonth(year, month)`,該函數返回該月的最後一天。有時是 30 日,31 日,甚至是 2 月的 28/29 日。

Parameters:
參數:

- `year` -- four-digits year, for instance 2012.
- `month` -- month, from 0 to 11.
- `year` -- 四位數的年份,例如 2012
- `month` -- 月份,從 0 到 11

For instance, `getLastDayOfMonth(2012, 1) = 29` (leap year, Feb).
例如,`getLastDayOfMonth(2012, 1) = 29`(閏年,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".
要獲得秒數,我們可以使用當前日期和時間 00:00:00 生成一個日期,然後從"現在"中減去它。

The difference is the number of milliseconds from the beginning of the day, that we should divide by 1000 to get seconds:
差值是從一天開始的毫秒數,我們應該將其除以 1000 以獲得秒數:

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

// create an object using the current day/month/year
// 使用當前的 天/月/年 建立一個物件:
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; // 毫秒差
return Math.round(diff / 1000); // 變成秒數
}

alert( getSecondsToday() );
```

An alternative solution would be to get hours/minutes/seconds and convert them to seconds:
另一種解法是取得 小時/分鐘/秒 並將它們轉換為秒:

```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?
# 今天已經過了多少秒?

Write a function `getSecondsToday()` that returns the number of seconds from the beginning of today.
撰寫一個函數 `getSecondsToday()`,該函數返回從今天開始已經過去的秒數。

For instance, if now were `10:00 am`, and there was no daylight savings shift, then:
例如,如果現在是 `上午10:00`,並且沒有日光節約時間變更,那麼:

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

The function should work in any day. That is, it should not have a hard-coded value of "today".
該函數應該在任何一天都能運行。也就是說,它不應該有一個寫死的"今天"的值。
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.
要獲得距離明天的毫秒數,我們可以從 "明天 00:00:00" 中減去當前日期。

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
let diff = tomorrow - now; // 毫秒差
return Math.round(diff / 1000); // 變成秒數
}
```

Alternative solution:
另一種解法:

```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.
請注意,許多國家實行夏令時制度(DST),因此可能有 23 小時或 25 小時的日子。我們可能需要單獨處理這些日子。
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.
建立一個函數 `getSecondsToTomorrow()`,用於回傳距離明天還有多少秒。

For instance, if now is `23:00`, then:
例如,如果現在是 `23:00`,則:

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

P.S. The function should work at any day, the "today" is not hardcoded.
附註:該函數應適用於任何一天,"今天"不是寫死的。
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.
要獲得從 `date` 到現在的時間,讓我們將日期相減。

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

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

let sec = Math.floor(diff / 1000); // convert diff to seconds
let sec = Math.floor(diff / 1000); // 轉換為秒數

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

let min = Math.floor(diff / 60000); // convert diff to minutes
let min = Math.floor(diff / 60000); // 轉換為分鐘數
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
].map(component => component.slice(-2)); // 取每個組件的最後 2 位數

// join the components into date
// 將組件組合成日期
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
// 昨天的日期,例如 31.12.2016 20:00
alert( formatDate(new Date(new Date - 86400 * 1000)) );
```

Alternative solution:
另一種解法:

```js run
function formatDate(date) {
Expand All @@ -58,7 +58,7 @@ 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;
Expand Down
16 changes: 8 additions & 8 deletions 1-js/05-data-types/11-date/8-format-date-relative/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ importance: 4

---

# Format the relative date
# 格式化相對日期

Write a function `formatDate(date)` that should format `date` as follows:
撰寫一個函數 `formatDate(date)`,應按照以下方式格式化 `date`

- If since `date` passed less than 1 second, then `"right now"`.
- Otherwise, if since `date` passed less than 1 minute, then `"n sec. ago"`.
- Otherwise, if less than an hour, then `"m min. ago"`.
- Otherwise, the full date in the format `"DD.MM.YY HH:mm"`. That is: `"day.month.year hours:minutes"`, all in 2-digit format, e.g. `31.12.16 10:00`.
- 如果從 `date` 到現在不足 1 秒,則顯示 `"right now"`
- 否則,如果從 `date` 到現在不足 1 分鐘,則顯示 `"n sec. ago"`
- 否則,如果不足一小時,則顯示 `"m min. ago"`
- 否則,以 `"DD.MM.YY HH:mm"` 的格式顯示完整日期。即:`"day.month.year hours:minutes"`,全部使用 2 位數格式,例如 `31.12.16 10:00`

For instance:
例如:

```js
alert( formatDate(new Date(new Date - 1)) ); // "right now"
Expand All @@ -20,6 +20,6 @@ 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.16 20:00
// 昨天的日期,例如 31.12.16 20:00
alert( formatDate(new Date(new Date - 86400 * 1000)) );
```
Loading