Reputation: 2525
I have following code:
string date = "13.04.2012";
string date2 = (DateTime.Parse(date).AddDays(1)).ToString();
This is working correctly without a problem but after the DateTime.Parse
function the variable date2 is '14.04.2012 00:00:00' but i would like to have only the date '14.04.2012' without the timestamp.
I thought about using the substring function like this:
string sub = date2.Substring(0, 10);
That would work like this but isn't there a better way to get that result?
Upvotes: 0
Views: 4836
Reputation: 2481
I think you are after formatting
System.DateTime now = System.DateTime.Now;
System.DateTime newDate = now.AddDays(36);
System.Console.WriteLine("{0:dd.mm.yyyy}", newDate);
Upvotes: 0
Reputation: 11844
Try DateTime.Date property. May be it will be correct for this. See the below code part
DateTime dateOnly = date1.Date;
and out put will be
// 6/1/2008
EDIT:
or simply you can try
DateTime.ToString("dd.MM.yyyy");
Upvotes: 0
Reputation: 437376
DateTime.Parse
returns a DateTime
value, which is not really a string so it's wrong to say that it has the value '14.04.2012 00:00:00'
.
What you need to do here is add a format parameter to the ToString
call, or use one of the convenience formatting methods.
Upvotes: 0
Reputation: 4652
try this
string date = "13.04.2012";
string date2 = (DateTime.Parse(date).AddDays(1)).ToShortDateString();
Upvotes: 6