Duffer Derek

Current Path : /var/www/api-mk-planner.bitkit.dk/httpdocs/Backend/src/services/
Upload File :
Current File : /var/www/api-mk-planner.bitkit.dk/httpdocs/Backend/src/services/commonFunctions.js

import fs from "fs";
import Log from "../configs/logger.js";

export const dateTimeFormat = (date, formate = "YYYY-MM-DD") => {
  let originalDate = new Date(date);
  let formattedDate = "";
  if (formate === "YYYY-MM-DD") {
    formattedDate = `${originalDate.getFullYear()}-${(
      originalDate.getMonth() + 1
    )
      .toString()
      .padStart(2, "0")}-${originalDate.getDate().toString().padStart(2, "0")}`;
  } else if (formate === "day_month_year") {
    const options = {
      weekday: "short",
      year: "numeric",
      month: "short",
      day: "2-digit",
    };
    formattedDate = originalDate
      .toLocaleDateString("en-US", options)
      .replace(/,/g, "");
  }
  return formattedDate;
};

export const addTimeStrings = (timeStrings) => {
  // Parse hours and minutes from each time string
  const totalMinutes = timeStrings.reduce((total, timeString) => {
    const [hours, minutes] = timeString.split(":").map(Number);
    return total + hours * 60 + minutes;
  }, 0);
  // Calculate total hours and remaining minutes
  const totalHours = Math.floor(totalMinutes / 60);
  const remainingMinutes = totalMinutes % 60;
  // Format the result as "HH:mm"
  const result = `${String(totalHours).padStart(2, "0")}:${String(
    remainingMinutes
  ).padStart(2, "0")}`;
  return result;
};

export const deleteFolder = (path) => {
  try {
    fs.rmdir(path, { recursive: true, force: true }, (err) => {
      if (err) {
        throw err;
      }
    });
    Log.info("Folder deleted", path);
  } catch (error) {
    Log.error(error);
  }
};
export const calculateEndDate = (daysToAdd) => {
  const currentDate = new Date();
  const endDate = new Date(
    currentDate.getTime() + daysToAdd * 24 * 60 * 60 * 1000
  );
  return endDate;
};

export const isPastDate = (date) => {
  const currentDate = dateTimeFormat(new Date());
  const comparisonDate = dateTimeFormat(date);
  if (comparisonDate < currentDate) {
    return true;
  } else {
    return false;
  }
};

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists