All files / utils dateUtils.js

100% Statements 56/56
100% Branches 28/28
100% Functions 8/8
100% Lines 56/56

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 12220x 303x                     20x 145x 145x 8x   137x 137x     20x 104x 104x 6x   98x 98x 104x 104x     20x 41x 41x 2x   39x     20x 149x 149x                 20x 261x 261x 261x 261x     20x 20x 20x 20x     153x 2x     151x 151x 1x     150x                     584x 1x     583x 583x 583x   583x 1x     582x 19x 19x     563x 2x 2x     561x 2x 2x     559x                    
const padWithZero = (n) => {
  return n < 10 ? "0" + n : n;
};
 
// Several backend timestamps (e.g. FarmerActivity.timestamp, Profit.timestamp)
// are naive "YYYY-MM-DDTHH:mm:ss" LocalDateTimes that are already in Pacific
// time (see FarmerActivityService/MilkTheCowsJob). Extract their digits
// directly rather than going through `new Date(...)`: a date-time string
// with no timezone offset is parsed by JS using the *viewing browser's*
// local timezone, which would silently show the wrong time (or even the
// wrong calendar day) for anyone not physically in the Pacific timezone.
// See issues #291 and #318.
const parsePacificDateTime = (dateTimeString) => {
  const match = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})/.exec(dateTimeString);
  if (!match) {
    return null;
  }
  const [, year, month, day, hour24Str, minute] = match;
  return { year, month, day, hour24: Number(hour24Str), minute };
};
 
const formatPacificTimestamp = (dateTimeString) => {
  const parts = parsePacificDateTime(dateTimeString);
  if (!parts) {
    return "";
  }
  const { year, month, day, hour24, minute } = parts;
  const period = hour24 >= 12 ? "PM" : "AM";
  const hour12 = ((hour24 + 11) % 12) + 1;
  return `${month}/${day}/${year}, ${hour12}:${minute} ${period}`;
};
 
const formatPacificDate = (dateTimeString) => {
  const parts = parsePacificDateTime(dateTimeString);
  if (!parts) {
    return "";
  }
  return `${parts.year}-${parts.month}-${parts.day}`;
};
 
const timestampToDate = (timestamp) => {
  var date = new Date(timestamp);
  return (
    date.getFullYear() +
    "-" +
    padWithZero(date.getMonth() + 1) +
    "-" +
    padWithZero(date.getDate())
  );
};
 
const daysSinceTimestamp = (date) => {
  var today = new Date();
  var startingDate = new Date(date);
  var timeDiff = Math.abs(today.getTime() - startingDate.getTime());
  return Math.ceil(timeDiff / (1000 * 3600 * 24));
};
 
const minutesInSeconds = 60;
const hourInSeconds = 60 * minutesInSeconds;
const dayInSeconds = 24 * hourInSeconds;
const weekInSeconds = 7 * dayInSeconds;
 
export function formatDateTime(dateTimeString) {
  if (!dateTimeString) {
    return "";
  }
 
  const date = new Date(dateTimeString);
  if (Number.isNaN(date.getTime())) {
    return "";
  }
 
  return new Intl.DateTimeFormat("en-US", {
    year: "numeric",
    month: "2-digit",
    day: "2-digit",
    hour: "numeric",
    minute: "2-digit",
    hour12: true,
  }).format(date);
}
 
export function formatTime(timeString) {
  if (!timeString) {
    return "";
  }
 
  const now = new Date();
  const dateFromEpoch = new Date(timeString);
  const secondsPast = Math.floor((now - dateFromEpoch) / 1000);
 
  if (secondsPast < minutesInSeconds * 2) {
    return "Online now";
  }
 
  if (secondsPast < hourInSeconds) {
    const minutes = Math.floor(secondsPast / 60);
    return `${minutes} minutes ago`;
  }
 
  if (secondsPast < dayInSeconds) {
    const hours = Math.floor(secondsPast / 3600);
    return `${hours} hour${hours > 1 ? "s" : ""} ago`;
  }
 
  if (secondsPast < weekInSeconds) {
    const days = Math.floor(secondsPast / 86400);
    return `${days} day${days > 1 ? "s" : ""} ago`;
  }
 
  return dateFromEpoch.toLocaleDateString();
}
 
export {
  timestampToDate,
  padWithZero,
  daysSinceTimestamp,
  formatPacificTimestamp,
  formatPacificDate,
};