✅ 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
leowest2y 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
khamasito 🇵🇸
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
leowest2y 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
Pobiega2y 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);
khamasito 🇵🇸
in DB and server side it would be UTC, just that the clients work on their own time zone
leowest
leowest2y ago
ok and what db is it?
khamasito 🇵🇸
postgres
leowest
leowest2y ago
so it uses DateTimeOffset
khamasito 🇵🇸
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
leowest2y ago
sure then u would just use TimeZoneInfo to offset it
khamasito 🇵🇸
say like I want to gather reports from 01/01/2024 to 31/01/2024 as CEST time
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
khamasito 🇵🇸
and DTO can output me a DT as UTC anyways
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
khamasito 🇵🇸
alright
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2y 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.
khamasito 🇵🇸
ok so I'll just create an offset based on a DateOnly
wasabi
wasabi2y 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.

Did you find this page helpful?