0

I'm using .Net Core Web API with EF

In the database we have a nullable DateTime column for example : 2021-07-25 11:48:13.820

Now when I query the database using Linq the datetime returned as 7/25/2021 11:48:13 AM

But I want to convert it to this format 2021-07-25T11:48:13.820000Z

Honestly I don't even now what this format is called. maybe if someone can tell me the format name then it will help ma to find a solution. But literally I'm stuck

Jack
  • 83
  • 6
  • 1
    Does this answer your question? [Given a DateTime object, how do I get an ISO 8601 date in string format?](https://stackoverflow.com/questions/114983/given-a-datetime-object-how-do-i-get-an-iso-8601-date-in-string-format) – Yong Shun Jul 27 '21 at 08:33
  • _"Now when I query the database using Linq the datetime returned as 7/25/2021 11:48:13 AM"_ - Until you have the final desired string format (in this case ISO8601) you should keep the value retrieved from the database as a .NET `DateTime`. – DiplomacyNotWar Jul 27 '21 at 08:36
  • @YongShun I tried it , it's giving me 2021-07-25T11:48:13Z I don't want that – Jack Jul 27 '21 at 09:06
  • 1
    Why do you expect `820000`? It seems that your value isn't that precise (it has no milliseconds/sub-millisecond component). If the data is there, it will be included: [example](https://rextester.com/GHY19640). – DiplomacyNotWar Jul 27 '21 at 09:08
  • The datetime returned is ***NOT*** `7/25/2021 11:48:13 AM`. That's how your computer is formatting the date returned. The `DateTime` value is simply a `ulong` - it does not have a format. – Enigmativity Jul 27 '21 at 10:39

1 Answers1

0

You can use

DateTime.Now.ToUniversalTime().ToString("o") //case sensitive

you can reference this page for more formats options.