I have a DateTimeOffset
instance, say: {11/13/2019 10:00:00 PM +02:00}
I want to keep the date time as is and change the offset to +00:00
=> {11/13/2019 10:00:00 PM +00:00}
Asked
Active
Viewed 3,378 times
4

Idrees
- 711
- 1
- 7
- 31
-
1Have you tried the `.DateTime` property? https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.datetime?view=netframework-4.8#System_DateTimeOffset_DateTime – Jasen Nov 13 '19 at 18:07
-
1Do you realize that your example result is now pointing to a completely different point in time? Did you mean to say that you want `{11/13/2019 08:00:00 PM +00:00}` instead (which is the same point in time, with zero offset, also called UTC) – Rufus L Nov 13 '19 at 18:33
-
@Jasen Yeah, 5 minutes after posting the question =D – Idrees Nov 13 '19 at 18:34
-
@RufusL Yes I do. I'm doing a syncing service with a MySQL db, and this weird requirement came from the business team. Thanks for mentioning that though, kudos :)) – Idrees Nov 13 '19 at 18:36
-
@Jasen But `DateTime` does not have an offset. Idrees, do you want a `DateTimeOffset` with zero offset, or a `DateTime`? See the excellent answer to [this question](https://stackoverflow.com/questions/4331189/datetime-vs-datetimeoffset) for some distinctions. – Rufus L Nov 13 '19 at 18:36
-
@RufusL Yes, I exactly want the SAME DateTime with zero offset. The original DB was saved wrong, so I had to correct the shift in offset manually. Thank you – Idrees Nov 13 '19 at 18:42
2 Answers
5
// Your original input
var dto1 = DateTimeOffset.Parse("2019-11-13T22:00:00.0000000+02:00");
// Here's one way to get the value you asked for:
var dto2 = new DateTimeOffset(dto1.DateTime, TimeSpan.Zero);
// Here's another way, which does the same thing:
var dto3 = dto1.Add(dto1.Offset).ToUniversalTime();
// But note that dto2 and dto3 are a different point in time.
// The actual UTC point in time is given by:
var dto4 = dto1.ToUniversalTime();
Console.WriteLine(dto1.ToString("o")); // "2019-11-13T22:00:00.0000000+02:00"
Console.WriteLine(dto2.ToString("o")); // "2019-11-13T22:00:00.0000000+00:00"
Console.WriteLine(dto3.ToString("o")); // "2019-11-13T22:00:00.0000000+00:00"
Console.WriteLine(dto4.ToString("o")); // "2019-11-13T20:00:00.0000000+00:00"
I'll also say, that you probably don't actually want to do this in most cases. The original value gave you the local time and the offset from UTC for that local time. By changing the offset without changing the time, you're actually picking a point in time that is 2 hours earlier. dto4
shows the correct conversion to UTC, which will indeed change the time (and possibly the date) as well.

Matt Johnson-Pint
- 230,703
- 74
- 448
- 575
-
15 minutes after posting the question I found out that DateTimeOffset had a getter to get the DateTime =D. And yes you are right, in most cases I don't want to do this. I'm doing a syncing service with a MySQL db, and this weird requirement came from the business team, because MySQL saves unixTimestamp, BUT the devs saved it wrongly. Thank you :)) – Idrees Nov 13 '19 at 18:37
2
I believe you would want something like this:
DateTimeOffset DTO = new DateTimeOffset(2019,11,13,10,0,0,0,new TimeSpan(2, 0, 0));
DateTimeOffset newDTO= new DateTimeOffset(DTO.DateTime,new TimeSpan(0, 0, 0));
Console.WriteLine(newDTO);
In this case DTO would be your original DateTimeOffset Instance, and you create the new one (or overwrite DTO) using the DateTime property of DTO and a new TimeSpan that corresponds with +00:00, newDTO displays "13.11.2019 10:00:00 +00:00"

Matías Odriozola
- 63
- 6