开发者

Remove timestamp from date in C#?

I h开发者_运维百科ave a date like this: 2011-03-29 00:00:00.000

I want to remove the timestamp from that, is that possible?

Thanks :-)


Instance of DateTime class has ToShortDateString method that displays Date only


Maybe:

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


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

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

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

Hope this helps.


try

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

Or

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


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

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

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


                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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜