Remove timestamp from date in C#?

2019-09-04 17:18发布

I have a date like this: 2011-03-29 00:00:00.000

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

Thanks :-)

5条回答
地球回转人心会变
2楼-- · 2019-09-04 17:21

Maybe:

date.ToString("yyyy-MM-dd");
查看更多
Fickle 薄情
3楼-- · 2019-09-04 17:24

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.

查看更多
姐就是有狂的资本
4楼-- · 2019-09-04 17:25

try

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

Or

string strDate = d.ToString("dd/MM/yyyy");
查看更多
Evening l夕情丶
5楼-- · 2019-09-04 17:39

Instance of DateTime class has ToShortDateString method that displays Date only

查看更多
男人必须洒脱
6楼-- · 2019-09-04 17:46

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.

查看更多
登录 后发表回答