Ver Fonte

Merge branch 'age-fix' of Viktoriia/nomadmania-app into dev

Viktoriia há 1 ano atrás
pai
commit
14ed764ea6
1 ficheiros alterados com 14 adições e 2 exclusões
  1. 14 2
      src/utils/responsive-font.ts

+ 14 - 2
src/utils/responsive-font.ts

@@ -3,5 +3,17 @@ import { PixelRatio } from 'react-native';
 const fontScale = PixelRatio.getFontScale();
 export const getFontSize = (size: number) => size / fontScale;
 
-export const getYears = (date: string) =>
-  Math.abs(new Date(date).getFullYear() - new Date().getFullYear());
+export const getYears = (date: string): number => {
+  const birthDate = new Date(date);
+  const today = new Date();
+
+  let age = today.getFullYear() - birthDate.getFullYear();
+  const monthDifference = today.getMonth() - birthDate.getMonth();
+  const dayDifference = today.getDate() - birthDate.getDate();
+
+  if (monthDifference < 0 || (monthDifference === 0 && dayDifference < 0)) {
+      age--;
+  }
+
+  return age;
+};