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

日期 #76

Merged
merged 28 commits into from May 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
1d48042
Update article.md
allenlongbaobao Apr 29, 2018
269676b
Update task.md
allenlongbaobao Apr 29, 2018
64c018a
Update solution.md
allenlongbaobao Apr 29, 2018
8fbbebc
Update task.md
allenlongbaobao Apr 29, 2018
ac5872d
Update solution.md
allenlongbaobao Apr 29, 2018
2ae02b0
Update task.md
allenlongbaobao Apr 29, 2018
a594a4c
Update solution.md
allenlongbaobao Apr 29, 2018
97a53d1
Update task.md
allenlongbaobao Apr 29, 2018
32ea9a2
Update solution.md
allenlongbaobao Apr 29, 2018
8de172e
Update task.md
allenlongbaobao Apr 29, 2018
4646d8f
Update task.md
allenlongbaobao Apr 29, 2018
15b7312
Update task.md
allenlongbaobao Apr 29, 2018
1da066f
Update solution.md
allenlongbaobao Apr 29, 2018
d177878
Update solution.md
allenlongbaobao Apr 29, 2018
93e1b77
Update task.md
allenlongbaobao Apr 29, 2018
07cb12b
Update solution.md
allenlongbaobao Apr 29, 2018
c5c8500
Update task.md
allenlongbaobao Apr 29, 2018
55eca00
Update solution.md
allenlongbaobao Apr 29, 2018
37e3cab
Update task.md
allenlongbaobao May 5, 2018
e85eb44
Update solution.md
allenlongbaobao May 5, 2018
19a2258
Update solution.md
allenlongbaobao May 5, 2018
74c5cb1
Update task.md
allenlongbaobao May 5, 2018
49337ba
Update task.md
allenlongbaobao May 5, 2018
93d7330
Update task.md
allenlongbaobao May 5, 2018
93c3ead
Update solution.md
allenlongbaobao May 5, 2018
498b32d
Update task.md
allenlongbaobao May 5, 2018
1008570
按两位校对者意见修改
allenlongbaobao May 5, 2018
1f19ffb
Update solution.md
leviding May 6, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions 1-js/05-data-types/10-date/1-new-date/solution.md
@@ -1,6 +1,6 @@
The `new Date` constructor uses the local time zone by default. So the only important thing to remember is that months start from zero.
`new Date` 构造函数默认使用当地时区。所以唯一需要牢记的是月份从 0 开始计数。

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/10-date/1-new-date/task.md
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` 对象,日期是:Feb 20, 2012, 3:12am。时区是当地时区。

Show it using `alert`.
使用 `alert` 显示结果。
4 changes: 2 additions & 2 deletions 1-js/05-data-types/10-date/2-get-week-day/solution.md
@@ -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
function getWeekDay(date) {
Expand Down
8 changes: 4 additions & 4 deletions 1-js/05-data-types/10-date/2-get-week-day/task.md
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"
alert( getWeekDay(date) );       // 应该输出 "TU"
```
2 changes: 1 addition & 1 deletion 1-js/05-data-types/10-date/3-weekday/solution.md
Expand Up @@ -3,7 +3,7 @@ function getLocalDay(date) {

let day = date.getDay();

if (day == 0) { // 0 becomes 7
 if (day == 0) { // 0,改为 7
day = 7;
}

Expand Down
6 changes: 3 additions & 3 deletions 1-js/05-data-types/10-date/3-weekday/task.md
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)`,返回日期的欧洲式星期数。

```js no-beautify
let date = new Date(2012, 0, 3); // 3 Jan 2012
alert( getLocalDay(date) ); // tuesday, should show 2
alert( getLocalDay(date) );       // 星期二,应该返回 2
```
6 changes: 3 additions & 3 deletions 1-js/05-data-types/10-date/4-get-date-ago/solution.md
@@ -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
function getDateAgo(date, days) {
Expand Down
10 changes: 5 additions & 5 deletions 1-js/05-data-types/10-date/4-get-date-ago/task.md
Expand Up @@ -2,13 +2,13 @@ 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 also work over months/years reliably:
跨月、年也应该是正确输出:

```js
let date = new Date(2015, 0, 2);
Expand All @@ -18,4 +18,4 @@ alert( getDateAgo(date, 2) ); // 31, (31 Dec 2014)
alert( getDateAgo(date, 365) ); // 2, (2 Jan 2014)
```

P.S. The function should not modify the given `date`.
另:函数不能修改给定的 `date` 值。
12 changes: 6 additions & 6 deletions 1-js/05-data-types/10-date/5-last-day-of-month/task.md
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,甚至是二月的 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`
10 changes: 5 additions & 5 deletions 1-js/05-data-types/10-date/6-get-seconds-today/solution.md
@@ -1,13 +1,13 @@
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());
 // 创建一个对象,使用当前的 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
Expand All @@ -16,7 +16,7 @@ function getSecondsToday() {
alert( getSecondsToday() );
```

An alternative solution would be to get hours/minutes/seconds and convert them to seconds:
另一种解决方法是得到 hours/minutes/seconds,然后把它们转化为秒数:

```js run
function getSecondsToday() {
Expand Down
8 changes: 4 additions & 4 deletions 1-js/05-data-types/10-date/6-get-seconds-today/task.md
Expand Up @@ -2,14 +2,14 @@ importance: 5

---

# How many seconds has passed today?
# 今天过去了多少秒?

Write a function `getSecondsToday()` that returns the number of seconds from the beginning of today.
写一个函数 `getSecondsToday()`,返回今天已经过去了多少秒?

For instance, if now `10:00 am`, and there was no daylight savings shift, then:
举个例子:如果现在是`10:00 am`,并且没有夏令时转移,那么:

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

The function should work in any day. That is, it should not have a hard-coded value of "today".
该函数应该在任意一天都能正确运行。那意味着,不应该有一个「今天」这个参数不能有意外值。
@@ -1,6 +1,5 @@
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:
为获取距离明天的毫秒数,我们可以用「第二天 00:00:00」这个对象减去当前的日期。
首先我们生成「第二天」,然后对它做操作:

```js run
function getSecondsToTomorrow() {
Expand All @@ -14,7 +13,7 @@ function getSecondsToTomorrow() {
}
```

Alternative solution:
另一种解法:

```js run
function getSecondsToTomorrow() {
Expand All @@ -29,4 +28,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/10-date/7-get-seconds-to-tomorrow/task.md
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.
另:该函数应该能在任意一天运行
32 changes: 16 additions & 16 deletions 1-js/05-data-types/10-date/8-format-date-relative/solution.md
@@ -1,37 +1,37 @@
To get the time from `date` till now -- let's substract the dates.
为获取 `date` 距离当前时间的间隔 —— 我们将两个日期相减。

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

if (diff < 1000) { // less than 1 second
return 'right now';
 if (diff < 1000) { // 少于一秒
   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
if (min < 60) {
 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
 // 格式化日期
 // 在单个数值之前加 0 日/月/小时/分钟
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)); // 得到每个组件的后两位

// join the components into date
return d.slice(0, 3).join('.') + ' ' + d.slice(3).join(':');
 // 将时间信息和日期组合在一起
return d.slice(0, 3).join('.') + ' ' + d.slice(3).join(':');
}

alert( formatDate(new Date(new Date - 1)) ); // "right now"
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,8 +58,8 @@ function formatDate(date) {
let diffMin = diffSec / 60;
let diffHour = diffMin / 60;

// formatting
year = year.toString().slice(-2);
 // 格式化
 year = year.toString().slice(-2);
month = month < 10 ? '0' + month : month;
dayOfMonth = dayOfMonth < 10 ? '0' + dayOfMonth : dayOfMonth;

Expand Down
14 changes: 7 additions & 7 deletions 1-js/05-data-types/10-date/8-format-date-relative/task.md
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 秒,输出 `"刚刚"`。
- 否则,如果少于 1 分钟,输出 `"n 秒之前"`。
- 否则,如果少于 1 小时,输出 `"n 分钟之前"`。
- 否则,输出完整日期,用格式`"DD.MM.YY HH:mm"`。即:`"day.month.year hours:minutes"`,所有的数都用两位数表示,例如:`31.12.16 10:00`

For instance:
举个例子:

```js
alert( formatDate(new Date(new Date - 1)) ); // "right now"
Expand Down