angular-calendar icon indicating copy to clipboard operation
angular-calendar copied to clipboard

24-hour clock

Open theunreal opened this issue 10 years ago • 25 comments

Is it possible to change the time and clock of the calendar to 24-hour clock format?

For example:

13:00 instead 1PM

23:00 instead of 11PM

theunreal avatar Oct 26 '16 18:10 theunreal

You can use DI to handle this:

import {CalendarNativeDateFormatter, DateFormatterParams, CalendarModule} from 'angular2-calendar';

class CustomDateFormatter extends CalendarNativeDateFormatter {

  public dayViewHour({date, locale}: DateFormatterParams): string {
    // change this to return a different date format
    return new Intl.DateTimeFormat(locale, {hour: 'numeric'}).format(date);
  }

}

@NgModule({
  import: [CalendarModule],
  providers: [
    CalendarEventTitleProvider, 
     {provide: CalendarDateFormatter, useClass: CustomDateFormatter}
  ]
})
class ModuleThatUsesTheCalendar{}

mattlewis92 avatar Oct 27 '16 08:10 mattlewis92

Hi @mattlewis92, Can you please help me to implement this? I can't get it to work..

eliortabeka avatar Jun 07 '17 10:06 eliortabeka

It seems like you need to force the locale to make this work, using canadian seems to do the trick: http://plnkr.co/edit/INtKoU2Tony08Soqy8x7?p=preview

mattlewis92 avatar Jun 07 '17 10:06 mattlewis92

@mattlewis92 Thank you for the fast response!

eliortabeka avatar Jun 07 '17 10:06 eliortabeka

@mattlewis92, One more thing please - why I don't see the events in the Calendar .. ? http://plnkr.co/edit/Ve1VQN3AjoiYNDM3RRto?p=preview

eliortabeka avatar Jun 07 '17 11:06 eliortabeka

There was no color set on each event (check the console output in your plunker). TS should complain about the missing properties in a real app though. http://plnkr.co/edit/b4fbs11FBOeldGifjmpt?p=preview

mattlewis92 avatar Jun 07 '17 12:06 mattlewis92

@mattlewis92 You are rock 👍 Thanks

eliortabeka avatar Jun 07 '17 12:06 eliortabeka

Error in importing CalendarEventTitleProvider in my case.

megamind2301 avatar Jul 17 '17 10:07 megamind2301

Hi, how to calculate no of days and hours from startDate and endDate when we add new event

nareshkumargv avatar Mar 21 '19 16:03 nareshkumargv

@mattlewis92 I need to show only from 09:00 to 17:00 hours in day view? how can I achieve this?

harsh-khandelwalcs avatar Aug 09 '20 04:08 harsh-khandelwalcs

@harsh-khandelwalcs i think you can use daystartHour and dayEndHOur Attribute of the Calendar so for example:

<mwl-calendar-day-view
                            [viewDate]="viewDate"
                            [events]="events"
                            [locale]="locale"
                            [weekStartsOn]="startWeek"
                            [dayStartHour]="9"
                            [dayEndHour]="17">
    </mwl-calendar-day-view>

That would be a day view only from 09:00 to 17:00 hours

JustinDuchow01 avatar Oct 11 '20 13:10 JustinDuchow01

Elegant solution, thank you very much!

For >= angular 9, the CustomDateFormatter should have a @Injectable() annotation - for >= angular 10, it must have the annotation.

stefanzugal avatar Mar 16 '21 08:03 stefanzugal

@Everyone Hi, How can I add an event with startTime as 10:25PM and endTime as 11:15PM, then plot it on the calendar day view to show that exact time on the calendar as an event. The view is in THIS format. i.e 1PM = 13:00

bbmattieu9 avatar Oct 30 '21 21:10 bbmattieu9

@mattlewis92 Hi, I have angular-calendar instead of angular2-calendar. In my case this solution is not working. Angular version is 6 and "angular-calendar": "0.23.2". Does this feature work with angular-calendar?

Simran-Satpute-964 avatar Jun 03 '22 05:06 Simran-Satpute-964

@mattlewis92 In the WeekViewComponent there is a:

/**
* The locale used to format dates
*/
@Input() locale: string;

Apparently (having a working version with [locale] binded to "nl-NL"; Netherlands) this ain't used for formatting times (I want to get rid of the a.m./p.m. notation). Although it gets passed into:

<mwl-calendar-week-view-hour-segment
              *ngFor="let segment of hour.segments; trackBy: trackByHourSegment"
              [style.height.px]="hourSegmentHeight"
              [segment]="segment"
              [segmentHeight]="hourSegmentHeight"
              [locale]="locale"
              [customTemplate]="hourSegmentTemplate"
              [isTimeLabel]="true"
              [daysInWeek]="daysInWeek"
            >
            </mwl-calendar-week-view-hour-segment>

Where it is used as parameter in:

segment.displayDate
              | calendarDate
                : (daysInWeek === 1 ? 'dayViewHour' : 'weekViewHour')
                : locale

But doesn't lead to a time representation in 24h manner which is used in The Netherlands for example.

What do I overlook? Any help is greatly appreciated and I highly prefer binding the locale instead of overriding a DateFormatter.

Bernoulli-IT avatar Jun 17 '22 15:06 Bernoulli-IT

@Bernoulli-IT I had the same problem and I was able to make it work (24-hour clock with German locale settings) by adding to my module:

import {  CalendarDateFormatter, CalendarNativeDateFormatter} from "angular-calendar";
...
  providers: [
    {provide: CalendarDateFormatter, useClass: CalendarNativeDateFormatter}
  ]

No need to create my own subclass of CalendarNativeDateFormatter and override a method like in @mattlewis92's example. Passing CalendarNativeDateFormatter as provider for CalendarDateFormatter is enough. (Given I passed locale='de' to mwl-calendar-week-view and having registerLocaleData(localeDe, 'de', localeDeExtra) in the module anyway. )

Can't answer why it works/how it works internally, but I hope that observation helps.

@mattlewis92 thank you for this awesome calendar component, great work!

tbelmega avatar Nov 04 '22 01:11 tbelmega

@tbelmega

I tried to follow your instructions but am having trouble with the I've added the [locale]="nl-NL" into the html on the mwl-calendar-week-view. And 'registerLocaleData(localeNL);' was already present in the app.module.

The calendar is now only showing 00 - 23 instead of 00:00 - 23:00. Am I right in thinking that I need a localeNlExtra to get the minutes? How/where can I define that parameter?

mcbashie avatar Dec 01 '22 16:12 mcbashie

@mcbashie My solution doesn't help you there. CalendarNativeDateFormatter displays only hours, not minutes, and I'm fine with that. I just wanted to localize the format for Germany, so that it shows "13 Uhr" instead of "1 PM". CalendarNativeDateFormatter can do that out of the box, that's what I showed.

If you want a different format, you can create your own date formatter and provide it instead of the CalendarNativeDateFormatter. Follow this answer my mattlewis92, but instead of new Intl.DateTimeFormat(locale, {hour: 'numeric'}) provide your own DateTimeFormat. Let me take a wild guess, new Intl.DateTimeFormat(locale, {hour: 'numeric', minute: 'numeric'}) should do the trick? But you better check the docs of Intl.DateTimeFormat

tbelmega avatar Dec 02 '22 03:12 tbelmega

@tbelmega Thank you so much for the quick reply! Adding minute: 'numeric' worked. Also thanks for the link to Intl.DateTimeFormat. I also added: hour12: false just in case.

I also realized I had to add weekViewHour as well besides the dayViewHour.

mcbashie avatar Dec 02 '22 07:12 mcbashie

@tbelmega Hey, can you explain a bit more WHERE you would put the code? I read quite often "Yeah, just add X and Y" and I am not a full-time frontend dev and wonder "Okay, nice ... uhm where?". I, for instance, added this code from the dependency injection but sadly nothing changed. Does it need any kind of connection with the component? Is doing this stuff in the module rly everything there is?

MiriamTheFox avatar Oct 12 '24 17:10 MiriamTheFox