JSFiddle - React, Tailwind, and code Playground

by cubicalmonkey

JavaScript

var MYDATE = new Date("2023/03/09 08:30:19").toISOString();
var CURDATE = Date();





const compareDates = (d1, d2) => {
  let date1 = new Date(d1).getTime();
  let date2 = new Date(d2).getTime();

let diff = date1 - date2;
console.log( convertMsToTime( diff ) )

};



function convertMsToTime(ms) {
  const seconds = Math.floor(ms / 1000);
  const minutes = Math.floor(seconds / 60);
  const hours = Math.floor(minutes / 60);
  const days = Math.floor(hours / 24);

	let DIFF;
	if ( days > 0){ DIFF =+ days+" day/s " }
  if ( hours > 0){ DIFF =+ hours+ " Hour/s " }
  if ( minutes > 0){ DIFF =+ minutes+ " Minute/s " }
  //if ( seconds > 0){ DIFF =+ seconds+  }
  
  return DIFF;
}


function getTimeDifference() {
  // Create a new Date object for the current date and time
  const now = new Date();

  // Convert the current date and time to UTC
  const nowUtc = new Date(now.getTime() + now.getTimezoneOffset() * 60000);

  // Create a new Date object for the specified date and time
  const specified = new Date('2023-03-09T13:38:16');

  // Calculate the difference between the two dates in milliseconds
  const differenceMs = Math.abs(nowUtc - specified);

  // Calculate the difference in days, hours, and minutes
  const days = Math.floor(differenceMs / (1000 * 60 * 60 * 24));
  const hours = Math.floor((differenceMs % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  const minutes = Math.floor((differenceMs % (1000 * 60 * 60)) / (1000 * 60));

  // Return the difference as an object
  return { days, hours, minutes };
}

// Call the function and log the result to the console
console.log(getTimeDifference());

//console.log( compareDates(CURDATE,  MYDATE) )