C
C#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
L
leowest44d 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
K
khamas44d 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
L
leowest44d 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
P
Pobiega44d 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);
K
khamas44d ago
in DB and server side it would be UTC, just that the clients work on their own time zone
L
leowest44d ago
ok and what db is it?
K
khamas44d ago
postgres
L
leowest44d ago
so it uses DateTimeOffset
K
khamas44d 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
L
leowest44d ago
sure then u would just use TimeZoneInfo to offset it
K
khamas44d ago
say like I want to gather reports from 01/01/2024 to 31/01/2024 as CEST time
UU
Unknown User44d ago
K
khamas44d ago
and DTO can output me a DT as UTC anyways
UU
Unknown User44d ago
K
khamas44d ago
alright
UU
Unknown User44d ago
M
MODiX44d 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.
K
khamas44d ago
ok so I'll just create an offset based on a DateOnly
W
wasabi40d 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
More Posts
Custom Newtonsoft JSON deserializationI have a dictionary where: - The key will always be a string - The value will be either: - string Issue with Form.Close() in WinformsI have a custom form called CustomMessageBox In that form, I have a method that defines a button clIssue with IMGUI.Net.Hello, I am creating an ImGUI application and It doesn't let me move the ui across my entire screen,Issue with deserializing Enums in saved JSONHey, I've been trying to deserialize a list of cards I serialized in C#, the issue I'm having is it From HttpClient, can I save/copy the request to share with someone?Is there a way to copy raw request details? For example, I'm looking for a similar output like: -3D Unity RaycastingHi guys, currently working on a puzzle game involving lasers and mirrors, however, have hit a brick Computer Science - Computer ArchitectureOn a 64 bit system (where the bus length and word size are 64 bits) how would we determine the ammouConnecting SQLite in MAUIHow to connect the database without absolute path? If with it, the program works only on the computeDifferent HttpClient headersWhen I make an HTTP request in my browser (Chrome), I get different response headers than I do in myHttpRequestException:An error occurred while sending the request. WebExceptionHttpRequestException:An error occurred while sending the request. WebException:Unable to connect to ✅ EF does not put datetimes properly to the datebaseI am writing a backend app in asp.net core. This is my service code : ```c# public virtual async TaShadow Prop Error with IdentityDbContextThis is my IdentityDbContext: public class ApplicationUser : IdentityUser { [Required(ErrorMessWpf custom button template probleHi! I am trying to create custom button control, but I have a problem with a content property. ButtoWinfoms problem in Hosts fileI am writing a mini site blocker, the program has 2 functions to block the site and add the site thaDTO validation with DRY principle (without repeating the same validation)Hello, I would like to know what is used in practice, regarding the DTO validation. Let's say for 2 I'm using fedora, and i want create some .NET formsWhat should i do to create projects with ASP.NET forms in Linux system(Fedora)?✅ My API returns 500 internal server error with the SQL exception text, why?Hello every one, I built an ASP.NET API in .NET and I wrote a controller Action that looks like thiAdvice on how to correctly integrate more complex Javascript codeHey, I've developed an ASP.NET app before, but that one was pretty much all about the backend and t✅ A generic error occurred in GDI+.' -> Save Image PathHey, I been trying to making some face recognition using EMGU.CV, but I faces a problem which, when Navigation and Dialogs with MVVM and WPFI'm using `CommunityToolkit.MVVM` which doesn't include pre-made classes for navigation and dialog i