C# Date time object with 24 hour format Without Am PM - c#

I am trying to set the DateTime format to 24 hours. Originally I have a string with a 12 hour representation. All solutions I have found are converting DateTime to string.
string dateString = "Mon 16 Jun 8:30 AM 2008"; // <-- Valid
string format = "dd/MM/yyyy HH:mm";
DateTime dateTime;
if (DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture,
DateTimeStyles.RoundtripKind, out dateTime))
{
DateTime dateIn24 = dateTime;// dateIn24 should be in 24 hour format
}
Is there anything we can do in web.config? Like the following:
<globalization fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8" culture="en-GB" uiCulture="en-GB"/>
i got the answer
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
if its en-US 12 hour format
by default it will be en-US based on system date time settings

The DateTime instance only contains the information, and the Hour property is an integer from 0 to 23, according to MSDN documentation:
Property Value
Type: System.Int32
The hour component, expressed as a value between 0 and 23.
https://msdn.microsoft.com/library/system.datetime.hour(v=vs.110).aspx
If you're talking about formatting it, then you need to convert it to a string, like mentioned in the comments.

As far as I know, the 24-hour format is neither not a matter of how the DateTime is parsed, nor saved, but rather of the formatting. Given that you enter the if-block (I have not been successful on parsing youre dateString in LinqPad) you can obtain a correctly formatted date string with
var dateStringWith24Hours = dateTime.ToString(dateString);
since the HH in your format string means that you'd like to format the hours as 24 hours.

Related

convert LocalDatetime to timestamp

I have an MVC application.
At controller(from view) I am getting start date as string "Tue Jan 01 2008 00:00:00 GMT 0100 (Central Europe Standard Time)".
Could anybody please tell me how to convert this datetime to normal dd-mmy-yyyy hh:mm:ss at Controller level.
Thanks in advance....
You need to use the DateTime.ParseExact method and supply the correct format string so the code knows what each part of the input string represents.
CultureInfo provider = CultureInfo.InvariantCulture;
string format = "ddd MMM dd yyyy hh:mm tt zzz";
result = DateTime.ParseExact(dateString, format, provider);
Double check the format strings against the documentation there are several subtle and not so subtle gotchas:
"h" represents the hour in 12 hour clock
"hh" represents the hour in 12 hour clock with leading zero
"HH" represents the hour in 24 hour clock with leading zero
"m" represents a minute from 0 to 59
"M" represents a month from 1 to 12
etc.
So you may find that you think your format string is OK for the examples you have, but then a new string will come along which won't parse. You'll have to examine it to see if you've got hours in 12 or 24 hour format, etc.
Though you will have to strip of the timezone name from the end of the string first as that won't be recognised by ParseExact.

DateTime Converted to Invalid Format

I tried converting 9/29/2013 2:44:28 PM (mm/dd/yyyy) to dd/mm/yyyy format.
I got a strange Date after Converting.
I tried
dateTimeVar.ToString("dd/mm/yyyy");
29/44/2013
The Date was a type of DateTime itself.
Lowercase mm means minutes, try this instead:
dateTimeVar.ToString("dd/MM/yyyy");
However, if this works depends on your local culture. If your current culture's date separator is different, / will be replaced with that. So if you want to enforce it use CultureInfo.InvariantCulture:
dateTimeVar.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
MM is for months, mm is for minutes. That's why it gets your minutes (which is 44) instead of your month value.
Use it like;
dateTimeVar.ToString("dd/MM/yyyy");
Check out;
The "MM" Custom Format Specifier
The "mm" Custom Format Specifier
And remember, / has special meaning when you use it as a date separator. It replace itself with your current culture date separator. Forcing to use with InvariantCulture would be better.
dateTimeVar.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
Take a look at;
The "/" Custom Format Specifier
What if I want to convert a string in dd/MM/yyyy to DateTime?
Then you can use DateTime.ParseExact method.
Converts the specified string representation of a date and time to its
DateTime equivalent using the specified format and culture-specific
format information. The format of the string representation must match
the specified format exactly.
As an example;
string s = "01/01/2013";
DateTime dt = DateTime.ParseExact(s, "dd/MM/yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(dt);
Output will be;
1/1/2013 12:00:00 AM
Here a DEMO.
dateTimeVar.ToString("dd/mm/yyyy"); // Change to dd/MM/yyyy
The problem is mm stands for minute and you need MM which would be months
Tim's answer is correct, but to remove the format string altogether you can use. 'ToShortDateString'
DateTime date = DateTime.Today;
var stringDate = date.ToShortDateString();
var stringDate2 = date.ToString("dd/MM/yyyy");

How to parse a string into a date time format in C#

i have a string which contains date time this...
string S="08/18/2013 24:00:00"
DateTime DT = DateTime.ParseExact(S, "MM/dd/yyyy HH:mm:ss", null);
i want to parse it into date time but shows an exception like this.
The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.
please tell me any solution for this problem.
The problem is with the hour being 24. DateTime doesn't support this, as far as I'm aware.
Options:
Use my Noda Time project which does support 24:00:00, but basically handles it by adding a day (it doesn't preserve a difference between that and "end of previous day")
Keep using DateTime, manually replace "24:00:00" with "00:00:00" when it occurs, and remember to add a day afterwards
If you want to preserve the information that it was actually "end of the day" you'd need to do that separately, and keep the information alongside the DateTime / LocalDateTime.
You should also parse with the invariant culture as other answers have suggested - you're not trying to parse a culture-specific string; you know the exact separators etc.
string S="08/18/2013 00:00:00"; // here is the first problem occurred
DateTime DT = DateTime.ParseExact(S, "MM/dd/yyyy hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
From The "HH" Custom Format Specifier
The "HH" custom format specifier (plus any number of additional "H"
specifiers) represents the hour as a number from 00 through 23; that
is, the hour is represented by a zero-based 24-hour clock that counts
the hours since midnight.
So, using 24 as an hour is invalid on this case.
Try with hh format with 00 instead like;
string S = "08/18/2013 00:00:00";
DateTime DT = DateTime.ParseExact(S, "MM/dd/yyyy hh:mm:ss", CultureInfo.InvariantCulture);
Here a DEMO.
If you really want to use 24:00:00 as a hour, take a look Noda Time which developed by Jon.

converting a string to date in the exact manner

I have a string "11 Jan 2011" which I want to convert to the datatype date (i.e 11 Jan 2011).
I have tried all resources about datetime.parse, datetime.parse exact but all these things gives me the same output 2011/01/11 12:00:00 AM. I really don't understand this behaviour. I tried the following:
1.DateTime date = DateTime.Parse("11 Jan 2011");
2.DateTime date = DateTime.ParseExact("11 Jan 2011" , #"dd MMM yyyy", System.Globalization.CultureInfo.InvariantCulture);
parsing and displaying are not the same thing
you parse the original string to a DateTime object but display results using Date/Time format strings
Both your calls are correct.
A DateTime structure preserves no information about formatting; it just represents the raw date and time.
What you need to do is ensure that when you display your date, you do so in the correct format - e.g. by calling string displayString = date.ToString("dd MMM yyyy");

DateTime Format like HH:mm 24 Hours without AM/PM

I was searching here about converting a string like "16:20" to a DateTime type without losing the format, I said I dont want to add dd/MM/yyy or seconds or AM/PM, because db just accept this format.
I tried with Cultures yet
Thanks in Advance
Just give a date format to your dateTime.
string DateFormat = "yyyy MM d " this willl give you the year month and day. after continuing;
string DateFormat = "yyyy MM d HH:mm:ss " in here the Capital H will give you the 24 hours time format and lowerCase "h" will give you the 12 hours time format...
when you give the Dateformat as a string you can do whatever you want with date and time.
string DateFormat = "yyyyMMdHHmmss";
string date = DateTime.Now.ToStrign(DateFormat);
OR
Console.writeline(DateTime.Now.ToStrign(DateFormat));
OUTPUT:
20120823132544
All DateTime objects must have a date and a time.
If you want just the time, use TimeSpan:
TimeSpan span = TimeSpan.Parse("16:20");
If you want a DateTime, add that time to the min value:
TimeSpan span = TimeSpan.Parse("16.20");
DateTime dt = DateTime.MinValue.Add(span);
// will get you 1/1/1900 4:20 PM which can be formatted with .ToString("HH:mm") for 24 hour formatting
DateTime.Now.ToString("hh:mm") - If it's C#.
Oh. Only read the header.
DateTime dt = new DateTime(2008, 12, 11, Convert.ToInt32("16"), Convert.ToInt32("32"), 0);
what do you mean by "losing the format".
if you convert it to a DateTime type, then the DateTime object will have dd/mm/yy and other properties. depending on how you plan to use the object, you can "recover" your original settings, by formatting the string output like this: DT.ToString("HH:mm");
Since you don't stipulate which DBMS you are using, it is hard to know which answer will help you. If you use IBM Informix Dynamic Server, you would simply use the data type 'DATETIME HOUR TO MINUTE', which will record values in the 24 hour clock.
DateTime.Parse("16:20")
I want to address this part of your question:
without losing the format
A database will generally store all datetime values in a standard common format that's not even human readable. If you use a datetime column the original format is destroyed.
However, when you retrieve the value you cast it back to any format you want. If you want HH:mm you can get it.

Categories

Resources