luxon#Info TypeScript Examples

The following examples show how to use luxon#Info. 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: luxon.ts    From dendron with GNU Affero General Public License v3.0 5 votes vote down vote up
generateConfig: GenerateConfig<DateTime> = {
  // get
  getNow: () => DateTime.local(),
  getFixedDate: (string) => DateTime.fromFormat(string, "yyyy-MM-dd"),
  getEndDate: (date) => date.endOf("month"),
  getWeekDay: (date) => date.weekday,
  getYear: (date) => date.year,
  getMonth: (date) => date.month - 1, // getMonth should return 0-11, luxon month returns 1-12
  getDate: (date) => date.day,
  getHour: (date) => date.hour,
  getMinute: (date) => date.minute,
  getSecond: (date) => date.second,

  // set
  addYear: (date, diff) => date.plus({ year: diff }),
  addMonth: (date, diff) => date.plus({ month: diff }),
  addDate: (date, diff) => date.plus({ day: diff }),
  setYear: (date, year) => (date || DateTime.local()).set({ year }),
  setMonth: (date, month) =>
    (date || DateTime.local()).set({ month: month + 1 }), // setMonth month argument is 0-11, luxon months are 1-12
  setDate: (date, day) => date.set({ day }),
  setHour: (date, hour) => date.set({ hour }),
  setMinute: (date, minute) => date.set({ minute }),
  setSecond: (date, second) => date.set({ second }),

  // Compare
  isAfter: (date1, date2) => date1 > date2,
  isValidate: (date) => date.isValid,

  locale: {
    getWeekFirstDate: (locale, date) =>
      date.setLocale(normalizeLocale(locale)).startOf("week"),
    getWeekFirstDay: (locale) =>
      DateTime.local().setLocale(normalizeLocale(locale)).startOf("week")
        .weekday,
    getWeek: (locale, date) =>
      date.setLocale(normalizeLocale(locale)).weekNumber,
    getShortWeekDays: (locale) => {
      const weekdays = Info.weekdays("short", {
        locale: normalizeLocale(locale),
      });

      // getShortWeekDays should return weekday labels starting from Sunday.
      // luxon returns them starting from Monday, so we have to shift the results.
      weekdays.unshift(weekdays.pop() as string);

      return weekdays;
    },
    getShortMonths: (locale) =>
      Info.months("short", { locale: normalizeLocale(locale) }),
    // @ts-ignore -- allow format to return `null`
    format: (locale, date, format) => {
      if (!date || !date.isValid) {
        return null;
      }

      return date
        .setLocale(normalizeLocale(locale))
        .toFormat(normalizeFormat(format));
    },
    parse: (locale, text, formats) => {
      for (let i = 0; i < formats.length; i += 1) {
        const normalizedFormat = normalizeFormat(formats[i]);

        const date = DateTime.fromFormat(text, normalizedFormat, {
          locale: normalizeLocale(locale),
        });

        if (date.isValid) {
          return date;
        }
      }

      return null;
    },
  },
}