2

I am in need to pass value of type DateTime to an API in the below format

2018-12-02T07:00:00.000Z //required format

but the datetime value from Db I am getting is as

2018-10-25 05:30:00.0000000

Now using the .ToString("o"), the value formatted as

2018-10-25T05:30:00.0000000

Checked below SO links

Where's the DateTime 'Z' format specifier?

http://stackoverflow.com/questions/9163977/converting-datetime-to-z-format

but not converting into the required format.

My understanding is having Z in the datetime represents UtcTimeZone.

halfer
  • 19,824
  • 17
  • 99
  • 186
Kgn-web
  • 7,047
  • 24
  • 95
  • 161
  • 1
    Do the datetimes in your database have an associated timezone, or are they already in UTC? – James Thorpe Dec 21 '18 at 10:47
  • @JamesThorpe No we are not storing anytime zone,its local time – Kgn-web Dec 21 '18 at 10:48
  • 1
    And do you need to send that local time to the API as though it were UTC, or do you need to actually send the UTC equivalent of that local time? IE do you have one problem (just formatting it correctly) or two problems (also converting between timezones). – James Thorpe Dec 21 '18 at 10:48
  • @JamesThorpe UTC equivalent of that value – Kgn-web Dec 21 '18 at 10:51
  • Does it actually have to end with `Z` or would other suffixes such as `-08:00` work? – Salman A Dec 21 '18 at 11:11
  • It's good practice to store all dates in your database as UTC, and don't allow a mix. Only when you actually display a date to the user should you convert to their local timezone. – mason Dec 21 '18 at 18:13
  • The answers so far are assuming you retrieve the value from the database as a string that needs to be parsed. Is that correct? Or are you reading a `DateTime` object from a `DataReader` or from Entity Framework or similar? It's difficult to answer your question without that information. Please edit the question to show us exactly how you load the field from your database. Thanks. – Matt Johnson-Pint Dec 28 '18 at 20:01

1 Answers1

3

Are you looking for something like this?

var baseTime = DateTime.Parse("2018-10-25 05:30:00.0000000");
var utcTime = baseTime.ToUniversalTime();
Console.WriteLine(utcTime.ToString("yyyy-MM-ddTHH:mm:ss.fffZ"));

You can try it out here: https://dotnetfiddle.net/HsdMcu

If you have summer and winter time in your region you might need to use a bit more sophisticated way for parsing your intial time correctly, this can be found in this answer.

Christoph Sonntag
  • 4,459
  • 1
  • 24
  • 49
  • Why not `.ToString("o")`? – ProgrammingLlama Dec 21 '18 at 11:04
  • 1
    Because it produces more digits after the last dot than the Op asked for - depending on the APIs requirements this might be a problem. – Christoph Sonntag Dec 21 '18 at 11:06
  • I agree with the above answer, but just note that you don't need the conversion to universal time, as there are no conversations being performed on the base date time. – Bigtoe Dec 21 '18 at 11:10
  • I just tried it out on my german system and it does make a difference as it parses the initial time on the local timezone (like the Op mentioned in his comments to be in the database) and then converts it. `utcTime.ToString` returns `2018-10-25T03:30:00.000Z` whereas `baseTime.ToString` returns `2018-10-25T05:30:00.000Z` on my system. – Christoph Sonntag Dec 21 '18 at 11:15
  • @Compufreak I suspect (and this is an assumption) that the OP may not want any change to the date passed in based upon local time, as it would then always depend on the current locale where the software was running as to what value would be placed in the utc time. – Bigtoe Dec 21 '18 at 11:56