Reputation: 3806
DateTime Temp= new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, site.start.Hour, site.start.Minute, 0);
site.start.Hour
is 23
My question is my start hour is 23 but Temp DateTime Object is showing hour in 12 hour format that is 11 pm. Why and how to get Temp value in format in which i am passing the value of site.start.Hour that is 24 hour format?
Upvotes: 1
Views: 54
Reputation: 17004
As others already commented, DateTime
doesn't store any display information.
When you hover your variable in the Debugger, ToString()
is called and displays a value for your current Environment. Most likely you are in an environment with 12 hour format.
To get the actual hour, you should use the Hour
property:
DateTime Temp = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, site.start.Hour, site.start.Minute, 0);
//should be 23
var tempHour = Temp.Hour;
You have different format strings, which ToString
can use. When the additional parameter IFormatProvider
(CultureInfo
for example) is not given, the string is displayed in the current threads culture.
So when you want to display it in a specific culture, use the culture and format string you want:
var cultureInfo = new CultureInfo("de-DE") // germany has 24 hour clock for example
var str = Temp.ToString("g", cultureInfo);
Generaly, only change the culture, if you only want to display your Date in the specific culture. In most cases, we do not want to limit it, so it's always in the format your user would expect.
Upvotes: 2