简体   繁体   中英

DateTime format culture - how to change it?

With this line in my web.config , numbers like 2.44 or 0.5 works properly - generally all decimals in ViewModels .
I found solution here, that if I want to have decimals in viewmodel, I have to change globalization like this:

<globalization culture="en-us" uiCulture="pl-PL" />

But now date is in format 6/19/2013 10:04:18 PM - I want european format, like 19-6-2013 22:04:18 . Always and automatically.

When I change culture into pl-PL , date format is proper, but decimals in textboxes doesn't work.. (return 0 when number is decimal).

How can I have european datetime format and working decimals?

Regards

I had quite the same problem, as I'm from Romania. Here's my solution:

Views/Shared/DisplayTemplates/Date.cshtml (create folders and file if not existing)

@model Nullable<DateTime>

@{
    DateTime dt = DateTime.Now;
    if (Model != null) {
        dt = (System.DateTime)Model;
    }
    @dt.ToString("dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
}

Basically, whenever you're using Html.Display or Html.DisplayFor for a DateTime (nullable, in my example), it will be rendered according to this template. You can also set up an editor template (where I use jquery ui's DatePicker). You can read more about MVC templates here

Script referenced in Layout.cs (therefore available in all views)

$.validator.methods.number = function (value, element) {
    value = floatValue(value);
    return this.optional(element) || !isNaN(value);
}
$.validator.methods.range = function (value, element, param) {
    value = floatValue(value);
    return this.optional(element) ||
        (value >= param[0] && value <= param[1]);
}

function floatValue(value) {
    return parseFloat(value.replace(",", "."));
}

This is for fixing the decimal mark issue in textboxes and such.

I'm not saying it's the best solution (at least the latter part, where actually replacing dots and commas might appear raw), but it gets the job done. I also mention that I use

<globalization enableClientBasedCulture="false" culture="ro" uiCulture="ro" />

, thereby forcing the culture (I never have to display date or currency - or whatever decimal number - in a different format).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM