24-hour clock
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
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{}
Hi @mattlewis92, Can you please help me to implement this? I can't get it to work..
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 Thank you for the fast response!
@mattlewis92, One more thing please - why I don't see the events in the Calendar .. ? http://plnkr.co/edit/Ve1VQN3AjoiYNDM3RRto?p=preview
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 You are rock 👍 Thanks
Error in importing CalendarEventTitleProvider in my case.
Hi, how to calculate no of days and hours from startDate and endDate when we add new event
@mattlewis92 I need to show only from 09:00 to 17:00 hours in day view? how can I achieve this?
@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
Elegant solution, thank you very much!
For >= angular 9, the CustomDateFormatter should have a @Injectable() annotation - for >= angular 10, it must have the annotation.
@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
@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?
@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 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
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
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
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.
@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?