6

I want to get midnight time of specific timezone in UTC. For e.g. consider my local machine timezone is +05:30. Please see below screenshot. I used https://www.epochconverter.com/ site for conversion.

enter image description here

In above example I enter midnight time in +05:30 and it give UTCtime for it.

Note: I am resetting my machine timezone to (UTC)Coordinated Universal Time.

So here what I want to find is, that UTC datetime. For this I have stored user timezone information. I get timezone info using below code.

TimeZoneInfo userTz = TimeZoneInfo.FindSystemTimeZoneById(LoginSessionDO.TimeZoneId);

So, I want to find midnight time of any timezone in UTC. How can I do that in c#?

for e.g. +05:30 - 2018-07-07 00:00:00 UTC- 2018-07-06 18:30:00

Sorry for uncleared question. I will explain my scenario.

EDIT:-

I have stored some user's activity data in Sql server table which contains dateofactivity column. All records datetime stored in UTC. So in frontend side, I want to fetch all todays activity records of user from midnight(When days start). If user is in +05:30 timezone & he want to fetch all 7th July 2018 00:00:00 records then I need to pass 6th July 2018 18:30:00.Now suppose another user is in Europe/Amsterdam (CEST) GMT +02:00 so want to find midnight time of Europe/Amsterdam in UTC.

Ajay
  • 6,418
  • 18
  • 79
  • 130
  • Midnight is 00:00 so just subtract the timezone offset from midnight and that is what the UTC time will be when it's midnight in whatever timezone you started with – Dave Jul 07 '18 at 08:06
  • UTC is not a format, it's a time zone. I don't really understand what is it that you're asking. – Zohar Peled Jul 07 '18 at 08:06
  • @ZoharPeled Sorry for uncleared question. If user is in any timezone I want midnight time of that timezone in UTC. Please see my last example. – Ajay Jul 07 '18 at 08:08
  • @Dave How can I do that using c#? – Ajay Jul 07 '18 at 08:09
  • @Dave using just utc offset won't work since there is also summer / winter (daylight saving) time included in timezone. Timezone offset to utctherefore may be different during year. – Tomas Apr 12 '21 at 13:41
  • @Tomas good point. I hate timezones – Dave Apr 13 '21 at 08:16

4 Answers4

8

Take the midnight time in your local timezone, get the timezone info for your timezone and call ConvertTimeToUtc

var tz = TimeZoneInfo.FindSystemTimeZoneById("Sri Lanka Standard Time");
var midnight = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Unspecified);
var converted = TimeZoneInfo.ConvertTimeToUtc(midnight, tz);

Console.WriteLine(midnight); //2000-01-01 00:00:00
Console.WriteLine(converted);//1999-12-31 18:30:00
Mirco Gericke
  • 331
  • 1
  • 8
2
 var midNightTime = new DateTime(2018, 7, 7, 0, 0, 0);
 TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"); // USE YOUR TIMEZONE INFO HERE INSTEAD.
 DateTime dateTimeInZone = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(midNightTime, timeZone.StandardName);
 DateTime universalTime = dateTimeInZone.ToUniversalTime();

Also if you are only looking for offset see here - Get Timezone Offset of Server in C#

Kedar Rudre
  • 134
  • 7
  • I tried your code. It gives me `07-07-2018 05:30:00` which is wrong. I set timezone to +05:30. So, according to this timezone I want `06-07-2018 18:30:00` – Ajay Jul 07 '18 at 08:30
  • Don't use `StandardName` as a substitute for `Id`. They are localized by OS language, and do not always match. Besides, you already have a `TimeZoneInfo`, so you can just use `ConvertTimeToUtc` and pass that as an argument. – Matt Johnson-Pint Jul 07 '18 at 19:57
  • Also, when you pass a `DateTime` with `DateTimeKind.Unspecified` to `ConvertTimeBySystemTimeZoneId`, it assumes that the value reflects the *local* time zone. The result will also have `DateTimeKind.Unspecified`, which when you call `.ToUniversalTime()` will again assume local. Thus, your code is actually converting from local time to Eastern time, then ignoring that it is Eastern and again converting from local to universal. It's not going to give the correct result in most cases. – Matt Johnson-Pint Jul 07 '18 at 20:03
1

Use DateTimeOffset.

It takes a DateTime and a TimeSpan for the offset, which you can use as the time zone, and has a ToUniversalTime method.

There is also good documentation that covers this topic: https://learn.microsoft.com/en-us/dotnet/standard/datetime/

George Helyar
  • 4,319
  • 1
  • 22
  • 20
  • 2
    Not exactly. Offsets can vary within a time zone depending on date. See "time zone != offset" in [the timezone tag wiki](https://stackoverflow.com/tags/timezone/info). One would have to know the specific offset in advance, or derive it from another function, in order to create the correct `DateTimeOffset` in the first place. – Matt Johnson-Pint Jul 07 '18 at 19:59
-2

Now datetime in UTC:

DateTime.UtcNow()

Parse to UTC:

DateTime.Parse("2018-05-08 12:00:00").ToUniversalTime()

Parse to Local Time:

DateTime.Parse("2018-05-08 00:00:00").ToLocalTime()
D3ivid
  • 63
  • 2
  • Date time objects don't know about timezones. They have a kind of local, UTC or none but local holds no information about what timezone local was when it was created. This makes this answer very fragile in.my opinion – Dave Jul 07 '18 at 08:14
  • This doesn't answer the question asked, as it doesn't use a *specific* time zone. – Matt Johnson-Pint Jul 07 '18 at 20:05