0

How do I tell .NET that a given DateTime is in a particular timezone? There seems to be no constructor where I can say "this is Paris time". If I serialise the time to JSON, remote systems think it's GMT when it's actually French.

If I try and force it to a French time zone, then it adds an hour which makes the time incorrect:

var france = TimeZoneInfo.FindSystemTimeZoneById("Romance Standard Time"); 
var frenchTime = TimeZoneInfo.ConvertTimeFromUtc(receipt.TransactionTimeStamp.Value, france);

I'm communicating with a 3rd party API which expects local times to be supplied in JSON format.

NickG
  • 9,315
  • 16
  • 75
  • 115
  • 2
    `DateTime` has no capability to store a timezone. Basically, a DateTime is either UTC or 'Local' (local being whatever 'locale' was detected when the program was started. There are many different ways to solve the timezone issue (always store as UTC, but convert to/from the user's chosen TZ from the 'UI' being just one). – Neil Aug 31 '18 at 15:56
  • Always store and transmit dates as UTC and your life will get infinitely more simple when it comes to challenges related to DateTime. – P. Roe Aug 31 '18 at 16:14
  • [DateTimeOffset](https://learn.microsoft.com/en-us/dotnet/standard/datetime/choosing-between-datetime#the-datetimeoffset-structure) includes timezones, but before you use that ensure you [really need to store it](https://stackoverflow.com/questions/4331189/). – Dour High Arch Aug 31 '18 at 16:42
  • Nick - if the json returned just had the raw date with NO offset, would that solve your problem? If so - you could always use `DateTime.SpecifyKind(dt, DateTimeKind.Unspecified)` to strip out timezone from the json entirely. – bri Aug 31 '18 at 16:43
  • @Bri - that change worked on my local machine but when I uploaded it to a live server which runs on UTC, the times reverted to being serialised as +0000 (UTC) instead of +0100 (Paris) – NickG Sep 05 '18 at 15:16
  • Serializing with newtonsoft? – bri Sep 06 '18 at 12:34
  • @Bri Yes I am... – NickG Sep 06 '18 at 12:35

0 Answers0