[ACCEPTED]-Remove timestamp from date in C#?-date-format

Accepted answer
Score: 10

Instance of DateTime class has ToShortDateString 1 method that displays Date only

Score: 1

Maybe:

date.ToString("yyyy-MM-dd");

0

Score: 1

Probably the easiest way to do this is to 5 use the parse function within the DateTime 4 class which takes a string in the format 3 you've described:

http://msdn.microsoft.com/en-us/library/1k1skd40%28v=VS.90%29.aspx

You can then use the 'Date' property 2 within the class to get the date without 1 the time stamp.

Hope this helps.

Score: 0

try

DateTime d ="2011-03-29  00:00:00';
string strDate = d.ToShortDateString();

Or

string strDate = d.ToString("dd/MM/yyyy");

0

Score: 0

There are a few ways to do this, however 10 some depend on assumptions.

a) convert it 9 date/time and then output just the date, the 8 time is still there if you had wanted it b) copy 7 the string upto the first space c) copy 6 the first 11 characters (eg the date) d) regex 5 the output to confirm its a date/time format 4 and then pull out the date

It depends a little 3 I guess on what you want to do with it after 2 and assuming it is a string in the first 1 place. if not myDateTime.tostring('Y-M-D') would work.

Score: 0
                DataColumn date = new DataColumn("date");
                date.DataType = System.Type.GetType("System.String");
                SomeDataTable.Columns.Add(date);
                date.SetOrdinal(n); //placing it in the n+1th position
                //n is where the actual column you want to replace is
                foreach (DataRow dr in SomeDataTable.Rows)
                {
                    DateTime date;
                    if (DateTime.TryParse(dr["date"].ToString(), out date))
                    {
                        dr["date"] = convert.ToDateTime(dr["date"]).ToString("MM/dd/yyyy");
                    }
                }
                SomeDataTable.Columns.RemoveAt(n-1);

0

More Related questions