0

I am replacing moment by date-fns and I have this

    start: moment().startOf('year').locale(this.$i18n.locale).toDate(),
    end: moment().endOf('year').locale(this.$i18n.locale).toDate(),

my first thought was to do this, to keep using this.$i18n.locale

    start: localYearStart(this.$i18n.locale),
    end: localYearEnd(this.$i18n.locale),
export const localYearStart = (localDate) => {
    const localTimeZone = getLocalTimeZone(localDate);
    const formattedLocalDate = format(new Date(), 'PPpp', {locale:localTimeZone, weekStartsOn:monday});

    return startOfYear(new Date(formattedLocalDate))
}

but if I use the following function, I get the same result

export const firstDayOfCurrentYear = () => {
    return startOfYear(NEW_DATE);
}

My concert is. Should I use the first function or go for the second one. This is my first job as a frontend junior, as well my first and half month and my first task, so I would like to do not mess up anything. :)

Sargentogato
  • 85
  • 1
  • 2
  • 9
  • Don't pass strings to the `Date` constructor unless you know they are formatted in the correct form of ISO 8601. See [Why does Date.parse give incorrect results?](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results). Also, not sure why you wouldn't use [`startOfYear`](https://date-fns.org/v2.22.1/docs/startOfYear) from date-fns. – Heretic Monkey Jul 08 '21 at 15:46
  • I wanted to use startOfYear, but I am using the other function because on the project they were using i18n as an argument so, I want to keep usign the same and to do it I need to pass the string (this.$i18n.locale) to an object and get the date-fns/locale code to fomart the date. But I am tented to use startOfYear instead. – Sargentogato Jul 08 '21 at 17:20

0 Answers0