How to correctly convert Midnight in a specific timezone to Utc time?

Multi tool use
This seems like a simple problem, but looks to be harder than it seems:
How do I convert Midnight of a specified timezone and convert it to Utc time (regardless of the timezone of the local computer)?
One example: Today Midnight CET is 11pm (the date before) in UTC.
I tried the following:
DateTime midnight = TimeZoneInfo.ConvertTime(DateTime.UtcNow, specifiedTimeZoneInfo).Date;
DateTime utcTime = midnight.ToUniversalTime();
The problem is that this code only works if the timezone of the local computer running the code is the same timezone as used in the TimeZoneInfo.ConvertTime.
How can one do this regardless of the timezone of the local computer?
c# .net datetime
add a comment |
This seems like a simple problem, but looks to be harder than it seems:
How do I convert Midnight of a specified timezone and convert it to Utc time (regardless of the timezone of the local computer)?
One example: Today Midnight CET is 11pm (the date before) in UTC.
I tried the following:
DateTime midnight = TimeZoneInfo.ConvertTime(DateTime.UtcNow, specifiedTimeZoneInfo).Date;
DateTime utcTime = midnight.ToUniversalTime();
The problem is that this code only works if the timezone of the local computer running the code is the same timezone as used in the TimeZoneInfo.ConvertTime.
How can one do this regardless of the timezone of the local computer?
c# .net datetime
add a comment |
This seems like a simple problem, but looks to be harder than it seems:
How do I convert Midnight of a specified timezone and convert it to Utc time (regardless of the timezone of the local computer)?
One example: Today Midnight CET is 11pm (the date before) in UTC.
I tried the following:
DateTime midnight = TimeZoneInfo.ConvertTime(DateTime.UtcNow, specifiedTimeZoneInfo).Date;
DateTime utcTime = midnight.ToUniversalTime();
The problem is that this code only works if the timezone of the local computer running the code is the same timezone as used in the TimeZoneInfo.ConvertTime.
How can one do this regardless of the timezone of the local computer?
c# .net datetime
This seems like a simple problem, but looks to be harder than it seems:
How do I convert Midnight of a specified timezone and convert it to Utc time (regardless of the timezone of the local computer)?
One example: Today Midnight CET is 11pm (the date before) in UTC.
I tried the following:
DateTime midnight = TimeZoneInfo.ConvertTime(DateTime.UtcNow, specifiedTimeZoneInfo).Date;
DateTime utcTime = midnight.ToUniversalTime();
The problem is that this code only works if the timezone of the local computer running the code is the same timezone as used in the TimeZoneInfo.ConvertTime.
How can one do this regardless of the timezone of the local computer?
c# .net datetime
c# .net datetime
edited Dec 29 '18 at 22:35
OlavT
asked Dec 29 '18 at 22:05
OlavTOlavT
6751823
6751823
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Consider the following code:
var cestZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
var cestNow = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, cestZone);
var cestMidnight = cestNow.Date;
var cestMidnightInUTC = TimeZoneInfo.ConvertTimeToUtc(cestMidnight, cestZone);
When DateTime.UtcNow
is 12/29/2018 - 11:30:00 PM
Will result in:
If you live in CEST and look at clock
cestNow: 12/30/2018 - 12:30:00 AM
If you live in CEST and had a look at clock at midnight (start of today, 30 minutes ago)
cestMidnight: 12/30/2018 - 12:00:00 AM
At your midnight, UTC was
cestMidnightInUTC: 12/29/2018 - 11:00:00 PM
Note: 12:00:00 AM
is start of the day. So for example if utc now is 12/29/2018 - 11:30:00 PM
, midnight was 23:30
hours ago at 12/29/2018 - 12:00:00 AM
utc.
Above code will return12:00:00 AM UTC
regardless of time zone. If this is not what you are looking for, please elaborate more about what you are looking for.
– Reza Aghaei
Dec 29 '18 at 22:27
Is this what you are looking for?
– Reza Aghaei
Dec 29 '18 at 22:48
Above code and example describes the hours in UTC and CET. Independent from client time zone.
– Reza Aghaei
Dec 29 '18 at 23:40
I added a few more description to prevent any confusion.
– Reza Aghaei
Dec 29 '18 at 23:49
1
Now it looks correct and is exactly what I was looking for!
– OlavT
Dec 30 '18 at 0:23
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53973700%2fhow-to-correctly-convert-midnight-in-a-specific-timezone-to-utc-time%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Consider the following code:
var cestZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
var cestNow = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, cestZone);
var cestMidnight = cestNow.Date;
var cestMidnightInUTC = TimeZoneInfo.ConvertTimeToUtc(cestMidnight, cestZone);
When DateTime.UtcNow
is 12/29/2018 - 11:30:00 PM
Will result in:
If you live in CEST and look at clock
cestNow: 12/30/2018 - 12:30:00 AM
If you live in CEST and had a look at clock at midnight (start of today, 30 minutes ago)
cestMidnight: 12/30/2018 - 12:00:00 AM
At your midnight, UTC was
cestMidnightInUTC: 12/29/2018 - 11:00:00 PM
Note: 12:00:00 AM
is start of the day. So for example if utc now is 12/29/2018 - 11:30:00 PM
, midnight was 23:30
hours ago at 12/29/2018 - 12:00:00 AM
utc.
Above code will return12:00:00 AM UTC
regardless of time zone. If this is not what you are looking for, please elaborate more about what you are looking for.
– Reza Aghaei
Dec 29 '18 at 22:27
Is this what you are looking for?
– Reza Aghaei
Dec 29 '18 at 22:48
Above code and example describes the hours in UTC and CET. Independent from client time zone.
– Reza Aghaei
Dec 29 '18 at 23:40
I added a few more description to prevent any confusion.
– Reza Aghaei
Dec 29 '18 at 23:49
1
Now it looks correct and is exactly what I was looking for!
– OlavT
Dec 30 '18 at 0:23
add a comment |
Consider the following code:
var cestZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
var cestNow = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, cestZone);
var cestMidnight = cestNow.Date;
var cestMidnightInUTC = TimeZoneInfo.ConvertTimeToUtc(cestMidnight, cestZone);
When DateTime.UtcNow
is 12/29/2018 - 11:30:00 PM
Will result in:
If you live in CEST and look at clock
cestNow: 12/30/2018 - 12:30:00 AM
If you live in CEST and had a look at clock at midnight (start of today, 30 minutes ago)
cestMidnight: 12/30/2018 - 12:00:00 AM
At your midnight, UTC was
cestMidnightInUTC: 12/29/2018 - 11:00:00 PM
Note: 12:00:00 AM
is start of the day. So for example if utc now is 12/29/2018 - 11:30:00 PM
, midnight was 23:30
hours ago at 12/29/2018 - 12:00:00 AM
utc.
Above code will return12:00:00 AM UTC
regardless of time zone. If this is not what you are looking for, please elaborate more about what you are looking for.
– Reza Aghaei
Dec 29 '18 at 22:27
Is this what you are looking for?
– Reza Aghaei
Dec 29 '18 at 22:48
Above code and example describes the hours in UTC and CET. Independent from client time zone.
– Reza Aghaei
Dec 29 '18 at 23:40
I added a few more description to prevent any confusion.
– Reza Aghaei
Dec 29 '18 at 23:49
1
Now it looks correct and is exactly what I was looking for!
– OlavT
Dec 30 '18 at 0:23
add a comment |
Consider the following code:
var cestZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
var cestNow = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, cestZone);
var cestMidnight = cestNow.Date;
var cestMidnightInUTC = TimeZoneInfo.ConvertTimeToUtc(cestMidnight, cestZone);
When DateTime.UtcNow
is 12/29/2018 - 11:30:00 PM
Will result in:
If you live in CEST and look at clock
cestNow: 12/30/2018 - 12:30:00 AM
If you live in CEST and had a look at clock at midnight (start of today, 30 minutes ago)
cestMidnight: 12/30/2018 - 12:00:00 AM
At your midnight, UTC was
cestMidnightInUTC: 12/29/2018 - 11:00:00 PM
Note: 12:00:00 AM
is start of the day. So for example if utc now is 12/29/2018 - 11:30:00 PM
, midnight was 23:30
hours ago at 12/29/2018 - 12:00:00 AM
utc.
Consider the following code:
var cestZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
var cestNow = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, cestZone);
var cestMidnight = cestNow.Date;
var cestMidnightInUTC = TimeZoneInfo.ConvertTimeToUtc(cestMidnight, cestZone);
When DateTime.UtcNow
is 12/29/2018 - 11:30:00 PM
Will result in:
If you live in CEST and look at clock
cestNow: 12/30/2018 - 12:30:00 AM
If you live in CEST and had a look at clock at midnight (start of today, 30 minutes ago)
cestMidnight: 12/30/2018 - 12:00:00 AM
At your midnight, UTC was
cestMidnightInUTC: 12/29/2018 - 11:00:00 PM
Note: 12:00:00 AM
is start of the day. So for example if utc now is 12/29/2018 - 11:30:00 PM
, midnight was 23:30
hours ago at 12/29/2018 - 12:00:00 AM
utc.
edited Dec 29 '18 at 23:48
answered Dec 29 '18 at 22:16
Reza AghaeiReza Aghaei
65.7k854161
65.7k854161
Above code will return12:00:00 AM UTC
regardless of time zone. If this is not what you are looking for, please elaborate more about what you are looking for.
– Reza Aghaei
Dec 29 '18 at 22:27
Is this what you are looking for?
– Reza Aghaei
Dec 29 '18 at 22:48
Above code and example describes the hours in UTC and CET. Independent from client time zone.
– Reza Aghaei
Dec 29 '18 at 23:40
I added a few more description to prevent any confusion.
– Reza Aghaei
Dec 29 '18 at 23:49
1
Now it looks correct and is exactly what I was looking for!
– OlavT
Dec 30 '18 at 0:23
add a comment |
Above code will return12:00:00 AM UTC
regardless of time zone. If this is not what you are looking for, please elaborate more about what you are looking for.
– Reza Aghaei
Dec 29 '18 at 22:27
Is this what you are looking for?
– Reza Aghaei
Dec 29 '18 at 22:48
Above code and example describes the hours in UTC and CET. Independent from client time zone.
– Reza Aghaei
Dec 29 '18 at 23:40
I added a few more description to prevent any confusion.
– Reza Aghaei
Dec 29 '18 at 23:49
1
Now it looks correct and is exactly what I was looking for!
– OlavT
Dec 30 '18 at 0:23
Above code will return
12:00:00 AM UTC
regardless of time zone. If this is not what you are looking for, please elaborate more about what you are looking for.– Reza Aghaei
Dec 29 '18 at 22:27
Above code will return
12:00:00 AM UTC
regardless of time zone. If this is not what you are looking for, please elaborate more about what you are looking for.– Reza Aghaei
Dec 29 '18 at 22:27
Is this what you are looking for?
– Reza Aghaei
Dec 29 '18 at 22:48
Is this what you are looking for?
– Reza Aghaei
Dec 29 '18 at 22:48
Above code and example describes the hours in UTC and CET. Independent from client time zone.
– Reza Aghaei
Dec 29 '18 at 23:40
Above code and example describes the hours in UTC and CET. Independent from client time zone.
– Reza Aghaei
Dec 29 '18 at 23:40
I added a few more description to prevent any confusion.
– Reza Aghaei
Dec 29 '18 at 23:49
I added a few more description to prevent any confusion.
– Reza Aghaei
Dec 29 '18 at 23:49
1
1
Now it looks correct and is exactly what I was looking for!
– OlavT
Dec 30 '18 at 0:23
Now it looks correct and is exactly what I was looking for!
– OlavT
Dec 30 '18 at 0:23
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53973700%2fhow-to-correctly-convert-midnight-in-a-specific-timezone-to-utc-time%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Wstum2f,By JoQSHt A0L