user458135
user458135

Reputation: 15

ASP.Net DateTime with CultureInfo

One general question:

Does the DateTime object stores the CultureInfo with it, or you need to use the Formatter to format the DateTime according to current culture ?

I have a class property that retuns a DateTime. Within that property I am setting the DateTime object with current culture information using CultureInfo object. Below is the code for class property I am using:

public DateTime PrintedQuoteDate { 
    get {
        DateTime printQuoteDate = DateTime.Today;
        // cInfo = CultureInfo object                             

        return Convert.ToDateTime(printQuoteDate , cInfo);

    }
}

So my question is when I will use the above property in my code, will it have the corrosponding culture information that I am setting in its get method, or I will have to use the same CONVERT code for formatting date time. The restriction here is that the Property should return only DateTime type.

Any idea, suggestions

Upvotes: 1

Views: 7455

Answers (1)

ntziolis
ntziolis

Reputation: 10231

DateTime does not store any Culture what so ever. In fact it does not even hold a reference to a TimeZone, all it knows is whether it is a UTC DateTime or not. This is handled by an internal enum.

You need to specify a format provider (every culture in itself is a format provider) when using the ToString method of a DateTime, otherwise it will use the culture (really the culture and not the UI Culture) of the current thread.

You can get a predifined culture by using the ISO country/locale codes like this:

var us = new CultureInfo("en-US");
var british = new CultureInfo("en-GB");
var danish = new CultureInfo("da");

As you can see for danish it is enough to specify the language since there are no other locales (to my knowledge).

Upvotes: 1

Related Questions