diff --git a/1-js/05-data-types/11-date/1-new-date/solution.md b/1-js/05-data-types/11-date/1-new-date/solution.md index bed449453..3e476c5b9 100644 --- a/1-js/05-data-types/11-date/1-new-date/solution.md +++ b/1-js/05-data-types/11-date/1-new-date/solution.md @@ -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. +Constructorul `new Date` utilizează fusul orar local. Așadar singurul lucru important de reținut este că lunile încep de la zero. -So February has number 1. +Deci Februarie are numărul 1. -Here's an example with numbers as date components: +Iată un exemplu cu numere ca și componente ale datei: ```js run -//new Date(year, month, date, hour, minute, second, millisecond) +//new Date(an, lună, dată, oră, minut, secundă, milisecundă) let d1 = new Date(2012, 1, 20, 3, 12); alert( d1 ); ``` -We could also create a date from a string, like this: +Am putea de asemenea să creăm o dată dintr-un șir, în felul următor: ```js run //new Date(datastring) diff --git a/1-js/05-data-types/11-date/1-new-date/task.md b/1-js/05-data-types/11-date/1-new-date/task.md index 1b40d5ac0..61d5e535a 100644 --- a/1-js/05-data-types/11-date/1-new-date/task.md +++ b/1-js/05-data-types/11-date/1-new-date/task.md @@ -2,8 +2,8 @@ importance: 5 --- -# Create a date +# Creați o dată -Create a `Date` object for the date: Feb 20, 2012, 3:12am. The time zone is local. +Creați un obiect `Date` pentru data: Feb 20, 2012, 3:12am. Fusul orar este local. -Show it using `alert`. +Afișați-o folosind `alert`. diff --git a/1-js/05-data-types/11-date/2-get-week-day/solution.md b/1-js/05-data-types/11-date/2-get-week-day/solution.md index 58d75c1c3..b88f4dfe0 100644 --- a/1-js/05-data-types/11-date/2-get-week-day/solution.md +++ b/1-js/05-data-types/11-date/2-get-week-day/solution.md @@ -1,6 +1,6 @@ -The method `date.getDay()` returns the number of the weekday, starting from sunday. +Metoda `date.getDay()` returnează numărul zilei săptămânii, începând de duminică. -Let's make an array of weekdays, so that we can get the proper day name by its number: +Să creăm o matrice de zile ale săptămânii, astfel încât să putem obține numele zilei corespunzătoare prin numărul ei: ```js run demo function getWeekDay(date) { diff --git a/1-js/05-data-types/11-date/2-get-week-day/task.md b/1-js/05-data-types/11-date/2-get-week-day/task.md index 5cf31565d..44e766bc4 100644 --- a/1-js/05-data-types/11-date/2-get-week-day/task.md +++ b/1-js/05-data-types/11-date/2-get-week-day/task.md @@ -2,13 +2,13 @@ importance: 5 --- -# Show a weekday +# Arată o zi a săptămânii -Write a function `getWeekDay(date)` to show the weekday in short format: 'MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU'. +Scrieți o funcție `getWeekDay(date)` pentru a afișa ziua săptămânii în format scurt: 'MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU'. -For instance: +De exemplu: ```js no-beautify let date = new Date(2012, 0, 3); // 3 Jan 2012 -alert( getWeekDay(date) ); // should output "TU" +alert( getWeekDay(date) ); // ar trebui să iasă "TU" ``` diff --git a/1-js/05-data-types/11-date/3-weekday/_js.view/solution.js b/1-js/05-data-types/11-date/3-weekday/_js.view/solution.js index fb9e3d2a4..ee31d80ca 100644 --- a/1-js/05-data-types/11-date/3-weekday/_js.view/solution.js +++ b/1-js/05-data-types/11-date/3-weekday/_js.view/solution.js @@ -2,7 +2,7 @@ function getLocalDay(date) { let day = date.getDay(); - if (day == 0) { // weekday 0 (sunday) is 7 in european + if (day == 0) { // ziua săptămânii 0 (sunday) este 7 în Europa day = 7; } diff --git a/1-js/05-data-types/11-date/3-weekday/_js.view/test.js b/1-js/05-data-types/11-date/3-weekday/_js.view/test.js index 57032154f..28e4eaae3 100644 --- a/1-js/05-data-types/11-date/3-weekday/_js.view/test.js +++ b/1-js/05-data-types/11-date/3-weekday/_js.view/test.js @@ -1,4 +1,4 @@ -describe("getLocalDay returns the \"european\" weekday", function() { +describe("getLocalDay returnează ziua \"europeană\"", function() { it("3 January 2014 - friday", function() { assert.equal(getLocalDay(new Date(2014, 0, 3)), 5); }); diff --git a/1-js/05-data-types/11-date/3-weekday/task.md b/1-js/05-data-types/11-date/3-weekday/task.md index ba62790cf..07b9456d1 100644 --- a/1-js/05-data-types/11-date/3-weekday/task.md +++ b/1-js/05-data-types/11-date/3-weekday/task.md @@ -2,11 +2,11 @@ importance: 5 --- -# European weekday +# Zi lucrătoare europeană -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`. +Țările europene au zile ale săptămânii care încep cu Monday (numărul 1), apoi Tuesday (numărul 2) și până Sunday (numărul 7). Scrieți o funcție `getLocalDay(date)` care returnează ziua săptămânii "Europene" pentru `date`. ```js no-beautify let date = new Date(2012, 0, 3); // 3 Jan 2012 -alert( getLocalDay(date) ); // tuesday, should show 2 +alert( getLocalDay(date) ); // tuesday, ar trebui să arate 2 ``` diff --git a/1-js/05-data-types/11-date/4-get-date-ago/_js.view/test.js b/1-js/05-data-types/11-date/4-get-date-ago/_js.view/test.js index 255acffe0..d41f9d9ab 100644 --- a/1-js/05-data-types/11-date/4-get-date-ago/_js.view/test.js +++ b/1-js/05-data-types/11-date/4-get-date-ago/_js.view/test.js @@ -1,23 +1,23 @@ describe("getDateAgo", function() { - it("1 day before 02.01.2015 -> day 1", function() { + it("1 zi înainte de 02.01.2015 -> day 1", function() { assert.equal(getDateAgo(new Date(2015, 0, 2), 1), 1); }); - it("2 days before 02.01.2015 -> day 31", function() { + it("2 zile înainte de 02.01.2015 -> ziua 31", function() { assert.equal(getDateAgo(new Date(2015, 0, 2), 2), 31); }); - it("100 days before 02.01.2015 -> day 24", function() { + it("100 zile înainte de 02.01.2015 -> ziua 24", function() { assert.equal(getDateAgo(new Date(2015, 0, 2), 100), 24); }); - it("365 days before 02.01.2015 -> day 2", function() { + it("365 zile înainte de 02.01.2015 -> ziua 2", function() { assert.equal(getDateAgo(new Date(2015, 0, 2), 365), 2); }); - it("does not modify the given date", function() { + it("nu modifică date-ul dat", function() { let date = new Date(2015, 0, 2); let dateCopy = new Date(date); getDateAgo(dateCopy, 100); diff --git a/1-js/05-data-types/11-date/4-get-date-ago/solution.md b/1-js/05-data-types/11-date/4-get-date-ago/solution.md index 5c394c100..5e7ba629d 100644 --- a/1-js/05-data-types/11-date/4-get-date-ago/solution.md +++ b/1-js/05-data-types/11-date/4-get-date-ago/solution.md @@ -1,4 +1,4 @@ -The idea is simple: to substract given number of days from `date`: +Ideea este simplă: se scade un anumit număr de zile din `date`: ```js function getDateAgo(date, days) { @@ -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. +...Dar funcția nu ar trebui să modifice `date`. Acesta este un lucru important, deoarece codul exterior care ne oferă data nu se așteaptă ca aceasta să se schimbe. -To implement it let's clone the date, like this: +Pentru a implementa acest lucru haideŧi să clonăm data, astfel: ```js run demo function getDateAgo(date, days) { diff --git a/1-js/05-data-types/11-date/4-get-date-ago/task.md b/1-js/05-data-types/11-date/4-get-date-ago/task.md index 058d39c7e..43bbaa210 100644 --- a/1-js/05-data-types/11-date/4-get-date-ago/task.md +++ b/1-js/05-data-types/11-date/4-get-date-ago/task.md @@ -2,13 +2,13 @@ importance: 4 --- -# Which day of month was many days ago? +# Ce zi a lunii a fost acum multe zile? -Create a function `getDateAgo(date, days)` to return the day of month `days` ago from the `date`. +Creați o funcție `getDateAgo(date, days)` pentru a returna ziua din lună de acum `days` din `date`. -For instance, if today is 20th, then `getDateAgo(new Date(), 1)` should be 19th and `getDateAgo(new Date(), 2)` should be 18th. +De exemplu, dacă astăzi este 20, atunci `getDateAgo(new Date(), 1)` ar trebui să fie 19 și `getDateAgo(new Date(), 2)` ar trebui să fie 18. -Should work reliably for `days=365` or more: +Ar trebui să funcționeze în mod fiabil pentru `days=365` sau mai mult: ```js let date = new Date(2015, 0, 2); @@ -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`. +P.S. Funcția nu trebuie să modifice `date`-ul dat. diff --git a/1-js/05-data-types/11-date/5-last-day-of-month/_js.view/test.js b/1-js/05-data-types/11-date/5-last-day-of-month/_js.view/test.js index 4ff3e116a..b46fea1ac 100644 --- a/1-js/05-data-types/11-date/5-last-day-of-month/_js.view/test.js +++ b/1-js/05-data-types/11-date/5-last-day-of-month/_js.view/test.js @@ -1,13 +1,13 @@ describe("getLastDayOfMonth", function() { - it("last day of 01.01.2012 - 31", function() { + it("ultima zi din 01.01.2012 - 31", function() { assert.equal(getLastDayOfMonth(2012, 0), 31); }); - it("last day of 01.02.2012 - 29 (leap year)", function() { + it("ultima zi din 01.02.2012 - 29 (an bisect)", function() { assert.equal(getLastDayOfMonth(2012, 1), 29); }); - it("last day of 01.02.2013 - 28", function() { + it("ultima zi din 01.02.2013 - 28", function() { assert.equal(getLastDayOfMonth(2013, 1), 28); }); }); diff --git a/1-js/05-data-types/11-date/5-last-day-of-month/solution.md b/1-js/05-data-types/11-date/5-last-day-of-month/solution.md index 4f642536e..f161c2903 100644 --- a/1-js/05-data-types/11-date/5-last-day-of-month/solution.md +++ b/1-js/05-data-types/11-date/5-last-day-of-month/solution.md @@ -1,4 +1,4 @@ -Let's create a date using the next month, but pass zero as the day: +Să creăm o dată folosind luna următoare, dar să trecem zero ca zi: ```js run demo function getLastDayOfMonth(year, month) { let date = new Date(year, month + 1, 0); @@ -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". +În mod normal, datele încep de la 1, dar, din punct de vedere tehnic putem trece orice număr, data se va ajusta singură. Deci când trecem 0, atunci înseamnă "cu o zi înainte de prima zi a lunii", cu alte cuvinte: "ultima zi a lunii precedente". diff --git a/1-js/05-data-types/11-date/5-last-day-of-month/task.md b/1-js/05-data-types/11-date/5-last-day-of-month/task.md index 10dfb7a7a..7989d311d 100644 --- a/1-js/05-data-types/11-date/5-last-day-of-month/task.md +++ b/1-js/05-data-types/11-date/5-last-day-of-month/task.md @@ -2,13 +2,13 @@ importance: 5 --- -# Last day of month? +# Ultima zi a lunii? -Write a function `getLastDayOfMonth(year, month)` that returns the last day of month. Sometimes it is 30th, 31st or even 28/29th for Feb. +Scrieți o funcție `getLastDayOfMonth(year, month)` care să returneze ultima zi a lunii. Uneori este 30, 31 sau chiar 28/29 pentru Feb. -Parameters: +Parametrii: -- `year` -- four-digits year, for instance 2012. -- `month` -- month, from 0 to 11. +- `year` -- anul din patru cifre, de exemplu 2012. +- `month` -- luna, de la 0 la 11. -For instance, `getLastDayOfMonth(2012, 1) = 29` (leap year, Feb). +De exemplu, `getLastDayOfMonth(2012, 1) = 29` (an bisect, Feb). diff --git a/1-js/05-data-types/11-date/6-get-seconds-today/solution.md b/1-js/05-data-types/11-date/6-get-seconds-today/solution.md index 8f8e52b68..715956ca4 100644 --- a/1-js/05-data-types/11-date/6-get-seconds-today/solution.md +++ b/1-js/05-data-types/11-date/6-get-seconds-today/solution.md @@ -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". +Pentru a obține numărul de secunde, putem genera o dată folosind ziua și ora curentă 00:00:00, apoi să o scădem din "now". -The difference is the number of milliseconds from the beginning of the day, that we should divide by 1000 to get seconds: +Diferența este numărul de milisecunde de la începutul zilei, pe care trebuie să-l împărțim la 1000 pentru a obține secundele: ```js run function getSecondsToday() { let now = new Date(); - // create an object using the current day/month/year + // creați un obiect folosind ziua/luna/anul curent 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; // diferența de ms + return Math.round(diff / 1000); // face secunde } alert( getSecondsToday() ); ``` -An alternative solution would be to get hours/minutes/seconds and convert them to seconds: +O soluție alternativă ar fi să se obțină ore/minute/secunde și să se convertească în secunde: ```js run function getSecondsToday() { diff --git a/1-js/05-data-types/11-date/6-get-seconds-today/task.md b/1-js/05-data-types/11-date/6-get-seconds-today/task.md index 456790928..15eec85d7 100644 --- a/1-js/05-data-types/11-date/6-get-seconds-today/task.md +++ b/1-js/05-data-types/11-date/6-get-seconds-today/task.md @@ -2,14 +2,14 @@ importance: 5 --- -# How many seconds have passed today? +# Câte secunde au trecut astăzi? -Write a function `getSecondsToday()` that returns the number of seconds from the beginning of today. +Scrieți o funcție `getSecondsToday()` care returnează numărul de secunde de la începutul zilei de astăzi. -For instance, if now were `10:00 am`, and there was no daylight savings shift, then: +De exemplu, dacă acum este `10:00 am`, și nu a fost schimbată ora de vară, atunci: ```js getSecondsToday() == 36000 // (3600 * 10) ``` -The function should work in any day. That is, it should not have a hard-coded value of "today". +Funcția ar trebui să funcționeze în orice zi. Adică, nu ar trebui să aibă o valoare "astăzi" hard-coded. diff --git a/1-js/05-data-types/11-date/7-get-seconds-to-tomorrow/solution.md b/1-js/05-data-types/11-date/7-get-seconds-to-tomorrow/solution.md index c337d1199..1f0dff454 100644 --- a/1-js/05-data-types/11-date/7-get-seconds-to-tomorrow/solution.md +++ b/1-js/05-data-types/11-date/7-get-seconds-to-tomorrow/solution.md @@ -1,20 +1,20 @@ -To get the number of milliseconds till tomorrow, we can from "tomorrow 00:00:00" substract the current date. +Pentru a obține numărul de milisecunde până mâine, putem scădea din "mâine 00:00:00" data curentă. -First, we generate that "tomorrow", and then do it: +Mai întâi, generăm acel "mâine", și apoi facem acest lucru: ```js run function getSecondsToTomorrow() { let now = new Date(); - // tomorrow date + // data de mâine 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; // diferența în ms + return Math.round(diff / 1000); // convertește în secunde } ``` -Alternative solution: +Soluție alternativă: ```js run function getSecondsToTomorrow() { @@ -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. +Vă rugăm să rețineți că multe țări au ora de vară (DST), astfel încât pot exista zile cu 23 sau 25 de ore. Este posibil să dorim să tratăm aceste zile separat. diff --git a/1-js/05-data-types/11-date/7-get-seconds-to-tomorrow/task.md b/1-js/05-data-types/11-date/7-get-seconds-to-tomorrow/task.md index e05903026..329130b45 100644 --- a/1-js/05-data-types/11-date/7-get-seconds-to-tomorrow/task.md +++ b/1-js/05-data-types/11-date/7-get-seconds-to-tomorrow/task.md @@ -2,14 +2,14 @@ importance: 5 --- -# How many seconds till tomorrow? +# Câte secunde până mâine? -Create a function `getSecondsToTomorrow()` that returns the number of seconds till tomorrow. +Creați o funcție `getSecondsToTomorrow()` care returnează numărul de secunde până mâine. -For instance, if now is `23:00`, then: +De exemplu, dacă acum este `23:00`, atunci: ```js getSecondsToTomorrow() == 3600 ``` -P.S. The function should work at any day, the "today" is not hardcoded. +P.S. Funcția ar trebui să funcționeze în orice zi, "astăzi" nu este hardcoded. diff --git a/1-js/05-data-types/11-date/8-format-date-relative/_js.view/solution.js b/1-js/05-data-types/11-date/8-format-date-relative/_js.view/solution.js index 4695354a5..e00e9c8c9 100644 --- a/1-js/05-data-types/11-date/8-format-date-relative/_js.view/solution.js +++ b/1-js/05-data-types/11-date/8-format-date-relative/_js.view/solution.js @@ -1,24 +1,24 @@ function formatDate(date) { - let diff = new Date() - date; // the difference in milliseconds + let diff = new Date() - date; // diferența în milisecunde - if (diff < 1000) { // less than 1 second - return 'right now'; + if (diff < 1000) { // mai puțin de 1 secundă + return 'chiar acum'; } - let sec = Math.floor(diff / 1000); // convert diff to seconds + let sec = Math.floor(diff / 1000); // convertește diff în secunde if (sec < 60) { - return sec + ' sec. ago'; + return sec + ' sec. în urmă'; } - let min = Math.floor(diff / 60000); // convert diff to minutes + let min = Math.floor(diff / 60000); // convertește diff în minute if (min < 60) { - return min + ' min. ago'; + return min + ' min. în urmă'; } - // format the date - // add leading zeroes to single-digit day/month/hours/minutes + // formatați data + // adăugați zerouri de început unde este o singură cifră la zi/lună/ore/minute let d = date; d = [ '0' + d.getDate(), @@ -26,8 +26,8 @@ function formatDate(date) { '' + 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)); // se iau ultimele 2 cifre din fiecare component - // join the components into date + // uniți componentele în date return d.slice(0, 3).join('.') + ' ' + d.slice(3).join(':'); } diff --git a/1-js/05-data-types/11-date/8-format-date-relative/_js.view/test.js b/1-js/05-data-types/11-date/8-format-date-relative/_js.view/test.js index 9b4cb2f58..2a9074317 100644 --- a/1-js/05-data-types/11-date/8-format-date-relative/_js.view/test.js +++ b/1-js/05-data-types/11-date/8-format-date-relative/_js.view/test.js @@ -1,17 +1,17 @@ describe("formatDate", function() { - it("shows 1ms ago as \"right now\"", function() { - assert.equal(formatDate(new Date(new Date - 1)), 'right now'); + it("arată 1ms în urmă ca \"chiar acum\"", function() { + assert.equal(formatDate(new Date(new Date - 1)), 'chiar acum'); }); - it('"30 seconds ago"', function() { - assert.equal(formatDate(new Date(new Date - 30 * 1000)), "30 sec. ago"); + it('"30 secunde în urmă"', function() { + assert.equal(formatDate(new Date(new Date - 30 * 1000)), "30 sec. în urmă"); }); - it('"5 minutes ago"', function() { - assert.equal(formatDate(new Date(new Date - 5 * 60 * 1000)), "5 min. ago"); + it('"5 minute în urmă"', function() { + assert.equal(formatDate(new Date(new Date - 5 * 60 * 1000)), "5 min. în urmă"); }); - it("older dates as DD.MM.YY HH:mm", function() { + it("date mai vechi precum DD.MM.YY HH:mm", function() { assert.equal(formatDate(new Date(2014, 2, 1, 11, 22, 33)), "01.03.14 11:22"); }); diff --git a/1-js/05-data-types/11-date/8-format-date-relative/solution.md b/1-js/05-data-types/11-date/8-format-date-relative/solution.md index 372485685..1dcddf09a 100644 --- a/1-js/05-data-types/11-date/8-format-date-relative/solution.md +++ b/1-js/05-data-types/11-date/8-format-date-relative/solution.md @@ -1,26 +1,26 @@ -To get the time from `date` till now -- let's substract the dates. +Pentru a obține timpul scurs de la `date` până acum -- haideți să scădem datele. ```js run demo function formatDate(date) { - let diff = new Date() - date; // the difference in milliseconds + let diff = new Date() - date; // diferența în milisecunde - if (diff < 1000) { // less than 1 second - return 'right now'; + if (diff < 1000) { // mai puțin de 1 secundă + return 'chiar acum'; } - let sec = Math.floor(diff / 1000); // convert diff to seconds + let sec = Math.floor(diff / 1000); // convertește diff în secunde if (sec < 60) { - return sec + ' sec. ago'; + return sec + ' sec. în urmă'; } - let min = Math.floor(diff / 60000); // convert diff to minutes + let min = Math.floor(diff / 60000); // convertește diff în minute if (min < 60) { - return min + ' min. ago'; + return min + ' min. în urmă'; } - // format the date - // add leading zeroes to single-digit day/month/hours/minutes + // formatați data + // adăugați zerouri de început unde este o singură cifră la zi/lună/ore/minute let d = date; d = [ '0' + d.getDate(), @@ -28,23 +28,23 @@ function formatDate(date) { '' + 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)); // se iau ultimele 2 cifre din fiecare component - // join the components into date - return d.slice(0, 3).join('.') + ' ' + d.slice(3).join(':'); + // uniți componentele în date + return d.slice(0, 3).join('.') + ' ' + d.slice(3).join(':')); } -alert( formatDate(new Date(new Date - 1)) ); // "right now" +alert( formatDate(new Date(new Date(new Date - 1)) ); // "chiar acum" -alert( formatDate(new Date(new Date - 30 * 1000)) ); // "30 sec. ago" +alert( formatDate(new Date(new Date(new Date - 30 * 1000)) ); // "30 sec. în urmă" -alert( formatDate(new Date(new Date - 5 * 60 * 1000)) ); // "5 min. ago" +alert( formatDate(new Date(new Date(new Date - 5 * 60 * 1000)) ); // "5 min. în urmă" -// yesterday's date like 31.12.2016 20:00 -alert( formatDate(new Date(new Date - 86400 * 1000)) ); +// data de ieri precum 31.12.2016 20:00 +alert( formatDate(new Date(new Date(new Date - 86400 * 1000)) ) ); ``` -Alternative solution: +Soluție alternativă: ```js run function formatDate(date) { @@ -58,7 +58,7 @@ function formatDate(date) { let diffMin = diffSec / 60; let diffHour = diffMin / 60; - // formatting + // formatare year = year.toString().slice(-2); month = month < 10 ? '0' + month : month; dayOfMonth = dayOfMonth < 10 ? '0' + dayOfMonth : dayOfMonth; diff --git a/1-js/05-data-types/11-date/8-format-date-relative/task.md b/1-js/05-data-types/11-date/8-format-date-relative/task.md index 9651b305f..3e3553853 100644 --- a/1-js/05-data-types/11-date/8-format-date-relative/task.md +++ b/1-js/05-data-types/11-date/8-format-date-relative/task.md @@ -2,24 +2,24 @@ importance: 4 --- -# Format the relative date +# Formatați data relativă -Write a function `formatDate(date)` that should format `date` as follows: +Scrieți o funcție `formatDate(date)` care ar trebui să formateze `date` după cum urmează: -- 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`. +- Dacă a trecut mai puțin de 1 secundă din `date`, atunci `"chiar acum"`. +- În caz contrar, dacă a trecut mai puțin de 1 minut de la `date`, atunci `"n sec. ago"`. +- În caz contrar, dacă a trecut mai puțin de o oră, atunci `"m min. ago"`. +- În caz contrar, data completă în formatul `"DD.MM.YY HH:mm"`. Adică: `"zi.lună.an ore:minute"`, toate în format de 2 cifre, de exemplu `31.12.16 10:00`. -For instance: +De exemplu: ```js -alert( formatDate(new Date(new Date - 1)) ); // "right now" +alert( formatDate(new Date(new Date(new Date - 1)) ) ); // "chiar acum" -alert( formatDate(new Date(new Date - 30 * 1000)) ); // "30 sec. ago" +alert( formatDate(new Date(new Date(new Date - 30 * 1000)) ); // "acum 30 sec." -alert( formatDate(new Date(new Date - 5 * 60 * 1000)) ); // "5 min. ago" +alert( formatDate(new Date(new Date(new Date - 5 * 60 * 1000)) ); // "5 min în urmă" -// yesterday's date like 31.12.16 20:00 -alert( formatDate(new Date(new Date - 86400 * 1000)) ); +// data de ieri precum 31.12.16 20:00 +alert( formatDate(new Date(new Date(new Date - 86400 * 1000)) ) ); ``` diff --git a/1-js/05-data-types/11-date/article.md b/1-js/05-data-types/11-date/article.md index ed4e21359..4167512bf 100644 --- a/1-js/05-data-types/11-date/article.md +++ b/1-js/05-data-types/11-date/article.md @@ -1,141 +1,141 @@ -# Date and time +# Data și ora -Let's meet a new built-in object: [Date](mdn:js/Date). It stores the date, time and provides methods for date/time management. +Să cunoaștem un nou obiect încorporat: [Date](mdn:js/Date). Acesta stochează data, ora și oferă metode de administrare a datei/orei. -For instance, we can use it to store creation/modification times, to measure time, or just to print out the current date. +De exemplu, îl putem utiliza pentru a stoca timpul de creare/modificare, pentru a măsura timpul, sau pur și simplu pentru a imprima data curentă. -## Creation +## Creare -To create a new `Date` object call `new Date()` with one of the following arguments: +Pentru a crea un nou obiect `Date` apelați `new Date()` cu unul dintre următoarele argumente: `new Date()` -: Without arguments -- create a `Date` object for the current date and time: +: Fără argumente -- creează un obiect `Date` pentru data și ora curentă: ```js run let now = new Date(); - alert( now ); // shows current date/time + alert( now ); // afișează data/ora curentă ``` -`new Date(milliseconds)` -: Create a `Date` object with the time equal to number of milliseconds (1/1000 of a second) passed after the Jan 1st of 1970 UTC+0. +`new Date(milisecunde)` +: Creează un obiect `Date` cu timpul egal cu numărul de milisecunde (1/1000 dintr-o secundă) care au trecut de la 1 ianuarie 1970 UTC+0. ```js run - // 0 means 01.01.1970 UTC+0 + // 0 înseamnă 01.01.1970 UTC+0 let Jan01_1970 = new Date(0); alert( Jan01_1970 ); - // now add 24 hours, get 02.01.1970 UTC+0 + // acum adaugă 24 de ore, get 02.01.1970 UTC+0 let Jan02_1970 = new Date(24 * 3600 * 1000); alert( Jan02_1970 ); ``` - An integer number representing the number of milliseconds that has passed since the beginning of 1970 is called a *timestamp*. + Un număr întreg care reprezintă numărul de milisecunde care au trecut de la începutul anului 1970 se numește *timestamp*. - It's a lightweight numeric representation of a date. We can always create a date from a timestamp using `new Date(timestamp)` and convert the existing `Date` object to a timestamp using the `date.getTime()` method (see below). + Este o reprezentare numerică ușoară a unei date. Putem oricând să creăm o dată dintr-un timestamp folosind `new Date(timestamp)` și să convertim obiectul `Date` existent într-un timestamp folosind metoda `date.getTime()` (a se vedea mai jos). - Dates before 01.01.1970 have negative timestamps, e.g.: + Datele anterioare 01.01.1970 au timestamp-uri negative, e.g.: ```js run // 31 Dec 1969 let Dec31_1969 = new Date(-24 * 3600 * 1000); alert( Dec31_1969 ); ``` -`new Date(datestring)` -: If there is a single argument, and it's a string, then it is parsed automatically. The algorithm is the same as `Date.parse` uses, we'll cover it later. +`new Date(șir de date)` +: Dacă există un singur argument, și acesta este un șir de caractere, atunci acesta este parsat automat. Algoritmul este același pe care îl folosește `Date.parse`, îl vom acoperi mai târziu. ```js run let date = new Date("2017-01-26"); alert(date); - // The time is not set, so it's assumed to be midnight GMT and - // is adjusted according to the timezone the code is run in - // So the result could be + // Ora nu este setată, așa că se presupune că este miezul nopții GMT și + // este ajustată în funcție de fusul orar în care este rulat codul + // Astfel rezultatul ar putea fi // Thu Jan 26 2017 11:00:00 GMT+1100 (Australian Eastern Daylight Time) - // or + // sau // Wed Jan 25 2017 16:00:00 GMT-0800 (Pacific Standard Time) ``` -`new Date(year, month, date, hours, minutes, seconds, ms)` -: Create the date with the given components in the local time zone. Only the first two arguments are obligatory. +`new Date(an, lună, date, ore, minute, secunde, ms)` +: Creează data cu componentele date în fusul orar local. Doar primele două argumente sunt obligatorii. - - The `year` must have 4 digits: `2013` is okay, `98` is not. - - The `month` count starts with `0` (Jan), up to `11` (Dec). - - The `date` parameter is actually the day of month, if absent then `1` is assumed. - - If `hours/minutes/seconds/ms` is absent, they are assumed to be equal `0`. + - Anul `year` trebuie să aibă 4 cifre. Pentru compatibilitate, sunt acceptate și 2 cifre, care sunt considerate `19xx`, e.g.`98` este același lucru cu `1998` aici, dar este întotdeauna puternic încurajată folosirea a 4 cifre. + - Numărătoarea "lunilor" începe cu `0` (Ian), până la `11` (Dec). + - Parametrul `date` este de fapt ziua lunii, dacă nu există atunci se presupune `1`. + - Dacă `ore/minute/secunde/ms` este absent, se presupune că sunt egale cu `0`. - For instance: + De exemplu: ```js new Date(2011, 0, 1, 0, 0, 0, 0); // 1 Jan 2011, 00:00:00 - new Date(2011, 0, 1); // the same, hours etc are 0 by default + new Date(2011, 0, 1); // la fel, orele etc sunt 0 în mod implicit ``` - The maximal precision is 1 ms (1/1000 sec): + Precizia maximă este 1 ms (1/1000 sec): ```js run let date = new Date(2011, 0, 1, 2, 3, 4, 567); alert( date ); // 1.01.2011, 02:03:04.567 ``` -## Access date components +## Accesarea componentelor de date -There are methods to access the year, month and so on from the `Date` object: +Există metode pentru a accesa anul, luna și așa mai departe din obiectul `Date`: [getFullYear()](mdn:js/Date/getFullYear) -: Get the year (4 digits) +: Obține anul (4 cifre) [getMonth()](mdn:js/Date/getMonth) -: Get the month, **from 0 to 11**. +: Obține luna, **de la 0 la 11**. [getDate()](mdn:js/Date/getDate) -: Get the day of month, from 1 to 31, the name of the method does look a little bit strange. +: Obține ziua lunii, de la 1 la 31, numele metodei pare puțin ciudat. [getHours()](mdn:js/Date/getHours), [getMinutes()](mdn:js/Date/getMinutes), [getSeconds()](mdn:js/Date/getSeconds), [getMilliseconds()](mdn:js/Date/getMilliseconds) -: Get the corresponding time components. +: Obține componentele de timp corespunzătoare. -```warn header="Not `getYear()`, but `getFullYear()`" -Many JavaScript engines implement a non-standard method `getYear()`. This method is deprecated. It returns 2-digit year sometimes. Please never use it. There is `getFullYear()` for the year. +```warn header="Nu `getYear()`, ci `getFullYear()`" +Multe motoare JavaScript implementează o metodă non-standard `getYear()`. Această metodă este depreciată. Ea returnează uneori anul din 2 cifre. Vă rugăm să nu o folosiți niciodată. Există `getFullYear()` pentru an. ``` -Additionally, we can get a day of week: +Adițional, putem obține o zi a săptămânii: [getDay()](mdn:js/Date/getDay) -: Get the day of week, from `0` (Sunday) to `6` (Saturday). The first day is always Sunday, in some countries that's not so, but can't be changed. +: Obține ziua săptămânii, de la `0` (Duminică) la `6` (Sâmbătă). Prima zi este întotdeauna Duminică, în unele țări nu este așa, dar nu poate fi schimbată. -**All the methods above return the components relative to the local time zone.** +**Toate metodele de mai sus returnează componentele în raport cu fusul orar local.** -There are also their UTC-counterparts, that return day, month, year and so on for the time zone UTC+0: [getUTCFullYear()](mdn:js/Date/getUTCFullYear), [getUTCMonth()](mdn:js/Date/getUTCMonth), [getUTCDay()](mdn:js/Date/getUTCDay). Just insert the `"UTC"` right after `"get"`. +Există de asemenea omologii lor UTC, care returnează ziua, luna, anul și așa mai departe pentru fusul orar UTC+0: [getUTCFullYear()](mdn:js/Date/getUTCFullYear), [getUTCMonth()](mdn:js/Date/getUTCMonth), [getUTCDay()](mdn:js/Date/getUTCDay). Doar introduceți `"UTC"` imediat după `"get"`. -If your local time zone is shifted relative to UTC, then the code below shows different hours: +Dacă fusul orar local este decalat față de UTC, atunci codul de mai jos afișează ore diferite: ```js run -// current date +// data curentă let date = new Date(); -// the hour in your current time zone +// ora în fusul vostru orar alert( date.getHours() ); -// the hour in UTC+0 time zone (London time without daylight savings) +// ora în fusul orar UTC+0 (ora Londrei fără ora de vară) alert( date.getUTCHours() ); ``` -Besides the given methods, there are two special ones that do not have a UTC-variant: +În afară de metodele date, există două metode speciale care nu au o variantă UTC: [getTime()](mdn:js/Date/getTime) -: Returns the timestamp for the date -- a number of milliseconds passed from the January 1st of 1970 UTC+0. +: Returnează timestamp-ul pentru dată -- un număr de milisecunde trecute de la 1 Ianuarie 1970 UTC+0. [getTimezoneOffset()](mdn:js/Date/getTimezoneOffset) -: Returns the difference between UTC and the local time zone, in minutes: +: Returnează diferența dintre UTC și fusul orar local, în minute: ```js run - // if you are in timezone UTC-1, outputs 60 - // if you are in timezone UTC+3, outputs -180 + // dacă vă aflați în fusul orar UTC-1, produce 60 + // dacă vă aflați în fusul orar UTC+3, produce -180 alert( new Date().getTimezoneOffset() ); ``` -## Setting date components +## Stabilirea componentelor datei -The following methods allow to set date/time components: +Următoarele metode permit stabilirea componentelor de dată/ora: - [`setFullYear(year, [month], [date])`](mdn:js/Date/setFullYear) - [`setMonth(month, [date])`](mdn:js/Date/setMonth) @@ -144,38 +144,38 @@ The following methods allow to set date/time components: - [`setMinutes(min, [sec], [ms])`](mdn:js/Date/setMinutes) - [`setSeconds(sec, [ms])`](mdn:js/Date/setSeconds) - [`setMilliseconds(ms)`](mdn:js/Date/setMilliseconds) -- [`setTime(milliseconds)`](mdn:js/Date/setTime) (sets the whole date by milliseconds since 01.01.1970 UTC) +- [`setTime(milliseconds)`](mdn:js/Date/setTime) (setează întreaga dată cu milisecunde începând cu 01.01.1970 UTC) -Every one of them except `setTime()` has a UTC-variant, for instance: `setUTCHours()`. +Fiecare dintre ele cu excepția `setTime()` au o variantă UTC, de exemplu: `setUTCHours()`. -As we can see, some methods can set multiple components at once, for example `setHours`. The components that are not mentioned are not modified. +După cum putem vedea, unele metode pot seta mai multe componente deodată, de exemplu `setHours`. Componentele care nu sunt menționate nu sunt modificate. -For instance: +De exemplu: ```js run let today = new Date(); today.setHours(0); -alert(today); // still today, but the hour is changed to 0 +alert(today); // tot astăzi, dar ora este schimbată la 0 today.setHours(0, 0, 0, 0); -alert(today); // still today, now 00:00:00 sharp. +alert(today); // tot astăzi, la ora 00:00:00 fix. ``` -## Autocorrection +## Autocorecție -The *autocorrection* is a very handy feature of `Date` objects. We can set out-of-range values, and it will auto-adjust itself. +*Autocorecția* este o caracteristică foarte utilă a obiectelor `Date`. Putem seta valori în afara intervalelor, iar aceasta se va auto-ajusta. -For instance: +De exemplu: ```js run let date = new Date(2013, 0, *!*32*/!*); // 32 Jan 2013 ?!? -alert(date); // ...is 1st Feb 2013! +alert(date); // ...este 1 Feb 2013! ``` -Out-of-range date components are distributed automatically. +Componentele de date în afara intervalului sunt distribuite automat. -Let's say we need to increase the date "28 Feb 2016" by 2 days. It may be "2 Mar" or "1 Mar" in case of a leap-year. We don't need to think about it. Just add 2 days. The `Date` object will do the rest: +Să spunem că trebuie să mărim data "28 Feb 2016" cu 2 zile. Aceasta poate fi "2 Mar" sau "1 Mar" în cazul unui an bisect. Nu trebuie să ne gândim la asta. Doar adăugăm 2 zile. Obiectul `Date` se va ocupa de restul: ```js run let date = new Date(2016, 1, 28); @@ -186,109 +186,109 @@ date.setDate(date.getDate() + 2); alert( date ); // 1 Mar 2016 ``` -That feature is often used to get the date after the given period of time. For instance, let's get the date for "70 seconds after now": +Acea caracteristică este adesea utilizată pentru a obține data după o perioadă de timp dată. De exemplu, să obținem data pentru "70 de secunde de acum încolo": ```js run let date = new Date(); date.setSeconds(date.getSeconds() + 70); -alert( date ); // shows the correct date +alert( date ); // afișează data corectă ``` -We can also set zero or even negative values. For example: +De asemenea putem seta valori zero sau chiar negative. De exemplu: ```js run let date = new Date(2016, 0, 2); // 2 Jan 2016 -date.setDate(1); // set day 1 of month +date.setDate(1); // setează ziua 1 a lunii alert( date ); -date.setDate(0); // min day is 1, so the last day of the previous month is assumed +date.setDate(0); // ziua minimă este 1, deci se presupune ultima zi a lunii precedente alert( date ); // 31 Dec 2015 ``` ## Date to number, date diff -When a `Date` object is converted to number, it becomes the timestamp same as `date.getTime()`: +Atunci când un obiect `Date` este convertit în număr, acesta devine timestamp-ul la fel ca `date.getTime()`: ```js run let date = new Date(); -alert(+date); // the number of milliseconds, same as date.getTime() +alert(+date); // numărul de milisecunde, la fel ca date.getTime() ``` -The important side effect: dates can be subtracted, the result is their difference in ms. +Efect secundar important: datele se pot scădea, rezultatul fiind diferența lor în ms. -That can be used for time measurements: +Acesta poate fi folosit pentru măsurători de timp: ```js run -let start = new Date(); // start measuring time +let start = new Date(); // începe măsurarea timpului -// do the job +// face treaba for (let i = 0; i < 100000; i++) { let doSomething = i * i * i; } -let end = new Date(); // end measuring time +let end = new Date(); // încheie măsurarea timpului -alert( `The loop took ${end - start} ms` ); +alert( `Bucla a durat ${end - start} ms` ); ``` ## Date.now() -If we only want to measure time, we don't need the `Date` object. +Dacă dorim doar să măsurăm timpul, nu avem nevoie de obiectul `Date`. -There's a special method `Date.now()` that returns the current timestamp. +Există o metodă specială `Date.now()` care returnează timestamp-ul curent. -It is semantically equivalent to `new Date().getTime()`, but it doesn't create an intermediate `Date` object. So it's faster and doesn't put pressure on garbage collection. +Aceasta este semantic echivalentă cu `new Date().getTime()`, dar nu creează un obiect `Date` intermediar. Astfel este mai rapidă și nu pune presiune pe garbage collection. -It is used mostly for convenience or when performance matters, like in games in JavaScript or other specialized applications. +Este folosit în mare parte pentru conveniență sau când performanța contează, cum ar fi în jocuri în JavaScript sau alte aplicații specializate. -So this is probably better: +Așa că este probabil mai bine așa: ```js run *!* -let start = Date.now(); // milliseconds count from 1 Jan 1970 +let start = Date.now(); // milisecundele se numără de la 1 Ian 1970 */!* -// do the job +// face treaba for (let i = 0; i < 100000; i++) { let doSomething = i * i * i; } *!* -let end = Date.now(); // done +let end = Date.now(); // gata */!* -alert( `The loop took ${end - start} ms` ); // subtract numbers, not dates +alert( `Bucla a durat ${end - start} ms` ); // scade numere, nu date ``` ## Benchmarking -If we want a reliable benchmark of CPU-hungry function, we should be careful. +Dacă dorim un benchmark fiabil al unei funcții care consumă mult CPU, trebuie să fim atenți. -For instance, let's measure two functions that calculate the difference between two dates: which one is faster? +De exemplu, să măsurăm două funcții care calculează diferența dintre două date: care dintre ele este mai rapidă? -Such performance measurements are often called "benchmarks". +Astfel de măsurători de performanță sunt adesea numite "benchmarks". ```js -// we have date1 and date2, which function faster returns their difference in ms? +// avem date1 și date2, care funcție returnează mai repede diferența lor în ms? function diffSubtract(date1, date2) { return date2 - date1; } -// or +// sau function diffGetTime(date1, date2) { return date2.getTime() - date1.getTime(); } ``` -These two do exactly the same thing, but one of them uses an explicit `date.getTime()` to get the date in ms, and the other one relies on a date-to-number transform. Their result is always the same. +Acestea două fac exact același lucru, dar una dintre ele folosește `date.getTime()` explicit pentru a obține data în ms, iar cealaltă se bazează pe o transformare dată-la-număr. Rezultatul lor este întotdeauna același. -So, which one is faster? +Așadar, care dintre ele este mai rapidă? -The first idea may be to run them many times in a row and measure the time difference. For our case, functions are very simple, so we have to do it at least 100000 times. +Prima idee ar putea fi să le rulăm de multe ori la rând și să măsurăm diferența de timp. Pentru cazul nostru, funcțiile sunt foarte simple, așa că trebuie să o facem de cel puțin 100000 de ori. -Let's measure: +Să măsurăm: ```js run function diffSubtract(date1, date2) { @@ -312,19 +312,19 @@ alert( 'Time of diffSubtract: ' + bench(diffSubtract) + 'ms' ); alert( 'Time of diffGetTime: ' + bench(diffGetTime) + 'ms' ); ``` -Wow! Using `getTime()` is so much faster! That's because there's no type conversion, it is much easier for engines to optimize. +Wow! Utilizând `getTime()` este mult mai rapid! Asta pentru că nu există conversie de tip, este mult mai ușor de optimizat pentru motoare. -Okay, we have something. But that's not a good benchmark yet. +Bine, avem ceva. Dar acesta nu este un benchmark bun încă. -Imagine that at the time of running `bench(diffSubtract)` CPU was doing something in parallel, and it was taking resources. And by the time of running `bench(diffGetTime)` that work has finished. +Imaginați-vă că în momentul rulării `bench(diffSubtract)` CPU-ul făcea ceva în paralel, și acesta consuma resurse. Iar până în momentul în care se execută `bench(diffGetTime)` acea sarcină s-a terminat. -A pretty real scenario for a modern multi-process OS. +Un scenariu destul de real pentru un multiproces modern de OS. -As a result, the first benchmark will have less CPU resources than the second. That may lead to wrong results. +Ca urmare, primul benchmark va avea mai puține resurse CPU decât al doilea. Acest lucru poate duce la rezultate greșite. -**For more reliable benchmarking, the whole pack of benchmarks should be rerun multiple times.** +**Pentru un benchmarking mai fiabil, întregul pachet de benchmarks ar trebui rulat de mai multe ori.** -For example, like this: +De exemplu, cam așa: ```js run function diffSubtract(date1, date2) { @@ -348,86 +348,86 @@ let time1 = 0; let time2 = 0; *!* -// run bench(diffSubtract) and bench(diffGetTime) each 10 times alternating +// execută bench(diffSubtract) și bench(diffGetTime) fiecare de 10 ori alternativ for (let i = 0; i < 10; i++) { time1 += bench(diffSubtract); time2 += bench(diffGetTime); } */!* -alert( 'Total time for diffSubtract: ' + time1 ); -alert( 'Total time for diffGetTime: ' + time2 ); +alert( 'Timp total pentru diffSubtract: ' + time1 ); +alert( 'Timp total pentru diffGetTime: ' + time2 ); ``` -Modern JavaScript engines start applying advanced optimizations only to "hot code" that executes many times (no need to optimize rarely executed things). So, in the example above, first executions are not well-optimized. We may want to add a heat-up run: +Motoarele JavaScript moderne încep să aplice optimizări avansate doar pentru "codul fierbinte" care se execută de mai multe ori (nu este nevoie să optimizeze lucruri executate rar). Așadar, în exemplul de mai sus, primele execuții nu sunt bine optimizate. Am vrea să adăugăm o execuție de încălzire: ```js -// added for "heating up" prior to the main loop +// adăugată pentru "încălzire" înainte de bucla principală bench(diffSubtract); bench(diffGetTime); -// now benchmark +// acum benchmark for (let i = 0; i < 10; i++) { time1 += bench(diffSubtract); time2 += bench(diffGetTime); } ``` -```warn header="Be careful doing microbenchmarking" -Modern JavaScript engines perform many optimizations. They may tweak results of "artificial tests" compared to "normal usage", especially when we benchmark something very small, such as how an operator works, or a built-in function. So if you seriously want to understand performance, then please study how the JavaScript engine works. And then you probably won't need microbenchmarks at all. +```warn header="Aveți grijă când faceți microbenchmarking" +Motoarele JavaScript moderne efectuează multe optimizări. Acestea ar putea ajusta rezultatele "testelor artificiale" în comparație cu "utilizarea normală", în special atunci când efectuăm benchmark-uri pentru ceva foarte mic, cum ar fi modul în care funcționează un operator sau o funcție încorporată. Deci dacă vreți să înțelegeți în mod serios performanța, atunci vă rugăm să studiați modul în care funcționează motorul JavaScript. Și atunci probabil că nu veți avea nevoie deloc de microbenchmarks. -The great pack of articles about V8 can be found at . +Marele pachet de articole despre V8 poate fi găsit la . ``` -## Date.parse from a string +## Date.parse dintr-un șir -The method [Date.parse(str)](mdn:js/Date/parse) can read a date from a string. +Metoda [Date.parse(str)](mdn:js/Date/parse) poate citi o dată dintr-un șir. -The string format should be: `YYYY-MM-DDTHH:mm:ss.sssZ`, where: +Formatul șirului trebuie să fie: `YYYY-MM-DDTHH:mm:ss.sssZ`, unde: -- `YYYY-MM-DD` -- is the date: year-month-day. -- The character `"T"` is used as the delimiter. -- `HH:mm:ss.sss` -- is the time: hours, minutes, seconds and milliseconds. -- The optional `'Z'` part denotes the time zone in the format `+-hh:mm`. A single letter `Z` would mean UTC+0. +- `YYYY-MM-DD` -- este data: an-lună-zi. +- Caracterul `"T"` este utilizat ca delimitator. +- `HH:mm:ss.sss` -- este timpul: ore, minute, secunde și milisecunde. +- Partea opțională `'Z'` denotă fusul orar în formatul `+-hh:mm`. O singură literă `Z` ar însemna UTC+0. -Shorter variants are also possible, like `YYYY-MM-DD` or `YYYY-MM` or even `YYYY`. +Sunt posibile și variante mai scurte, precum `YYYY-MM-DD` sau `YYYY-MM` sau chiar `YYYY`. -The call to `Date.parse(str)` parses the string in the given format and returns the timestamp (number of milliseconds from 1 Jan 1970 UTC+0). If the format is invalid, returns `NaN`. +Apelul la `Date.parse(str)` parsează șirul în formatul dat și returnează timestamp-ul (numărul de milisecunde de la 1 ianuarie 1970 UTC+0). Dacă formatul nu este valid, se returnează `NaN`. -For instance: +De exemplu: ```js run let ms = Date.parse('2012-01-26T13:51:50.417-07:00'); -alert(ms); // 1327611110417 (timestamp) +alert(ms); // 1327611110417 (timestamp) ``` -We can instantly create a `new Date` object from the timestamp: +Putem crea instantaneu un obiect `new Date` din timestamp: ```js run let date = new Date( Date.parse('2012-01-26T13:51:50.417-07:00') ); -alert(date); +alert(date); ``` -## Summary +## Sumar -- Date and time in JavaScript are represented with the [Date](mdn:js/Date) object. We can't create "only date" or "only time": `Date` objects always carry both. -- Months are counted from zero (yes, January is a zero month). -- Days of week in `getDay()` are also counted from zero (that's Sunday). -- `Date` auto-corrects itself when out-of-range components are set. Good for adding/subtracting days/months/hours. -- Dates can be subtracted, giving their difference in milliseconds. That's because a `Date` becomes the timestamp when converted to a number. -- Use `Date.now()` to get the current timestamp fast. +- Data și ora în JavaScript sunt reprezentate cu obiectul [Date](mdn:js/Date). Nu putem crea "doar data" sau "doar ora": Obiectele `Date` le conțin întotdeauna pe amândouă. +- Lunile sunt numărate de la zero (da, Ianuarie este o lună zero). +- Zilele săptămânii în `getDay()` sunt de asemenea numărate de la zero (asta-i Duminică). +- `Date` se autocorectează atunci când sunt setate componente în afara intervalului. Bună pentru adăugarea/subtragerea zilelor/lunilor/orelor. +- Datele pot fi sustrase, dând diferența lor în milisecunde. Asta pentru că un `Date` devine timestamp-ul atunci când este convertit într-un număr. +- Folosiți `Date.now()` pentru a obține rapid timestamp-ul curent. -Note that unlike many other systems, timestamps in JavaScript are in milliseconds, not in seconds. +Rețineți că spre deosebire de multe alte sisteme, timestamp-urile din JavaScript sunt în milisecunde, nu în secunde. -Sometimes we need more precise time measurements. JavaScript itself does not have a way to measure time in microseconds (1 millionth of a second), but most environments provide it. For instance, browser has [performance.now()](mdn:api/Performance/now) that gives the number of milliseconds from the start of page loading with microsecond precision (3 digits after the point): +Uneori avem nevoie de măsurători de timp mai precise. JavaScript în sine nu are o modalitate de a măsura timpul în microsecunde (1 milionime dintr-o secundă), dar majoritatea mediilor o oferă. De exemplu, browserul are [performance.now()](mdn:api/Performance/now) care oferă numărul de milisecunde de la începutul încărcării paginii cu precizie de microsecunde (3 cifre după punct): ```js run -alert(`Loading started ${performance.now()}ms ago`); -// Something like: "Loading started 34731.26000000001ms ago" -// .26 is microseconds (260 microseconds) -// more than 3 digits after the decimal point are precision errors, only the first 3 are correct +alert(`Încărcarea a început cu ${performance.now()}ms în urmă`); +// Ceva precum: "Încărcarea a început acum 34731.26000000001ms" +// .26 reprezintă microsecunde (260 microsecunde) +// mai mult de 3 cifre după virgulă sunt erori de precizie, doar primele 3 sunt corecte ``` -Node.js has `microtime` module and other ways. Technically, almost any device and environment allows to get more precision, it's just not in `Date`. +Node.js are modulul `microtime` și alte modalități. Tehnic, aproape orice dispozitiv și mediu permite obținerea unei precizii mai mare, doar că nu este în `Date`.