Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to convert string to date keeping 24 hr format in vb.net??
I have used date.parse but it is changing 24 to 12 hr format!!!
And format gives output as string....
Posted

Format will always give output as a string: because 12 hr / 24 hr is not an innate property of a date or time: it is only relevant when presenting the result to the user, or when getting an input from a user. DateTime objects do not care about 12 hr or 24 hour format, because they don't use them: internally they are just a number of milliseconds since an arbitrary fixed point in time.

To format a DateTime as a string with 12 or 24 format, just pass the appropriate format string to DateTime.ToString (or String.Format) - they are listed here: Formatting a DateTime for display - format string description[^] - if you don't supply a format specifier then the current system setting is used, which may be 12 or 14 hour, depending on the user preference.
 
Share this answer
 
DateTime.Parse produces a a DateTime object, that is an entity unaware of formats.
If you meant something like "DateTime.Parse doesn't intepretate correctly the input string" then you probably could use the DateTime.ParseExact[^] method, possibly specifying a custom format[^] (have a look at "H" and "HH" in the table).
 
Share this answer
 
Hi,

Try this:

C#
MyDateTime = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm tt",null);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900