Skip to content
Merged
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
2 changes: 1 addition & 1 deletion 1-js/05-data-types/11-date/1-new-date/solution.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
`new Date` 构造函数默认使用当地时区。所以唯一需要牢记的是月份从 0 开始计数。
`new Date` 构造函数默认使用本地时区。所以唯一需要牢记的就是月份从 0 开始计数。

所以二月对应的数值是 1。

Expand Down
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,8 +1,8 @@
`date.getDay()` 方法返回星期数,从星期日开始
`date.getDay()` 方法返回从星期日开始的星期数

我们创建一个星期数组,这样可以通过它的序号得到名称
我们创建一个关于星期的数组,这样我们就可以通过编号获取正确的日期名称

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

Expand Down
6 changes: 3 additions & 3 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,11 +2,11 @@ importance: 5

---

# 展示星期数
# 显示星期数

写一个函数 `getWeekDay(date)` 来显示一个日期的星期数,用简写表示:'MO''TU''WE''TH''FR''SA''SU'。
编写一个函数 `getWeekDay(date)` 以短格式来显示一个日期的星期数:'MO''TU''WE''TH''FR''SA''SU'。

举个例子
例如:

```js no-beautify
let date = new Date(2012, 0, 3); // 3 Jan 2012
Expand Down
13 changes: 0 additions & 13 deletions 1-js/05-data-types/11-date/3-weekday/solution.md
Original file line number Diff line number Diff line change
@@ -1,14 +1 @@
```js run
function getLocalDay(date) {

let day = date.getDay();

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

return day;
}

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

# 欧洲的星期表示方法

欧洲国家的星期计算是从星期一(数字 1)开始,然后星期二(数字 2),直到星期日(数字 7)。写一个函数 `getLocalDay(date)`,返回日期的欧洲式星期数
欧洲国家的星期计算是从星期一(数字 1)开始的,然后是星期二(数字 2),直到星期日(数字 7)。编写一个函数 `getLocalDay(date)`,并返回日期的欧洲式星期数

```js no-beautify
let date = new Date(2012, 0, 3); // 3 Jan 2012
alert( getLocalDay(date) );       // 星期二,应该返回 2
alert( getLocalDay(date) ); // 星期二,应该显示 2
```
4 changes: 2 additions & 2 deletions 1-js/05-data-types/11-date/4-get-date-ago/solution.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ function getDateAgo(date, days) {
}
```

...但是函数不能修改 `date`。这一点很重要,因为外部环境不希望它被修改
……但是函数不能修改 `date`。这一点很重要,因为我们提供日期的外部代码不希望它被修改

要实现这一点,我们可以复制这个日期,就像这样:

```js run
```js run demo
function getDateAgo(date, days) {
let dateCopy = new Date(date);

Expand Down
8 changes: 4 additions & 4 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,11 +2,11 @@ importance: 4

---

# 许多天之前是该月的哪一天
# 许多天之前是哪个月几号

写一个函数 `getDateAgo(date, days)`,返回特定日期 `date` 往前 `days` 天数后,是当月的哪一天
写一个函数 `getDateAgo(date, days)`,返回特定日期 `date` 往前 `days` 天是哪个月的哪一天

举个例子,今天是 20 号,那么 `getDateAgo(new Date(), 1)` 应该是 19 号,`getDateAgo(new Date(), 2)` 应该是 18 号。
例如,假设今天是 20 号,那么 `getDateAgo(new Date(), 1)` 的结果应该是 19 号,`getDateAgo(new Date(), 2)` 的结果应该是 18 号。

跨月、年也应该是正确输出:

Expand All @@ -18,4 +18,4 @@ alert( getDateAgo(date, 2) ); // 31, (31 Dec 2014)
alert( getDateAgo(date, 365) ); // 2, (2 Jan 2014)
```

另:函数不能修改给定的 `date` 值。
P.S. 函数不应该修改给定的 `date` 值。
6 changes: 3 additions & 3 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,5 +1,5 @@
Let's create a date using the next month, but pass zero as the day:
```js run
让我们使用下个月创建日期,但将零作为天数(day)传递:
```js run demo
function getLastDayOfMonth(year, month) {
let date = new Date(year, month + 1, 0);
return date.getDate();
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 时,它的意思是“一个月的第一天的前一天”,换句话说:“上个月的最后一天”。
4 changes: 2 additions & 2 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 @@ -4,11 +4,11 @@ importance: 5

# 某月的最后一天?

写一个函数 `getLastDayOfMonth(year, month)`,返回某月的最后一天,有时候是 30,有时是31,甚至是二月的 28/29。
写一个函数 `getLastDayOfMonth(year, month)` 返回 month 月的最后一天。有时候是 30,有时是 31,甚至在二月的时候会是 28/29。

参数:

- `year` —— 四位数的年份,比如 2012。
- `month` —— 月份,从 0 到 11。

举个例子,`getLastDayOfMonth(2012, 1) = 29`
举个例子,`getLastDayOfMonth(2012, 1) = 29`(闰年,二月)
10 changes: 5 additions & 5 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,13 +1,13 @@
为获取秒数,我们可以创建一个日期,使用今天的日期和 00:00:00 这个时间,然后当前时间减去该时间
为获取秒数,我们可以使用今天的日期和 00:00:00 这个时间创建一个日期,然后使用当前时间减去该时间

不同之处在于,这样得到的今天之初的日期是毫秒计算,我们应该除以 1000,得到秒数
不同之处在于,从今天之初开始算起的时间是以毫秒计算的,我们应该将其处以 1000,进而得到秒数

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

 // 创建一个对象,使用当前的 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() );
```

另一种解决方法是得到 hours/minutes/seconds,然后把它们转化为秒数
另一种解决方法是获取 hours/minutes/seconds,然后把它们转换为秒数

```js run
function getSecondsToday() {
Expand Down
4 changes: 2 additions & 2 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 @@ -6,10 +6,10 @@ importance: 5

写一个函数 `getSecondsToday()`,返回今天已经过去了多少秒?

举个例子:如果现在是`10:00 am`,并且没有夏令时转移,那么:
例如:如果现在是 `10:00 am`,并且没有夏令时转换,那么:

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

该函数应该在任意一天都能正确运行。那意味着,它不应具有「今天」的硬编码值。
该函数应该在任意一天都能正确运行。那意味着,它不应具有“今天”的硬编码值。
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
为获取距离明天的毫秒数,我们可以用「第二天 00:00:00」这个对象减去当前的日期。
首先我们生成「第二天」,然后对它做操作:
为获取距离明天的毫秒数,我们可以用“明天 00:00:00”这个日期减去当前的日期。

首先我们生成“明天”,然后对其进行减法操作:

```js run
function getSecondsToTomorrow() {
Expand Down Expand Up @@ -28,4 +29,4 @@ function getSecondsToTomorrow() {
}
```

请注意,很多国家有夏令时(DST),因此他们可能一天有 23 小时或者 25 小时。我们对这些天数要区别对待。
请注意,很多国家有夏令时(DST),因此他们的一天可能有 23 小时或者 25 小时。我们对这些天数要区别对待。
4 changes: 2 additions & 2 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 @@ -6,10 +6,10 @@ importance: 5

写一个函数 `getSecondsToTomorrow()`,返回距离明天的秒数。

举个例子,现在是 `23:00`,那么:
例如,现在是 `23:00`,那么:

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

另:该函数应该能在任意一天运行
P.S. 该函数应该在任意一天都能正确运行。那意味着,它不应具有“今天”的硬编码值。
28 changes: 15 additions & 13 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,26 +1,26 @@
为获取 `date` 距离当前时间的间隔 —— 我们将两个日期相减。
为了获取 `date` 距离当前时间的间隔 —— 我们将两个日期相减。

```js run
```js run demo
function formatDate(date) {
 let diff = new Date() - date; // 差值用毫秒表示
let diff = new Date() - date; // 以毫秒表示的差值

 if (diff < 1000) { // 少于一秒
 if (diff < 1000) { // 少于 1 秒
   return 'right now';
}

 let sec = Math.floor(diff / 1000); // 将间隔转化为秒
let sec = Math.floor(diff / 1000); // 将 diff 转换为秒

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

 let min = Math.floor(diff / 60000); // 将间隔转化为分钟
let min = Math.floor(diff / 60000); // 将 diff 转换为分钟
 if (min < 60) {
return min + ' min. ago';
}

 // 格式化日期
 // 在单个数值之前加 0 日/月/小时/分钟
// 格式化 date
// 将前置 0 加到一位数 day/month/hours/minutes 前
let d = date;
d = [
'0' + d.getDate(),
Expand All @@ -40,7 +40,7 @@ alert( formatDate(new Date(new Date - 30 * 1000)) ); // "30 sec. ago"

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

// 昨天的日期如: 31.12.2016, 20:00
// 昨天的日期如:31.12.2016, 20:00
alert( formatDate(new Date(new Date - 86400 * 1000)) );
```

Expand All @@ -57,12 +57,14 @@ function formatDate(date) {
let diffSec = Math.round(diffMs / 1000);
let diffMin = diffSec / 60;
let diffHour = diffMin / 60;
 // 格式化
 year = year.toString().slice(-2);

// 格式化
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) {
Expand Down
12 changes: 6 additions & 6 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 @@ -4,12 +4,12 @@ importance: 4

# 格式化相对日期

写一个函数 `formatDate(date)`,能够将 `date` 格式化如下
写一个函数 `formatDate(date)`,能够对 `date` 进行如下格式化

- 如果 `date` 距离现在少于 1 秒,输出 `"刚刚"`。
- 否则,如果少于 1 分钟,输出 `"n 秒之前"`。
- 否则,如果少于 1 小时,输出 `"n 分钟之前"`。
- 否则,输出完整日期,用格式`"DD.MM.YY HH:mm"`。即:`"day.month.year hours:minutes"`,所有的数都用两位数表示,例如:`31.12.16 10:00`。
- 如果 `date` 距离现在不到 1 秒,输出 `"right now"`。
- 否则,如果 `date` 距离现在不到 1 分钟,输出 `"n sec. ago"`。
- 否则,如果不到 1 小时,输出 `"m min. ago"`。
- 否则,`"DD.MM.YY HH:mm"` 格式输出完整日期。即:`"day.month.year hours:minutes"`,全部以两位数格式表示,例如:`31.12.16 10:00`。

举个例子:

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.2016, 20:00
// yesterday's date like 31.12.16, 20:00
alert( formatDate(new Date(new Date - 86400 * 1000)) );
```
Loading