MrLakish
MrLakish

Reputation: 31

Set the correct Datetime format

I got an multi language Application in c# with a dropdownfield to select the duration of a project

this is how it looks in English

enter image description here

this is how it looks in German

enter image description here

the dropdown should always look like the one in German.

here is the code:

@(Html.Kendo().Grid<WorkTimesViewModel>(Model.WorkTimes)
      .Name("grid")
      .Columns(columns =>
      {
          columns.Bound(p => p.Project).Title(Html.LabelFor(m => m.Projekt).ToString()).Width(140).ClientTemplate("#: Project.Name #");
          columns.Bound(p => p.Activity).Title(Html.LabelFor(m => m.Aktivität).ToString()).Width(140).ClientTemplate("#: Activity.Name #");
          columns.Bound(p => p.Duration).Title(Html.LabelFor(m => m.Dauer).ToString()).Format("{0:t}").Width(80);
          columns.Bound(p => p.Description).Title(Html.LabelFor(m => m.Bemerkung).ToString()).Width(180);
          columns.Command(command =>
          {
              command.Edit().Text(" ").CancelText(" ").UpdateText(" ");
              command.Destroy().Text(" ");
          }
              ).Width(140);
      })
      .Editable(editable => editable.Mode(GridEditMode.InLine).DisplayDeleteConfirmation(Html.LabelFor(m => m.LöschAktivi).ToString()))
      .HtmlAttributes(new { style = "width: 830px; height: 302px;" })
      .ToolBar(toolbar => toolbar.Create().Text(Html.LabelFor(m => m.NeuerArbeitszeiteintrag).ToString()))
      .Scrollable()
      .Selectable(conf => conf.Enabled(true).Mode(GridSelectionMode.Single).Type(GridSelectionType.Row))
      .Events(a => a.Edit("onGridEdit"))
      .DataSource(dataSource => dataSource
                                    .Ajax()
                                    .Events(e => e.Error("error_handler").RequestEnd("onRequestEnd"))
                                    .Model(model =>
                                    {
                                        model.Id(p => p.Id);
                                        model.Field(p => p.Project).DefaultValue(ViewData["defaultProject"] as ProjectViewModel);
                                        model.Field(p => p.Activity).DefaultValue(ViewData["defaultActivity"] as ActivityViewModel);
                                        model.Field(p => p.Description).DefaultValue("");
                                        model.Field(p => p.Duration).DefaultValue(new DateTime(1, 1, 1, 1, 0, 0, DateTimeKind.Utc));

                                    })

this is the property in the model

/// <summary>
    /// Gets or sets the duration.
    /// </summary>
    [UIHint("CustomGridTime")]
    public DateTime Duration { get; set; }

and here is where I set the value:

private static WorkTimesViewModel PrepareModel(WorkTimesViewModel model)
    {
        var newDateTime = DateTime.UtcNow.Date;
        newDateTime = newDateTime.AddHours(model.Duration.Hour);
        newDateTime = newDateTime.AddMinutes(model.Duration.Minute);
        newDateTime = newDateTime.AddSeconds(model.Duration.Second);
        model.Duration = newDateTime;
        return model;
    }

Could somebody help me?

Best regards and Thx

Upvotes: 0

Views: 269

Answers (2)

Martin D.
Martin D.

Reputation: 2090

You can set Kendo's culture using JavaScript. Set the culture globally in the _Layout.cshtml file or locally by adding this in your view. It should apply the correct format to your TimePicker input.

<script type="text/javascript">
    kendo.culture("de-DE");
</script>

See Kendo UI documentation for more information: https://docs.telerik.com/kendo-ui/framework/globalization/overview

OR

Create an Editor Template named CustomGridTime.cshtml in the "Views > [WorkTimes?] > EditorTemplates" folder. Create the EditorTemplates folder if it doesn't exist.

@model DateTime
@Html.Kendo().TimePickerFor(m => m).Format(System.Globalization.CultureInfo.GetCultureInfo("de-DE").DateTimeFormat.ShortTimePattern)

Then change your grid column.

columns.Bound(p => p.Duration).Title(Html.LabelFor(m => m.Dauer).ToString()).Format("{0:" + System.Globalization.CultureInfo.GetCultureInfo("de-DE").DateTimeFormat.ShortTimePattern + "}").EditorTemplateName("CustomGridTime").Width(80);

Upvotes: 1

SunPaz
SunPaz

Reputation: 160

You could force culture of your current Thread like this :

Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");

Please refer to MSDN documentation for more information : https://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo(v=vs.110).aspx

Upvotes: 0

Related Questions