import moment from "moment";
import "moment/locale/id";

export function countTime(date: Date) {
   const dateNow = new Date()

   const diffTime = Math.abs(dateNow.getTime() - date.getTime());
   let totalSeconds = Math.floor(diffTime / 1000);
   const days = Math.floor(totalSeconds / (24 * 60 * 60));
   totalSeconds -= days * (24 * 60 * 60);

   const hours = Math.floor(totalSeconds / (60 * 60));
   totalSeconds -= hours * (60 * 60);

   const minutes = Math.floor(totalSeconds / 60);
   totalSeconds -= minutes * 60;

   const seconds = totalSeconds;

   if (days > 0) {
      return String(dateNow.getFullYear()) == moment(date).format("YYYY") ? moment(date).format("DD MMM") : moment(date).format("ll")
   } else if (hours > 0) {
      return `${hours} jam`
   } else if (minutes > 0) {
      return `${minutes} mnt`
   } else {
      return `baru saja`
   }
}