0

I would like to convert the real date with the function DateTime.Today from this format: "8/24/2012 4:09:17 AM" to this one: "2012-06-28T08:26:57Z".

Do you have any ideas how I am supposed to do this?

Thank you for your help.

  • https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings - you can call ToString() on DateTime.Today with a standard format string – Steven Wood Jun 10 '22 at 08:26
  • Hi and welcome. So it seems that you want a string representation of your `DateTime` object. Is this correct? – Mong Zhu Jun 10 '22 at 08:27
  • `DateTime.Today` always returns the date of today with time component set to 00:00:00, but in your example it is not the case. Maybe you need `DateTime.Now`. – wohlstad Jun 10 '22 at 08:29
  • You can see some examples how to format a `DateTime` string here: https://stackoverflow.com/questions/3025361/c-sharp-datetime-to-yyyymmddhhmmss-format. – wohlstad Jun 10 '22 at 08:33
  • In fact I am creating a function that needs to take as start time the **DateTime.Today** starting with midnight. Althought the problem is that the date in the DB has not the same format as the function **DateTime.Today**. That is why i need to convert it. – Doina Verejan Jun 10 '22 at 08:37
  • " the problem is that the date in the DB has not the same format " what is the column type in the DB? you see the format exists only in the string representation of a date not in the actual type `DateTime` or `Date`. Can you please post the code that reads this column from the DB ? – Mong Zhu Jun 10 '22 at 12:39
  • @MongZhu I am coding in C# and with the API. I tried to copy/paste the fonction here, but its too long... – Doina Verejan Jun 10 '22 at 13:37
  • we don't need the entire method. we need to see only the problematic part, for the beginning – Mong Zhu Jun 10 '22 at 13:47
  • The problem is that I am trying to search an event during a specific period of time. The start time needs to be the current date starting with midnight and the end time needs to be the current time. So for the start time, if I specifically write a date like this for ex **string thisDay = "2022-06-10T07:30:00.00Z"** and end time: **string endTime = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");** it's ok I get the event that i want. Although I need as start time to be a fonction of dateTime so for it to work no matter the day/hour etc. so I wrote like this: (see the next comment) – Doina Verejan Jun 10 '22 at 14:00
  • **DateTime thisDay = DateTime.Today;** and with the same endTime as previously. The only problem is that while I write like this, I do not find the event, even if I know that there is one that was triggered before. – Doina Verejan Jun 10 '22 at 14:02

0 Answers0