Created
May 31, 2023 18:45
-
-
Save alvaaz/5fa283de2adc620b6124abc7d3999c9a to your computer and use it in GitHub Desktop.
Aberración para relativeDate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function calcDate(date1: any, date2: any) { | |
| //new date instance | |
| const dt_date1 = new Date(date1); | |
| const dt_date2 = new Date(date2); | |
| //Get the Timestamp | |
| const date1_time_stamp = dt_date1.getTime(); | |
| const date2_time_stamp = dt_date2.getTime(); | |
| let calc; | |
| //Check which timestamp is greater | |
| if (date1_time_stamp > date2_time_stamp) { | |
| calc = new Date(date1_time_stamp - date2_time_stamp); | |
| } else { | |
| calc = new Date(date2_time_stamp - date1_time_stamp); | |
| } | |
| //Retrieve the date, month and year | |
| const calcFormatTmp = calc.getDate() + '-' + (calc.getMonth() + 1) + '-' + calc.getFullYear(); | |
| //Convert to an array and store | |
| const calcFormat = calcFormatTmp.split("-"); | |
| //Subtract each member of our array from the default date | |
| // @ts-ignore | |
| const days_passed = Number(Math.abs(calcFormat[0]) - 1); | |
| // @ts-ignore | |
| const months_passed = Number(Math.abs(calcFormat[1]) - 1); | |
| // @ts-ignore | |
| const years_passed = Number(Math.abs(calcFormat[2]) - 1970); | |
| //Set up custom text | |
| const yrsTxt = ["año", "años"]; | |
| const mnthsTxt = ["mes", "meses"]; | |
| const daysTxt = ["día", "dias"]; | |
| //Convert to days and sum together | |
| const total_days = (years_passed * 365) + (months_passed * 30.417) + days_passed; | |
| const total_secs = total_days * 24 * 60 * 60; | |
| const total_mins = total_days * 24 * 60; | |
| const total_hours = total_days * 24; | |
| const total_weeks = ( total_days >= 7 ) ? total_days / 7 : 0; | |
| const result = () => { | |
| if(years_passed == 1) { | |
| return years_passed + ' ' + yrsTxt[0] + ' ' | |
| } else { | |
| if(years_passed > 1) { | |
| return years_passed + ' ' + yrsTxt[1] + ' ' | |
| } else { | |
| if(months_passed == 1) { | |
| return months_passed + ' ' + mnthsTxt[0] | |
| } else { | |
| if(months_passed > 1) { | |
| return months_passed + ' ' + mnthsTxt[1] + ' ' | |
| } else { | |
| if(days_passed == 1) { | |
| return days_passed + ' ' + daysTxt[0] | |
| } else { | |
| if(days_passed > 1) { | |
| return days_passed + ' ' + daysTxt[1] | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| return { | |
| "total_days": Math.round(total_days), | |
| "total_weeks": Math.round(total_weeks), | |
| "total_hours" : Math.round(total_hours), | |
| "total_minutes" : Math.round(total_mins), | |
| "total_seconds": Math.round(total_secs), | |
| "result": result()?.trim() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment