DEV Community 👩‍💻👨‍💻

irshad4250
irshad4250

Posted on

Function that returns amount of time passed(months,weeks etc) since date provided

The function returns amount of minutes, hours, weeks, months, years or the day of the week whichever is closer to the date provided, i.e it will return 1 Month instead of 4 weeks, 1 year instead of 365 days

Function input is date(YYYY-MM-DD) and Time(HH:MM).
Function gets current date(no need to input)

Function set Tag to plural form
Ex: 1 Month, 10 Months

Date Today: 2021-12-18 @ 14:00
Examples:
calculateAmountOfTime('2021-02-11','07:00')
output: 10 Months

Now let's take a date which is 5 days from the current date
calculateAmountOfTime('2021-12-13','07:00')
output: Monday

calculateAmountOfTime('2021-12-18','11:00')
output : 3 Hours

Here's the code:

function calculateAmountOfTime(date, time) {
  let dateArr = date.split("-")
  let timeArr = time.split(":")
  let days = [
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
  ]

  let dateProvided = new Date(
    dateArr[0],
    dateArr[1] - 1,
    dateArr[2],
    timeArr[0],
    timeArr[1]
  )
  let dateNow = new Date()

  let dateDifference = dateNow - dateProvided

  let dateDifferenceInMinutes = dateDifference / (1000 * 60)
  let dateDifferenceInHours = dateDifference / (1000 * 60 * 60)
  let dateDifferenceInDays = dateDifference / (1000 * 60 * 60 * 24)
  let dateDifferenceInWeeks = dateDifference / (1000 * 60 * 60 * 24 * 7)
  let dateDifferenceInMonths = dateDifference / (1000 * 60 * 60 * 24 * 30)
  let dateDifferenceInYears = dateDifference / (1000 * 60 * 60 * 24 * 365)

  dateDifferenceInHours = parseInt(dateDifferenceInHours)
  dateDifferenceInMinutes = parseInt(dateDifferenceInMinutes)
  dateDifferenceInDays = parseInt(dateDifferenceInDays)
  dateDifferenceInWeeks = parseInt(dateDifferenceInWeeks)
  dateDifferenceInMonths = parseInt(dateDifferenceInMonths)
  dateDifferenceInYears = parseInt(dateDifferenceInYears)

  if (dateDifferenceInYears > 0) {
    let tag = " Year"
    dateDifferenceInYears > 1 ? (tag = tag + "s") : (tag = tag)
    return dateDifferenceInYears + tag
  } else if (dateDifferenceInMonths > 0) {
    let tag = " Month"
    dateDifferenceInMonths > 1 ? (tag = tag + "s") : (tag = tag)
    return dateDifferenceInMonths + tag
  } else if (dateDifferenceInWeeks > 0) {
    let tag = " Week"
    dateDifferenceInWeeks > 1 ? (tag = tag + "s") : (tag = tag)
    return dateDifferenceInWeeks + tag
  } else if (dateDifferenceInDays > 0) {
    let tag = " Day"
    dateDifferenceInDays > 1 ? (tag = tag + "s") : (tag = tag)
    return days[dateProvided.getDay()]
  } else if (dateDifferenceInHours > 0) {
    let tag = " Hour"
    dateDifferenceInHours > 1 ? (tag = tag + "s") : (tag = tag)
    return dateDifferenceInHours + tag
  } else if (dateDifferenceInMinutes > 0) {
    let tag = " Minute"
    dateDifferenceInMinutes > 1 ? (tag = tag + "s") : (tag = tag)
    return dateDifferenceInMinutes + tag
  } else {
    return " 1 second"
  }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

12 Rarely Used Javascript APIs You Need

Practical examples of some unique Javascript APIs that beautifully demonstrate a practical use-case.