All files / app/assets/javascripts/monitoring format_date.js

100% Statements 6/6
33.33% Branches 1/3
100% Functions 1/1
100% Lines 6/6

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    27x                       27x                                         27x 2173x 2173x 2173x    
import dateFormat from 'dateformat';
 
export const timezones = {
  /**
   * Renders a date with a local timezone
   */
  LOCAL: 'LOCAL',
 
  /**
   * Renders at date with UTC
   */
  UTC: 'UTC',
};
 
export const formats = {
  shortTime: 'h:MM TT',
  shortDateTime: 'm/d h:MM TT',
  default: 'dd mmm yyyy, h:MMTT (Z)',
};
 
/**
 * Formats a date for a metric dashboard or chart.
 *
 * Convenience wrapper of dateFormat with default formats
 * and settings.
 *
 * dateFormat has some limitations and we could use `toLocaleString` instead
 * See: https://gitlab.com/gitlab-org/gitlab/-/issues/219246
 *
 * @param {Date|String|Number} date
 * @param {Object} options - Formatting options
 * @param {string} options.format - Format or mask from `formats`.
 * @param {string} options.timezone - Timezone abbreviation.
 * Accepts "LOCAL" for the client local timezone.
 */
export const formatDate = (date, options = {}) => {
  const { format = formats.default, timezone = timezones.LOCAL } = options;
  const useUTC = timezone === timezones.UTC;
  return dateFormat(date, format, useUTC);
};