date-fns#differenceInYears JavaScript Examples

The following examples show how to use date-fns#differenceInYears. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: relativeCommitTimeCalculator.js    From gitconvex with Apache License 2.0 6 votes vote down vote up
export function relativeCommitTimeCalculator(commitTime) {
  let commitRelativeTime;

  const days = differenceInCalendarDays(new Date(), new Date(commitTime));
  const hours = differenceInHours(new Date(), new Date(commitTime));
  const minutes = differenceInMinutes(new Date(), new Date(commitTime));

  if (days > 0) {
    if (days >= 30) {
      const month = differenceInMonths(new Date(), new Date(commitTime));
      commitRelativeTime =
        month === 1 ? month + " Month Ago" : month + " Months Ago";
    } else if (days >= 365) {
      const year = differenceInYears(new Date(), new Date(commitTime));
      commitRelativeTime =
        year === 1 ? year + " Year Ago" : year + " Years Ago";
    } else {
      commitRelativeTime = days === 1 ? days + " Day Ago" : days + " Days Ago";
    }
  } else if (hours > 0) {
    commitRelativeTime =
      hours === 1 ? hours + " Hour Ago" : hours + " Hours Ago";
  } else if (minutes > 0) {
    commitRelativeTime =
      minutes === 1 ? minutes + " Minute Ago" : minutes + " Minutes Ago";
  } else {
    commitRelativeTime = "recent commit";
  }

  return commitRelativeTime;
}
Example #2
Source File: Age.jsx    From awesome-react-starter with MIT License 6 votes vote down vote up
Age = () => {
  const { values, setFieldValue } = useFormikContext();
  useEffect(() => {
    if (values.dateOfBirth) {
      let age = differenceInYears(Date.now(), new Date(values.dateOfBirth));
      if (age * 1 < 0) {
        age = 0;
      }
      setFieldValue('age', age);
    }
  }, [values.dateOfBirth]);

  return (
    <Fieldset name="age" label="Age" help="Computed from your date of birth">
      <Field id="age" name="age" as={Number} readOnly />
    </Fieldset>
  );
}