Skip to content

Commit

Permalink
add dateTime as date format
Browse files Browse the repository at this point in the history
  • Loading branch information
david-loe committed Aug 15, 2024
1 parent dafed39 commit 3cb6c5a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
4 changes: 2 additions & 2 deletions backend/pdf/travel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,11 @@ function drawGeneralTravelInformation(page: pdf_lib.PDFPage, travel: Travel, opt
var text =
i18n.t('labels.from', { lng: opts.language }) +
': ' +
options.formatter.date(travel.startDate) +
options.formatter.dateTime(travel.startDate) +
' ' +
i18n.t('labels.to', { lng: opts.language }) +
': ' +
options.formatter.date(travel.endDate)
options.formatter.dateTime(travel.endDate)
if (travel.professionalShare !== 1) {
text =
text + ' ' + i18n.t('labels.professionalShare', { lng: opts.language }) + ': ' + Math.round(travel.professionalShare! * 100) + '%'
Expand Down
35 changes: 33 additions & 2 deletions common/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import { baseCurrencyMoneyToMoney, isValidDate } from './scripts.js'
import { BaseCurrencyMoney, Locale, Money, baseCurrency } from './types.js'
import { baseCurrencyMoneyToMoney, isValidDate } from './scripts.js';
import { BaseCurrencyMoney, Locale, Money, baseCurrency } from './types.js';

type MoneyStringOptions = { locale?: Locale; useExchangeRate?: boolean; func?: (x: number) => number; warning?: boolean }

class Formatter {
#dateFormat!: Intl.DateTimeFormat
#simpleDateFormat!: Intl.DateTimeFormat
#dateTimeFormat!: Intl.DateTimeFormat
#simpleDateTimeFormat!: Intl.DateTimeFormat
#baseCurrencyFormat!: Intl.NumberFormat
#floatFormat!: Intl.NumberFormat
#dateFormatOptions: Intl.DateTimeFormatOptions = { timeZone: 'UTC', month: 'numeric', day: 'numeric', year: 'numeric' }
#simpleDateFormatOptions: Intl.DateTimeFormatOptions = { timeZone: 'UTC', month: 'numeric', day: 'numeric' }
#dateTimeFormatOptions: Intl.DateTimeFormatOptions = {
timeZone: 'UTC',
year: 'numeric',
month: 'numeric',
day: 'numeric',
hour: 'numeric',
minute: 'numeric'
}
#simpleDateTimeFormatOptions: Intl.DateTimeFormatOptions = {
timeZone: 'UTC',
month: 'numeric',
Expand All @@ -30,11 +39,15 @@ class Formatter {
this.locale = locale
this.#dateFormat = new Intl.DateTimeFormat(locale, this.#dateFormatOptions)
this.#simpleDateFormat = new Intl.DateTimeFormat(locale, this.#simpleDateFormatOptions)
this.#dateTimeFormat = new Intl.DateTimeFormat(locale, this.#dateTimeFormatOptions)
this.#simpleDateTimeFormat = new Intl.DateTimeFormat(locale, this.#simpleDateTimeFormatOptions)
this.#baseCurrencyFormat = new Intl.NumberFormat(locale, this.#baseCurrencyFormatOptions)
this.#floatFormat = new Intl.NumberFormat(locale, this.#floatFormatOptions)
}
}
/**
* Day + Month + Year
*/
date(date: string | number | Date, locale?: Locale) {
this.setLocale(locale)
const validDate = isValidDate(date)
Expand All @@ -44,6 +57,9 @@ class Formatter {
return ''
}
}
/**
* Day + Month
*/
simpleDate(date: string | number | Date, locale?: Locale) {
this.setLocale(locale)
const validDate = isValidDate(date)
Expand All @@ -53,6 +69,21 @@ class Formatter {
return ''
}
}
/**
* Day + Month + Year + Hour + Minute
*/
dateTime(date: string | number | Date, locale?: Locale) {
this.setLocale(locale)
const validDate = isValidDate(date)
if (validDate) {
return this.#dateTimeFormat.format(validDate)
} else {
return ''
}
}
/**
* Day + Month + Hour + Minute
*/
simpleDateTime(date: string | number | Date, locale?: Locale) {
this.setLocale(locale)
const validDate = isValidDate(date)
Expand Down

0 comments on commit 3cb6c5a

Please sign in to comment.