0

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?

colosso
  • 2,525
  • 3
  • 25
  • 49
  • There is already one link in stackoverflow itself, try http://stackoverflow.com/questions/501460/format-date-in-c-sharp – Muthu Feb 22 '12 at 11:19

5 Answers5

6

try this

string date = "13.04.2012";
string date2 = (DateTime.Parse(date).AddDays(1)).ToShortDateString();
Akrem
  • 5,033
  • 8
  • 37
  • 64
0

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.

Jon
  • 428,835
  • 81
  • 738
  • 806
0

DateTime.ToString(string format)

penartur
  • 9,792
  • 5
  • 39
  • 50
0

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");
0

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);
Krishna
  • 2,451
  • 1
  • 26
  • 31