0

I am trying to convert string into DateTime object with format dd/mm/yyyy without time. but it comes with time. And also month changed to 01. The following is my coding.

  dateString = "4/11/2014"
  DateTime date = DateTime.ParseExact(dateString,"d/mm/yyyy",null);
                Debug.WriteLine("date:" + date);

output is 4/01/2014 12:03:00 AM

If you have any idea, I appreciate very much.

user3289230
  • 431
  • 2
  • 6
  • 19
  • possible duplicate of [Converting String to DateTime C#.net](http://stackoverflow.com/questions/919244/converting-string-to-datetime-c-net) – t1w Nov 08 '14 at 08:29
  • I'm really suprised your parsing operation will succeed. What is your `CurrentCulture`? What do you want as an output? – Soner Gönül Nov 08 '14 at 10:30

4 Answers4

3

DateTime doesn't have a format. Strings have formats. You need to convert your datetime to string in the format you want:

Debug.WriteLine("date:" + date.ToString("dd/MM/yyyy"));
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
1

You have several issues in your code. Format string is missing. mm represents minutes instead you must use MM for month. Also the object DateTime hasn't any representation it is just DateTime. You should make you own representation itself. For example:

var dateString = "04/11/2014";
DateTime date = DateTime.ParseExact(dateString,"dd/MM/yyyy",null);
Debug.WriteLine("date: " + date.ToString("dd/MM/yyyy"));
Hamlet Hakobyan
  • 32,965
  • 6
  • 52
  • 68
0

You can use date.date which will output the date part of the DateTime object.

Eric
  • 19,525
  • 19
  • 84
  • 147
0

I found solution.

DateTime date = DateTime.ParseExact(dateString,"dd/MM/yyyy",null).Date;

.Date shows only date without time.

Thank you guys for reply.

user3289230
  • 431
  • 2
  • 6
  • 19