Back to Devexpress

DxSchedulerAppointmentMappings.TimeZoneId Property

blazor-devexpress-dot-blazor-dot-dxschedulerappointmentmappings-bc696335.md

latest9.9 KB
Original Source

DxSchedulerAppointmentMappings.TimeZoneId Property

Maps information about an appointment time zone ID.

Namespace : DevExpress.Blazor

Assembly : DevExpress.Blazor.v25.2.dll

NuGet Package : DevExpress.Blazor

Declaration

csharp
public DxSchedulerMapping TimeZoneId { get; set; }

Property Value

TypeDescription
DxSchedulerMapping

Maps the data field to the TimeZoneId appointment property.

|

Remarks

The following code snippet creates three appointments in different time zones. It also contains a combo box that allows users to select a time zone. Once they change the time zone, the OnValueChanged method updates the scheduler’s time zone and redraws the scheduled appointments accordingly.

razor
@using System.Globalization;

<DxScheduler StartDate="@DateTime.Today"
             DataStorage="@DataStorage"
             GroupType="SchedulerGroupType.Resource"
             VisibleResourcesDataSource="VisibleResources">
    <DxSchedulerDayView DayCount="1" ShowWorkTimeOnly="true"></DxSchedulerDayView>
    <DxSchedulerWeekView ShowWorkTimeOnly="true"></DxSchedulerWeekView>
    <DxSchedulerWorkWeekView ShowWorkTimeOnly="true"></DxSchedulerWorkWeekView>
    <DxSchedulerMonthView ShowWorkDaysOnly="true" MonthCount="1"></DxSchedulerMonthView>
    <DxSchedulerTimelineView Duration="@TimeSpan.FromHours(48)" CellMinWidth="80">
        <Scales>
            <DxSchedulerTimeScale Unit="@SchedulerTimeScaleUnit.Day" UnitCount="1"></DxSchedulerTimeScale>
            <DxSchedulerTimeScale Unit="@SchedulerTimeScaleUnit.Hour" UnitCount="2"></DxSchedulerTimeScale>
        </Scales>
    </DxSchedulerTimelineView>
</DxScheduler>

<p></p>

<DxComboBox Data="timeZones" Value=selectedTimeZone ValueChanged="@((string t) => OnValueChanged(t))"></DxComboBox>

@code {
    List<string> timeZones = new List<string>() { 
        "UTC", "Egypt Standard Time", "Turkey Standard Time", "Caucasus Standard Time" 
    };
    string selectedTimeZone { get; set; } = "UTC";
    System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> zones = TimeZoneInfo.GetSystemTimeZones();
    DxSchedulerDataStorage DataStorage = null;

    protected override void OnInitialized() {
        base.OnInitialized();
        DataStorage = new DxSchedulerDataStorage() {
                AppointmentsSource = AppointmentCollection.GetAppointments(),
                AppointmentMappings = new DxSchedulerAppointmentMappings() {
                    Id = "AppointmentID",
                    Type = "AppointmentType",
                    Start = "StartDate",
                    End = "EndDate",
                    Subject = "Caption",
                    AllDay = "AllDay",
                    Location = "Location",
                    Description = "Description",
                    LabelId = "Label",
                    StatusId = "Status",
                    RecurrenceInfo = "Recurrence",
                    ResourceId = "ResourceId",
                    TimeZoneId = "TimeZone"
                },
                ResourcesSource = ResourceCollection.GetResourcesForGrouping(),
                ResourceMappings = new DxSchedulerResourceMappings() {
                    Id = "Id",
                    Caption = "Name",
                    BackgroundCssClass = "BackgroundCss",
                    TextCssClass = "TextCss"
                },
                TimeZone = TimeZoneInfo.GetSystemTimeZones().FirstOrDefault(tz => tz.Id == selectedTimeZone)
            };
        DataStorage.RefreshData();
    }

    void OnValueChanged(string newTZ) {
        selectedTimeZone = newTZ;
        DataStorage.TimeZone = TimeZoneInfo.GetSystemTimeZones().FirstOrDefault(tz => tz.Id == selectedTimeZone);
        DataStorage.RefreshData();
    }

    List<Resource> VisibleResources = ResourceCollection.GetResources().Take(2).ToList();
}
csharp
public class Appointment {
    public Appointment() { }
    public int AppointmentID { get; set; }
    public int AppointmentType { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public string Caption { get; set; }
    public string Description { get; set; }
    public string Location { get; set; }
    public int? Label { get; set; }
    public int Status { get; set; }
    public bool AllDay { get; set; }
    public string Recurrence { get; set; }
    public int? ResourceId { get; set; }
    public bool Accepted { get; set; }
    public string TimeZone { get; set; }
}
csharp
public class Resource {        
        public int Id { get; set; }
        public int? GroupId { get; set; }
        public string Name { get; set; }
        public bool IsGroup { get; set; }
        public string TextCss { get; set; }
        public string BackgroundCss { get; set; }
        public string ImageFileName => $"employees/{Id + 1}.jpg";
        public override bool Equals(object obj) {
            Resource resource = obj as Resource;
            return resource != null && resource.Id == Id;
        }
        public override int GetHashCode() {
            return Id;
        }
    }
csharp
public static partial class AppointmentCollection {
    public static List<Appointment> GetAppointments() {
        DateTime date = DateTime.Today;
        return new List<Appointment>() {
            new Appointment {
                AppointmentID = 1,
                AppointmentType = 0,
                Caption = "Watercolor Landscape (13:00 - TimeZone+2)",
                Label = 5,
                StartDate = date + (new TimeSpan(13, 0, 0)),
                EndDate = date + (new TimeSpan(16, 0, 0)),
                ResourceId = 0,
                TimeZone = "Egypt Standard Time"
            },
            new Appointment {
                AppointmentID = 2,
                AppointmentType = 0,
                Caption = "Oil Painting for Beginners (13:00 - TimeZone+3)",
                Label = 2,
                StartDate = date + (new TimeSpan(13, 0, 0)),
                EndDate = date + (new TimeSpan(16, 0, 0)),
                ResourceId = 0,
                TimeZone = "Turkey Standard Time"
            },
            new Appointment {
                AppointmentID = 3,
                AppointmentType = 0,
                Caption = "Testing (13:00 - TimeZone+4)",
                Label = 8,
                StartDate = date + (new TimeSpan(13, 0, 0)),
                EndDate = date + (new TimeSpan(16, 0, 0)),
                ResourceId = 0,
                TimeZone = "Caucasus Standard Time"
            }
        };
    }
    private static string ToString(DateTime dateTime) {
        return dateTime.ToString(CultureInfo.InvariantCulture);
    }
}
csharp
public static partial class ResourceCollection {
    public static List<Resource> GetResourcesForGrouping() {
        return GetResources().Take(3).ToList();
    }
    public static List<Resource> GetResources() {
        return new List<Resource>() {
            new Resource() { Id=0 , Name="Nancy Davolio", GroupId=100, BackgroundCss="dxbl-green-color", TextCss="text-white" },
            new Resource() { Id=1 , Name="Andrew Fuller", GroupId=101, BackgroundCss="dxbl-orange-color", TextCss="text-white" },
            new Resource() { Id=2 , Name="Janet Leverling", GroupId=100, BackgroundCss="dxbl-purple-color", TextCss="text-white" },
            new Resource() { Id=3 , Name="Margaret Peacock", GroupId=101, BackgroundCss="dxbl-indigo-color", TextCss="text-white" },
            new Resource() { Id=4 , Name="Steven Buchanan", GroupId=100, BackgroundCss="dxbl-red-color", TextCss="text-white" }
        };
    }
    public static List<Resource> GetResourceGroups() {
        return new List<Resource>() {
            new Resource() { Id=100, Name="Sales and Marketing", IsGroup=true },
            new Resource() { Id=101, Name="Engineering", IsGroup=true }
        };
    }
}
csharp
public static class DateTimeUtils {
    public static DateTime GetWeekStart(DateTime date) {
        return date.DayOfWeek == DayOfWeek.Sunday ? ValidWeekStart(date.Date) : ValidWeekStart(date.Date - CreateWeekOffset(date, DayOfWeek.Sunday));
    }
    public static DateTime GetBeginOfMonth(DateTime date) {
        return new DateTime(date.Year, date.Month, 1);
    }
    static DateTime ValidWeekStart(DateTime date) {
        TimeSpan weekSpan = new TimeSpan(7, 0, 0, 0);
        DateTime baseDate = date.Date;
        if (DateTime.MaxValue - date < weekSpan)
            return baseDate - weekSpan;
        return baseDate;
    }
    static TimeSpan CreateWeekOffset(DateTime date, DayOfWeek firstDayOfWeek) {
        int offset = (date.DayOfWeek + 7 - firstDayOfWeek) % 7;
        TimeSpan result = TimeSpan.FromDays(offset);
        if (date.Ticks < result.Ticks)
            result = TimeSpan.FromDays(offset - 7);
        return result;
    }
}

Note

In a real project, you may need to determine the time zone of the current user. You can store this information as part of the user account. You can also call the JavaScript method and get information about the client time zone from the browser (refer to this thread for example).

See Also

DxSchedulerAppointmentMappings Class

DxSchedulerAppointmentMappings Members

DevExpress.Blazor Namespace