Convert string to specific DateTime format

I’ve been searching the Internet for a long time, and for me life cannot find a solution. I thought it would be easy, but it would take too much time and I will move on to stackoverflow.

I need to convert a string that contains the date and time for a DateTime variable. I formatted the string in the exact format that I want to save, but when I convert it to DateTime, it adds seconds that I don't want. I want it to be stored as 01/01/2010 09:00. Here is the code I have used so far:

DateTime.ParseExact(startTime,"MM/dd/yyyy hh:mmtt", null); 

.. but he adds seconds to it. Please inform.

+2
source share
5 answers

If it is stored as a DateTime data type, it is saved correctly, but your user interface is not displayed correctly. The DateTime data type always has seconds (milliseconds, etc.), regardless of how you set the value. The problem is how it is displayed back to the user.

You need to display the desired date in the correct string format during the display, as in

 Label1.Text = startTime.ToString("MM/dd/yyyy hh:mmtt"); 

Edit - Added

To format it in a GridView, see here: http://peterkellner.net/2006/05/24/how-to-set-a-date-format-in-gridview-using-aspnet-20using-htmlencode-property/

+7
source

I think that when you say that you add seconds, you mean when you try to read this by reading also seconds, you want to do something like

 DateTime parsedDateTime = DateTime.ParseExact(startTime, "MM/DD/YYYY hh:mmtt", null); string dateTime = parseDateTime.ToString("MM/DD/YYY hh:mmtt"); 

The DateTime class always has seconds, milliseconds, and nanoseconds, which I think are stored in it. Always, if you fill them out or not, this assumes, although you just need to format how you read the information from the DateTime object so as not to give you pieces of DateTime data that you did not fill out or use.

If you store it in a database, understand that the database will also store all this information, whether you transfer it or not. There are different types of SQL DateTime, such as SmallDate, etc., which store different amounts of DateTime data, do not know if there is one that is stored for an hour, not a second, but with SQL you can also format the way that you select the data or just select it in your C # as a C # DateTime object, and then when you present it again, format ToString () so that it does not display the data you don't want / have.

+3
source

Try it.

  DateTime dt; if (DateTime.TryParse(startTime, out dt)) { string newDateTime = dt.ToString("MM/dd/yyyy hh:mmtt"); } 
+1
source

If you have a string, you must first ensure that it can actually be converted to a date and time. So, do it first. When you have a date-time, save it in the database as it is, you do not need to bypass the format (as others said here).

and then, if you need to, reformat it to another view only if you need to display it somewhere.

 string s = "21 June 2010 09:23:56"; DateTime dt; if (DateTime.TryParse(s, out dt)) // dit is a valid datetime, storeit or whatever else throw ApplicationException( "{0} is not a valid datetime.", s); // -- and now you can redisplay dt any way you want ----- debug.Print(s.ToString("d MMM yyyy"); debug.Print(s.ToString("HH:mm:ss"); debug.Print(s.ToString("dddd, d MMM yy"); debug.Print(s.ToString("MMM d, yyyy"); debug.Print(s.ToString("HH:mm:ss dddd"); 

//etc.

0
source

Here is an example for this.

  String strDate="12/20/2013"; string strFormat="dd/MM/yyyy"; DateTime objDT; if (DateTime.TryParseExact(strDate, strFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out objDT) == true) { Response.Write("<b>Formatted DateTime : </b>" + objDT.ToString()); } else { Response.Write("<b>Not able to parse datetime.</b>"); } 
0
source

All Articles