-1

I have tried following way but doesn't work

dateTime="2018-12-13T07:33:35.893Z"

DateTime dt;

DateTime.TryParseExact(dateTime, out dt); 

But I am always getting dt as {1/1/0001 12:00:00 AM}.

Can you please tell me why? and how can I convert that string to date?

I also tried Convert.ToDateTime but doesn't work.

What I actually want is getting the dd/MM/yyyy string'd DateTime so I could perform a query on a DB.

Gonzo345
  • 1,133
  • 3
  • 20
  • 42
Mangesh Ukade
  • 35
  • 1
  • 4
  • 2
    Welcome to Stack Overflow. That code wouldn't compile - `TryParseExact` has no overload accepting only a single string. Please post the actual code you're using. Basically, you need to tell `TryParseExact` the format of your string - that's the part you haven't shown us that part. Additionally, you should use the return value of `TryParseExact` to detect when parsing has failed. – Jon Skeet Dec 17 '18 at 08:12
  • 1
    As an aside: remember that a `DateTime` object doesn't have a format (it's just stored as a single number). If you need formatting, you need a string. – ProgrammingLlama Dec 17 '18 at 08:17
  • can you suggest for india – Mangesh Ukade Dec 17 '18 at 08:27
  • 1
    @Mangesh What do you mean "suggest for India"? – ProgrammingLlama Dec 17 '18 at 08:33
  • @MangeshUkade That's an ISO8601 date. It's the only format *not* affected by locales and timezones - `Z` specifies UTC. A simple `DateTime.Parse()` or `DateTimeOffset.Parse()` just work. There's no reason to use `ParseExact`. What are you trying to do? – Panagiotis Kanavos Dec 17 '18 at 09:12
  • @MangeshUkade besides, `DateTime` and `DateTimeOffset` objects have no format. They are binary objects. If you want to display them in a certain way, specify the format string you want when converting them to strings – Panagiotis Kanavos Dec 17 '18 at 09:13

2 Answers2

1

Have you got the original DateTime object or you simply have it in a string?

In case you've got it as DateTime:

string european = dateTime.ToString("dd/MM/yyyy");

In case you've got it as a string:

string date = "2018-12-13T07:33:35.893Z";

if(DateTime.TryParse(date , out DateTime result))
    result.ToString("dd/MM/yyyy");

Have a look at the original MSDN documentation about the DateTime.ToString method

Since you've got a DateTime you can convert to that format:

var thisExactMoment = DateTime.Now;
thisExactMoment.ToString("dd/MM/yyyy");

With your "dateTime" variable, just perform dateTime.ToString("dd/MM/yyyy") and you're ready to go.

Gonzo345
  • 1,133
  • 3
  • 20
  • 42
  • gives an error for socond answer if(DateTime.TryParse(date , out DateTime result)) result.ToString("dd/MM/yyyy"); – Mangesh Ukade Dec 17 '18 at 08:24
  • System.DateTime is type but used as variabe – Mangesh Ukade Dec 17 '18 at 08:24
  • @MangeshUkade What do you actually need to get? A string with that dd/MM/yyyy? – Gonzo345 Dec 17 '18 at 08:25
  • i want to pass that string to the database for filtering data from DB – Mangesh Ukade Dec 17 '18 at 08:29
  • @MangeshUkade I've updated my solution with an example of using "now". It gets the system's time and converts it to `dd/MM/yyyy` string. It's applying the same to your `DateTime`. – Gonzo345 Dec 17 '18 at 08:31
  • DateTime.now is working fine for me but I want "2018-12-13T07:33:35.893Z" this string conversion to DateTime because this format i got from angular file for filteration purpose from date to TO date – Mangesh Ukade Dec 17 '18 at 08:35
  • @MangeshUkade My second case is your way to go. It performs a `TryParse` from that string so if it passes you get the string in the format you desired. In case you want to do whatever with the `DateTime` object itself, then use the `result` variable inside the `if` body. – Gonzo345 Dec 17 '18 at 08:39
0
var dateTime = "2018-12-13T07:33:35.893Z";
var x = DateTime.Parse(dateTime).ToString(@"MM\/dd\/yyyy");
JohnyL
  • 6,894
  • 3
  • 22
  • 41