joanllenas / ngx-date-fns

⏳ date-fns pipes for Angular
163 stars 14 forks source link

Get weekday name pipe #326

Closed breslavsky closed 4 years ago

breslavsky commented 4 years ago

Please check my suggestion for pipe <div>{{0 | dfnsWeekdayName:weekdayName.short}}</div>

import locale from 'date-fns/esm/locale/ru';

enum WeekdayName {
  full = 'full',
  short = 'short'
}

function getWeekdayName(day: number, view: WeekdayName = WeekdayName.full) {
  const now = new Date();
  const week = eachDayOfInterval({
    start: startOfWeek(now, {locale: locale}),
    end: endOfWeek(now, {locale: locale})
  });
  return format(week[day], view === WeekdayName.short 
    ? 'EEEEEE' : 'EEEE', {locale: locale});
}

console.log(getWeekdayName(0, WeekdayName.short)); // пн
console.log(getWeekdayName(1)); // вторник

The pipe will be useful to create calendars as we should write weekdays names based on current locale.

JoeFamiliar commented 4 years ago

That will be nice! Wanna it)

joanllenas commented 4 years ago

That's nice, until now this library has been strictly a one to one conversion from date-fns to Angular pipes, but I like the idea of opening a new utilities section to add more value. This could be the first of many :)

This weekend I'll work on all pending proposals. Thanks!

breslavsky commented 4 years ago

Thanks a lot for your job and reactive feedback ;-)

We included your library to our open source UI kit for angular. In 1 month we are preparing release and sure, will be a special section about ngx date fns how to configure locales.

I will share the link you soon ;-)

joanllenas commented 4 years ago

This is available on v6.1.0. See: https://github.com/joanllenas/ngx-date-fns/releases/tag/v6.1.0 Closing