File tree 2 files changed +47
-0
lines changed
1-js/05-data-types/10-date
7-get-seconds-to-tomorrow 2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change @@ -13,3 +13,19 @@ function getSecondsToTomorrow() {
13
13
return Math .round (diff / 1000 ); // convert to seconds
14
14
}
15
15
```
16
+
17
+ Alternative solution:
18
+
19
+ ``` js run
20
+ function getSecondsToTomorrow () {
21
+ let now = new Date ();
22
+ let hour = now .getHours ();
23
+ let minutes = now .getMinutes ();
24
+ let seconds = now .getSeconds ();
25
+ let totalSecondsToday = (hour * 60 + minutes) * 60 + seconds;
26
+ let totalSecondsInADay = 86400 ;
27
+
28
+ return totalSecondsInADay - totalSecondsToday;
29
+ }
30
+
31
+ ```
Original file line number Diff line number Diff line change @@ -43,3 +43,34 @@ alert( formatDate(new Date(new Date - 5 * 60 * 1000)) ); // "5 min. ago"
43
43
// yesterday's date like 31.12.2016, 20:00
44
44
alert ( formatDate (new Date (new Date - 86400 * 1000 )) );
45
45
```
46
+
47
+ Alternative solution:
48
+
49
+ ``` js run
50
+ function formatDate (date ) {
51
+ let dayOfMonth = date .getDate ();
52
+ let month = date .getMonth () + 1 ;
53
+ let year = date .getFullYear ();
54
+ let hour = date .getHours ();
55
+ let minutes = date .getMinutes ();
56
+ let diffMs = new Date () - date;
57
+ let diffSec = Math .round (diffMs / 1000 );
58
+ let diffMin = diffSec / 60 ;
59
+ let diffHour = diffMin / 60 ;
60
+
61
+ // formatting
62
+ year = year .toString ().slice (- 2 );
63
+ month = month < 10 ? ' 0' + month : month;
64
+ dayOfMonth = dayOfMonth < 10 ? ' 0' + dayOfMonth : dayOfMonth;
65
+
66
+ if (diffSec < 1 ) {
67
+ return ' right now' ;
68
+ } else if (diffMin < 1 ) {
69
+ return ` ${ diffSec} sec. ago`
70
+ } else if (diffHour < 1 ) {
71
+ return ` ${ diffMin} min. ago`
72
+ } else {
73
+ return ` ${ dayOfMonth} .${ month} .${ year} ${ hour} :${ minutes} `
74
+ }
75
+ }
76
+ ```
You can’t perform that action at this time.
0 commit comments