|
| 1 | +/* |
| 2 | + Dates... |
| 3 | +*/ |
| 4 | + |
| 5 | +console.log('Dates...') |
| 6 | + |
| 7 | +const actualDate: Date = new Date() |
| 8 | +const bornDate: Date = new Date(2002, 1, 20) |
| 9 | + |
| 10 | +console.log(`\nToday is: ${actualDate}`) |
| 11 | +console.log(`\nLucas Hoz born date: ${bornDate}`) |
| 12 | + |
| 13 | +const millisecondsPerDay: number = 86_400_000 |
| 14 | +const millisecondsBetweenDates = actualDate.getTime() - bornDate.getTime() |
| 15 | +const daysBetweenDates = Math.round( |
| 16 | + millisecondsBetweenDates / millisecondsPerDay |
| 17 | +) |
| 18 | + |
| 19 | +console.log( |
| 20 | + `\nNumber of days between today and Lucas Hoz born date: ${daysBetweenDates} days` |
| 21 | +) |
| 22 | + |
| 23 | +console.log( |
| 24 | + '\n# ---------------------------------------------------------------------------------- #\n' |
| 25 | +) |
| 26 | + |
| 27 | +/* |
| 28 | + Additional challenge... |
| 29 | +*/ |
| 30 | + |
| 31 | +console.log('Additional challenge...') |
| 32 | + |
| 33 | +function dayOfYear(date: Date): number { |
| 34 | + const millisecondsPerDay: number = 86_400_000 |
| 35 | + |
| 36 | + const baseDate = new Date(date.getFullYear(), 0, 0) |
| 37 | + const dayOfTheYear = Math.floor( |
| 38 | + (date.getTime() - baseDate.getTime()) / millisecondsPerDay |
| 39 | + ) |
| 40 | + |
| 41 | + return dayOfTheYear |
| 42 | +} |
| 43 | + |
| 44 | +const bornDateObj = { |
| 45 | + day: bornDate.getDate(), |
| 46 | + month: bornDate.getMonth() + 1, |
| 47 | + year: bornDate.getFullYear(), |
| 48 | + hours: bornDate.getHours(), |
| 49 | + minutes: bornDate.getMinutes(), |
| 50 | + seconds: bornDate.getSeconds(), |
| 51 | + dayOfTheYear: dayOfYear(bornDate), |
| 52 | + dayOfTheWeek: bornDate.getDay(), |
| 53 | + monthName: bornDate.toLocaleString('en', { month: 'long' }), |
| 54 | +} |
| 55 | + |
| 56 | +console.log( |
| 57 | + `\nDay, month, and year: ${bornDateObj.day}, ${bornDateObj.month}, and ${bornDateObj.year}` |
| 58 | +) |
| 59 | + |
| 60 | +console.log( |
| 61 | + `\nHours, minutes, and seconds: ${bornDateObj.hours} hours, ${bornDateObj.minutes} minutes, and ${bornDateObj.seconds} seconds` |
| 62 | +) |
| 63 | + |
| 64 | +console.log(`\nDay of the year: ${bornDateObj.dayOfTheYear}`) |
| 65 | + |
| 66 | +console.log(`\nName of the month: ${bornDateObj.monthName}`) |
0 commit comments