0

I have a DateTime in my .NET program that I need to print with particular value of time zone offset (for instance, +01:00 always). Output should contain full date time with timezone. It has to be unrelated to system timezone setting. How I could achieve this?

Example: I have a timestamp such as 12-03-2016T12:30:34+03:00 and I need to output it calculated for predefined TZ +1: 12-03-2016T10:30:34+01:00

onkami
  • 8,791
  • 17
  • 90
  • 176
  • does `DateTime.AddHours(hours)` not fulfill this? – Takarii Sep 08 '16 at 07:09
  • Well which bit is causing you problems? What "kind" of `DateTime` do you have (Utc, Unspecified, Local). Do you need to include the time zone offset itself, or are you just trying to output the local time? At the moment there aren't enough details for us to help you... – Jon Skeet Sep 08 '16 at 07:09
  • @JonSkeet i need time zone offset yes. The initial datetime is parsed from time stamp string. I guess it can be utc kind. – onkami Sep 08 '16 at 07:11
  • So can you provide a [mcve] showing sample input, expected output, and whatever output you've got from what you've tried so far? (Hint: DateTimeOffset is probably your friend here...) – Jon Skeet Sep 08 '16 at 07:14
  • @JonSkeet added the edit. – onkami Sep 08 '16 at 07:22
  • That's really not a [mcve], and it doesn't show how far you've got at all... – Jon Skeet Sep 08 '16 at 07:32
  • @JonSkeet Dear Jon, I believe my question is clear enough. I simply need a string output of a DateTime in pre-defined time zone. Could you pick up a utc kind of datetime and kindly provide the answer for the case? To be honest I see that question's formal structure now receives much more attention than its meaning. The example I gave for this trivial matter should be rather clear. – onkami Sep 08 '16 at 07:40
  • 1
    What *isn't* clear is whether you've got *anywhere* yet. Have you managed to parse the input yet? Have you tried using `DateTimeOffset` as I've suggested? Improving the question is in line with the aim of Stack Overflow to create a repository of high quality questions and answers... which means questioners need to put in some effort when asking questions. Your question would be vastly improved by a [mcve] as I requested earlier. Of course, you can ignore my advice and wait for someone else who doesn't care as much about question quality, or you can improve the question and I'll answer it. – Jon Skeet Sep 08 '16 at 07:43

1 Answers1

0

Found some approach to it.

First of all, DateTime does not have time zone stored in it. Instead it has flag whether it is UTC or Local (without the idea what Local TZ shift is). So: first thing is to get your initial parsing of time from any string time stamp in UTC.

Once it is stored in DateTime object (with Kind=UTC), you have to convert it to the timezone you desire output for. I find examples here useful: datetime to string with time zone.

Note: if you need to convert London daylight-saving time, you have to know right names of timezones in NET so you get it right. See Difference between UTC and GMT Standard Time in .NET

Community
  • 1
  • 1
onkami
  • 8,791
  • 17
  • 90
  • 176