date-fns#differenceInMinutes TypeScript Examples

The following examples show how to use date-fns#differenceInMinutes. 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: dates.ts    From Full-Stack-React-TypeScript-and-Node with MIT License 6 votes vote down vote up
getTimePastIfLessThanDay = (compTime: Date | null): string => {
  if (!compTime) return "";

  const now = new Date();
  const diffInMinutes = differenceInMinutes(now, compTime);
  console.log("diff", diffInMinutes);
  if (diffInMinutes > 60) {
    if (diffInMinutes > 24 * 60) {
      return format(compTime, StandardDateTimeFormat);
    }
    return Math.round(diffInMinutes / 60) + "h ago";
  }
  return Math.round(diffInMinutes) + "m ago";
}
Example #2
Source File: sleep-data-helper.ts    From nyxo-app with GNU General Public License v3.0 6 votes vote down vote up
export function getAverageOfTimes(days: Day[]): number {
  let averageBedTime = 0
  let divideBy = 0
  days.forEach((day) => {
    const dayStarted = new Date(day.date) // Beginning of the day
    if (day.bedStart) {
      const bedTimeStart = new Date(day.bedStart)

      const totalDifference = differenceInMinutes(bedTimeStart, dayStarted)

      // Add difference to the average time
      averageBedTime += totalDifference
      // increment divider
      divideBy += 1
    }
  })

  if (divideBy !== 0) {
    averageBedTime /= divideBy
  }

  // Remove the extra 24 hours
  if (averageBedTime > 1440) {
    averageBedTime = -1440
  }
  return averageBedTime
}
Example #3
Source File: sleep.ts    From nyxo-app with GNU General Public License v3.0 6 votes vote down vote up
export function getDurationString(start: string, end: string): string {
  const startDate = new Date(start)
  const endDate = new Date(end)

  let minutes = differenceInMinutes(startDate, endDate)
  const hours = Math.floor(minutes / 60)
  minutes -= hours * 60

  return `${hours} h ${minutes} m`
}
Example #4
Source File: tempo.ts    From tempomat with MIT License 6 votes vote down vote up
function createWorklogInputs(tracker: Tracker, remainingEstimate?: string): [Interval, AddWorklogInput][] {
    return tracker.intervals.map(interval => {
        return [
            interval,
            {
                issueKeyOrAlias: tracker.issueKey,
                description: tracker.description,
                when: fnsLightFormat(interval.start, 'yyyy-MM-dd'),
                startTime: fnsLightFormat(interval.start, 'HH:mm'),
                durationOrInterval: `${differenceInMinutes(interval.end, interval.start)}m`,
                remainingEstimate: remainingEstimate
            }
        ]
    })
}
Example #5
Source File: trackersTable.ts    From tempomat with MIT License 6 votes vote down vote up
function toRows(intervals: Interval[]) {
    if (intervals.length === 0) {
        return [[{ colSpan: 3, content: chalk.redBright('No intervals'), hAlign: 'center' }]] as Cell[][]
    }
    return intervals.map(interval => {
        const duration = differenceInMinutes(interval.end, interval.start)
        const startedAt = fnsLightFormat(interval.start, 'yyyy-MM-dd HH:mm:ss')
        const stoppedAt = fnsLightFormat(interval.end, 'yyyy-MM-dd HH:mm:ss')
        const row = {
            startedAt: { colSpan: 1, content: startedAt, hAlign: 'right' },
            stoppedAt: { colSpan: 1, content: stoppedAt, hAlign: 'right' },
            duration: { colSpan: 1, content: `${duration}m`, hAlign: 'right' }
        }
        return Object.values(row)
    }) as Cell[][]
}
Example #6
Source File: trackersTable.ts    From tempomat with MIT License 6 votes vote down vote up
function generateSummaryFooter(tracker: Tracker, now: Date) {
    const intervalsDuration = tracker.intervals
        .map(interval => differenceInMinutes(interval.end, interval.start))
        .reduce((previous, current) => previous + current, 0)
    let activeDuration = 0
    if (tracker.isActive) {
        activeDuration = differenceInMinutes(now.getTime(), tracker.activeTimestamp)
    }
    const totalDuration = intervalsDuration + activeDuration
    return [
        { colSpan: 2, content: 'Total duration:', hAlign: 'right' },
        { colSpan: 1, content: `${totalDuration}m`, hAlign: 'right' }
    ].map(row => row as Cell)
}
Example #7
Source File: sleep-data-helper.ts    From nyxo-app with GNU General Public License v3.0 5 votes vote down vote up
export function calculateBedtimeWindow(
  lastSevenDays: Day[]
): {
  goToSleepWindowStart: string
  goToSleepWindowCenter: string
  goToSleepWindowEnd: string
} {
  let averageBedTime = 0
  let divideBy = 0
  lastSevenDays.forEach((day) => {
    const dayStarted = new Date(day.date) // Beginning of the day
    if (day.bedStart) {
      const bedTimeStart = new Date(day.bedStart)

      const totalDifference = differenceInMinutes(bedTimeStart, dayStarted)
      // Add difference to the average time
      averageBedTime += totalDifference
      // increment divider
      divideBy += 1
    }
  })

  if (divideBy !== 0) {
    averageBedTime /= divideBy
  }

  // Remove the extra 24 hours
  if (averageBedTime > 1440) {
    averageBedTime = -1440
  }

  const bedTimeWindowCenter = roundToNearestMinutes(
    setMinutes(startOfDay(new Date()), averageBedTime),
    {
      nearestTo: 15
    }
  ).toISOString()

  const bedTimeWindowStart = subMinutes(
    new Date(bedTimeWindowCenter),
    45
  ).toISOString()

  const bedTimeWindowEnd = addMinutes(
    new Date(bedTimeWindowCenter),
    45
  ).toISOString()

  const insights = {
    goToSleepWindowStart: bedTimeWindowStart,
    goToSleepWindowCenter: bedTimeWindowCenter,
    goToSleepWindowEnd: bedTimeWindowEnd
  }

  return insights
}
Example #8
Source File: sleep.ts    From nyxo-app with GNU General Public License v3.0 5 votes vote down vote up
export function getNightDuration(start: string, end: string): number {
  return differenceInMinutes(new Date(start), new Date(end))
}
Example #9
Source File: networkRevenueProvider.ts    From akashlytics with GNU General Public License v3.0 4 votes vote down vote up
getWeb3IndexRevenue = async (debug: boolean) => {
  if (!debug && cachedRevenue && cachedRevenueDate && Math.abs(differenceInMinutes(cachedRevenueDate, new Date())) < 30) {
    return cachedRevenue;
  }

  while (isCalculatingRevenue || isSyncingPrices) {
    await sleep(5000);
  }

  const dailyNetworkRevenues = await getDailyRevenue();
  // console.log(dailyNetworkRevenues[0]);
  let days = dailyNetworkRevenues.map((r) => ({
    date: r.date.getTime() / 1000,
    revenue: round(r.usd, 2),
    revenueUAkt: r.uakt,
    aktPrice: r.aktPrice,
    dateStr: r.date
  }));

  const today = getTodayUTC();
  const oneDayAgo = add(today, { days: -1 });
  const twoDaysAgo = add(today, { days: -2 });
  const oneWeekAgo = add(today, { weeks: -1 });
  const twoWeeksAgo = add(today, { weeks: -2 });
  const thirtyDaysAgo = add(today, { days: -30 });
  const sixtyDaysAgo = add(today, { days: -60 });
  const ninetyDaysAgo = add(today, { days: -90 });
  let totalRevenue: number = 0,
    oneDayAgoRevenue: number = 0,
    twoDaysAgoRevenue: number = 0,
    oneWeekAgoRevenue: number = 0,
    twoWeeksAgoRevenue: number = 0,
    thirtyDaysAgoRevenue: number = 0,
    sixtyDaysAgoRevenue: number = 0,
    ninetyDaysAgoRevenue: number = 0;
  let totalRevenueUAkt: number = 0,
    oneDayAgoRevenueUAkt: number = 0,
    twoDaysAgoRevenueUAkt: number = 0,
    oneWeekAgoRevenueUAkt: number = 0,
    twoWeeksAgoRevenueUAkt: number = 0,
    thirtyDaysAgoRevenueUAkt: number = 0,
    sixtyDaysAgoRevenueUAkt: number = 0,
    ninetyDaysAgoRevenueUAkt: number = 0;

  days.forEach((b) => {
    const date = new Date(b.date * 1000);

    if (date <= ninetyDaysAgo) {
      ninetyDaysAgoRevenue += b.revenue;
      ninetyDaysAgoRevenueUAkt += b.revenueUAkt;
    }
    if (date <= sixtyDaysAgo) {
      sixtyDaysAgoRevenue += b.revenue;
      sixtyDaysAgoRevenueUAkt += b.revenueUAkt;
    }
    if (date <= thirtyDaysAgo) {
      thirtyDaysAgoRevenue += b.revenue;
      thirtyDaysAgoRevenueUAkt += b.revenueUAkt;
    }
    if (date <= twoWeeksAgo) {
      twoWeeksAgoRevenue += b.revenue;
      twoWeeksAgoRevenueUAkt += b.revenueUAkt;
    }
    if (date <= oneWeekAgo) {
      oneWeekAgoRevenue += b.revenue;
      oneWeekAgoRevenueUAkt += b.revenueUAkt;
    }
    if (date <= twoDaysAgo) {
      twoDaysAgoRevenue += b.revenue;
      twoDaysAgoRevenueUAkt += b.revenueUAkt;
    }
    if (date <= oneDayAgo) {
      oneDayAgoRevenue += b.revenue;
      oneDayAgoRevenueUAkt += b.revenueUAkt;
    }

    totalRevenue += b.revenue;
    totalRevenueUAkt += b.revenueUAkt;
  }, 0);

  if (!debug) {
    days = days.map(({ dateStr, revenueUAkt, aktPrice, ...others }) => others) as any;
  }

  let revenueStats = {
    now: round(totalRevenue),
    oneDayAgo: round(oneDayAgoRevenue),
    twoDaysAgo: round(twoDaysAgoRevenue),
    oneWeekAgo: round(oneWeekAgoRevenue),
    twoWeeksAgo: round(twoWeeksAgoRevenue),
    thirtyDaysAgo: round(thirtyDaysAgoRevenue),
    sixtyDaysAgo: round(sixtyDaysAgoRevenue),
    ninetyDaysAgo: round(ninetyDaysAgoRevenue)
  };

  if (debug) {
    revenueStats = {
      ...revenueStats,
      nowAkt: uaktToAKT(totalRevenueUAkt, 6),
      oneDayAgoAkt: uaktToAKT(oneDayAgoRevenueUAkt, 6),
      twoDaysAgoAkt: uaktToAKT(twoDaysAgoRevenueUAkt, 6),
      oneWeekAgoAkt: uaktToAKT(oneWeekAgoRevenueUAkt, 6),
      twoWeeksAgAkt: uaktToAKT(twoWeeksAgoRevenueUAkt, 6),
      thirtyDaysAgoAkt: uaktToAKT(thirtyDaysAgoRevenueUAkt, 6),
      sixtyDaysAgoAkt: uaktToAKT(sixtyDaysAgoRevenueUAkt, 6),
      ninetyDaysAgoAkt: uaktToAKT(ninetyDaysAgoRevenueUAkt, 6)
    } as any;
  }

  const responseObj = {
    revenue: revenueStats,
    days
  };

  cachedRevenue = responseObj;
  cachedRevenueDate = new Date();

  return responseObj;
}