C
C#10mo ago
khamas

✅ Creating a datetime with a timezone

How do I create a DateTime with new DateTime(2010, 05, 12); and give it a specific TimeZoneInfo
19 Replies
leowest
leowest10mo ago
I dont think DateTime can hold time zone that would be DateTimeOffset best u can get would be doing UTC and then using TimeZoneInfo
khamas
khamasOP10mo ago
yeah got it, but then how would I get the time of the first day of the month? I'd want to get like 01/03/2024 at 00:00 in the specific timezone
leowest
leowest10mo ago
get as in what sense? querying a db? just represent it in the datatime object or what? because if you're getting it from the db u would be doing so in UTC as well so I dont see the problem
Pobiega
Pobiega10mo ago
var tzi = TimeZoneInfo.Local; // set to whatever timezone you want.

var x = new DateTimeOffset(2010, 05, 12, 0, 0, 0, tzi.BaseUtcOffset);
var tzi = TimeZoneInfo.Local; // set to whatever timezone you want.

var x = new DateTimeOffset(2010, 05, 12, 0, 0, 0, tzi.BaseUtcOffset);
khamas
khamasOP10mo ago
in DB and server side it would be UTC, just that the clients work on their own time zone
leowest
leowest10mo ago
ok and what db is it?
khamas
khamasOP10mo ago
postgres
leowest
leowest10mo ago
so it uses DateTimeOffset
khamas
khamasOP10mo ago
anyways DB doesn't matter, it's all UTC there is no need for offset I just want the client app to have it's own timezone and I'll convert to UTC when sending to server
leowest
leowest10mo ago
sure then u would just use TimeZoneInfo to offset it
khamas
khamasOP10mo ago
say like I want to gather reports from 01/01/2024 to 31/01/2024 as CEST time
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
khamas
khamasOP10mo ago
and DTO can output me a DT as UTC anyways
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
khamas
khamasOP10mo ago
alright
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX10mo ago
TeBeCo
using System;

var rstTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Romance Standard Time");
var localTime = new TimeOnly(13, 30, 0);

Console.WriteLine(GetFormatedFutureLocatedDateTime(new DateOnly(2023, 01, 28), localTime , rstTimeZoneInfo ));
Console.WriteLine(GetFormatedFutureLocatedDateTime(new DateOnly(2023, 03, 28), localTime , rstTimeZoneInfo ));

string GetFormatedFutureLocatedDateTime(DateOnly localDate, TimeOnly localTime, TimeZoneInfo rstTimeZoneInfo)
{
var unknownDateTime = localDate.ToDateTime(localTime);

var rstOffset = rstTimeZoneInfo.GetUtcOffset(unknownDateTime);
var targetDateTimeOffset = new DateTimeOffset(unknownDateTime, rstOffset);

return targetDateTimeOffset.ToString("yyyy-MM-ddTHH:mm:ddK");
}
using System;

var rstTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Romance Standard Time");
var localTime = new TimeOnly(13, 30, 0);

Console.WriteLine(GetFormatedFutureLocatedDateTime(new DateOnly(2023, 01, 28), localTime , rstTimeZoneInfo ));
Console.WriteLine(GetFormatedFutureLocatedDateTime(new DateOnly(2023, 03, 28), localTime , rstTimeZoneInfo ));

string GetFormatedFutureLocatedDateTime(DateOnly localDate, TimeOnly localTime, TimeZoneInfo rstTimeZoneInfo)
{
var unknownDateTime = localDate.ToDateTime(localTime);

var rstOffset = rstTimeZoneInfo.GetUtcOffset(unknownDateTime);
var targetDateTimeOffset = new DateTimeOffset(unknownDateTime, rstOffset);

return targetDateTimeOffset.ToString("yyyy-MM-ddTHH:mm:ddK");
}
2023-01-28T13:30:28+01:00
2023-03-28T13:30:28+02:00
^
2023-01-28T13:30:28+01:00
2023-03-28T13:30:28+02:00
^
Quoted by
<@689473681302224947> from #web (click here)
React with ❌ to remove this embed.
khamas
khamasOP10mo ago
ok so I'll just create an offset based on a DateOnly
wasabi
wasabi10mo ago
Worht mentioning that offset isn't a timezone, for all of the pedantics out there. That's a lossy conversion going from one to the other.
Want results from more Discord servers?
Add your server