Skip to content

Commit

Permalink
Refactor code that is being reused.
Browse files Browse the repository at this point in the history
  • Loading branch information
joao-mbn committed Oct 5, 2024
1 parent acb4560 commit a62be00
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions app/utils/datetime.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
import { BIRTH_DATE, CAREER_START_DATE } from './constants';

export function getMyAge() {
const today = new Date();

const yearDifference = today.getFullYear() - BIRTH_DATE.getFullYear();
const monthDifference = today.getMonth() - BIRTH_DATE.getMonth();
const dayDifference = today.getDate() - BIRTH_DATE.getDate();

if (monthDifference < 0 || (monthDifference === 0 && dayDifference < 0)) {
return yearDifference - 1;
} else {
return yearDifference;
}
return getDateDifferenceInMonths(new Date(), BIRTH_DATE).years;
}

export function getMyWorkExperience() {
const today = new Date();
return getDateDifferenceInMonths(new Date(), CAREER_START_DATE);
}

let yearDifference = today.getFullYear() - CAREER_START_DATE.getFullYear();
let monthDifference = today.getMonth() - CAREER_START_DATE.getMonth();
const dayDifference = today.getDate() - CAREER_START_DATE.getDate();
function getDateDifferenceInMonths(date1: Date, date2: Date) {
let yearDifference = date1.getFullYear() - date2.getFullYear();
let monthDifference = date1.getMonth() - date2.getMonth();
const dayDifference = date1.getDate() - date2.getDate();

if (dayDifference < 0) {
monthDifference -= 1;
Expand Down

0 comments on commit a62be00

Please sign in to comment.