Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2b92a6b
Translate a part of article
mahdiHash Aug 12, 2021
9a92e61
Translate a part of article
mahdiHash Aug 13, 2021
ad3968e
Add English term
mahdiHash Aug 13, 2021
87837a5
Translate a part of article
mahdiHash Aug 14, 2021
da70de4
Typo
mahdiHash Aug 14, 2021
545d065
Add a missing pronoun
mahdiHash Aug 14, 2021
c7d40b1
Translate a part of article
mahdiHash Aug 15, 2021
206704b
Translate a part of article
mahdiHash Aug 15, 2021
a6cc74d
Merge branch 'javascript-tutorial:master' into master
mahdiHash Aug 16, 2021
5808cf5
Translate a part of article
mahdiHash Aug 16, 2021
01a1089
Translate a part of article
mahdiHash Aug 17, 2021
87618e3
Translate article
mahdiHash Aug 18, 2021
69b5161
Translate task of "new date"
mahdiHash Aug 19, 2021
5fdcb79
Translate solution of "new date"
mahdiHash Aug 19, 2021
0a19720
Translate task of "get week day"
mahdiHash Aug 19, 2021
7ce160b
Translate solution of "get week day"
mahdiHash Aug 19, 2021
6b24d51
Translate task of "weekday"
mahdiHash Aug 19, 2021
8853862
Fix a translation mistake
mahdiHash Aug 19, 2021
db88420
Translate solution.js of "weekday"
mahdiHash Aug 19, 2021
251de89
Translate task of "get date ago"
mahdiHash Aug 20, 2021
9fdba6a
Translate solution of "get date ago"
mahdiHash Aug 20, 2021
5cb395a
Translate task of "last day of month"
mahdiHash Aug 20, 2021
6eaa1ca
Translate solution of "last day of month"
mahdiHash Aug 20, 2021
bfa29a0
Translate task of "get seconds today"
mahdiHash Aug 20, 2021
1d1095c
Translate solution of "get seconds today"
mahdiHash Aug 20, 2021
9ac0369
Translate task of "get seconds to tomorrow"
mahdiHash Aug 20, 2021
44fc7a1
Translate solution of "get seconds to tomorrow"
mahdiHash Aug 20, 2021
db6f956
Fix a translation mistake
mahdiHash Aug 20, 2021
306ddb7
Translate task of "format date relative"
mahdiHash Aug 20, 2021
725e637
Translate solution of "format date relative"
mahdiHash Aug 20, 2021
6482007
fix: fix a typo
mahdyar Aug 20, 2021
8d31cd3
Change "مهرزمانی" to "برچسب زمان"
mahdiHash Aug 20, 2021
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.
تابع سازنده `new Date` از منطقه زمانی محلی استفاده می‌کند. پس تنها چیز مهم به یاد داشتن این است که ماه‌ها از صفر شروع می‌شوند.

So February has number 1.
پس فوریه عدد 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.
یک شیء `Date` برای این تاریخ بسازید: 20 فوریه 2012، ساعت 3:12 قبل از ظهر. منطقه زمانی محلی است.

Show it using `alert`.
با استفاده از `alert` آن را نمایش دهید.
4 changes: 2 additions & 2 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
function getWeekDay(date) {
Expand Down
8 changes: 4 additions & 4 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"
alert( getWeekDay(date) ); // را نشان دهد "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) { // روز هفته 0 (یکشنبه) به اروپایی برابر با 7 است
day = 7;
}

Expand Down
6 changes: 3 additions & 3 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
alert( getLocalDay(date) ); // سه‌شنبه، باید 2 را برگرداند
```
6 changes: 3 additions & 3 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
function getDateAgo(date, days) {
Expand Down
10 changes: 5 additions & 5 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,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)` بسازید که روز ماه را به اندازه `days` روز قبل از `date` برگرداند.

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);
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` داده شده را تغییر دهد.
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:
بیایید یک تاریخ با استفاده از ماه بعد بسازیم اما برای روز 0 را قرار دهیم:
```js run
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 شروع می‌شوند اما از لحاظ فنی ما می‌توانیم هر عددی را قرار دهیم، شیء date به طور خودکار خودش را تنظیم می‌کند. پس زمانی که ما 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ام یا حتی برای ماه فوریه 28/29ام است.

Parameters:
پارامترها:

- `year` -- four-digits year, for instance 2012.
- `month` -- month, from 0 to 11.
- `year` -- سال 4 رقمی، برای مثال 2012.
- `month` -- ماه، از 0 تا 11.

For instance, `getLastDayOfMonth(2012, 1) = 29` (leap year, Feb).
برای مثال، `getLastDayOfMonth(2012, 1) = 29` (سال کبیسه، فوریه).
14 changes: 7 additions & 7 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 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".
تابع باید به ازای هر روزی کار کند. یعنی اینکه نباید برای «امروز» کد اختصاصی داشته باشد.
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.
پی‌نوشت: تابع باشد به ازای هر روزی کار کند، «امروز» کد اختصاصی ندارد.
22 changes: 11 additions & 11 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
return 'right now';
}

let sec = Math.floor(diff / 1000); // convert diff to seconds
let sec = Math.floor(diff / 1000); // به ثانیه diff تبدیل

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

let min = Math.floor(diff / 60000); // convert diff to minutes
let min = Math.floor(diff / 60000); // به دقیقه diff تبدیل
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
// تاریخ دیروز مانند 20:00 31.12.2016
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"`(n ثانیه قبل) است.
- در غیر این صورت اگر کمتر از یک ساعت باشد، سپس شکل جدید `"m min. ago"`(m دقیقه پیش) است.
- در غیر این صورت، تاریخ کامل با به شکل `"DD.MM.YY HH:mm"` باشد. یعنی: `"day.month.year hours:minutes"`، همه به شکل دو رقمی مانند `10:00 31.12.16`.

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
// تاریخ دیروز مانند 20:00 31.12.16
alert( formatDate(new Date(new Date - 86400 * 1000)) );
```
Loading