If I have a DateTime instance which represents a valid UTC time, and an offset that converts that DateTime to the time zone where it applies, how do I construct a DateTimeOffset instance to represent this?
var utcDateTime = new DateTime(2011, 02, 29, 12, 43, 0, /*DateTimeKind.Utc*/);
var localOffset = TimeSpan.FromHours(2.0);
var dto = ...
// Here the properties should be as follows;
// dto.UtcDateTime = 2011-02-29 12:43:00
// dto.LocalDateTime = 2011-02-29 14:43:00
Perhaps I'm not understanding the DateTimeOffset structure correctly, but I'm unable to get the expected output.
Thanks in advance
Looks like you want:
var utcDateTime = new DateTime(2012, 02, 29, 12, 43, 0, DateTimeKind.Utc);
var dto = new DateTimeOffset(utcDateTime).ToOffset(TimeSpan.FromHours(2));
Note that I changed the year from 2011 (which is not a leap year and does not have 29 days in February) to 2012.
Test:
Console.WriteLine("Utc = {0}, Original = {1}", dto.UtcDateTime, dto.DateTime);
Output:
Utc = 2/29/2012 12:43:00 PM, Original = 2/29/2012 2:43:00 PM
Do note that you probably don't want the LocalDateTime property, which may represent the instant in time as of the local system's timezone.
Related
I have date list in yyyy/mm/dd:
2020/06/10
2020/06/18
2020/07/17
and given date
2020/06/10
I want to find closest day in future from the given date in LINQ (expected result: 2020/06/18).
If you mean to find the closest date in future then you can filter out all earlier dates (including the same date), order it and then take the first value:
List<DateTime> allDates = new List<System.DateTime>()
{
new DateTime(2020, 06, 10),
new DateTime(2020, 06, 18),
new DateTime(2020, 07, 17),
};
DateTime givenDate = new DateTime(2020, 06, 10);
DateTime closestDateInFuture = allDates.Where(x => x > givenDate).OrderBy(x=> x).First();
Console.WriteLine(closestDateInFuture);
Output:
18.06.2020 00:00:00
Another suggestion by #Johnathan Barclay is to use the Min method, which yields the same result:
DateTime closestDateInFuture = allDates.Where(x => x > givenDate).Min()
When converting the current date and time, got from DateTime.Now, TimeZoneInfo.ConvertTimeToUtc(DateTime.Now) converts it correctly.
However, if a DateTime object is created, it does not convert it, it leaves it the same:
// get the local time as dd/mm/yyyy hh:mm:ss tt
DateTime dateTime = new DateTime(2020, 2, 5, 11, 59, 53, 0, DateTimeKind.Local); // tried with and without DateTimeKind specified
Console.WriteLine(dateTime.ToString());
// convert it to UTC
DateTime UTCdateTime = TimeZoneInfo.ConvertTimeToUtc(dateTime);
Console.WriteLine(UTCdateTime.ToString());
This prints:
2/5/2020 11:59:53 AM
2/5/2020 11:59:53 AM
The second timestamp should be UTC.
Why is this?
Note: the values plugged in above I was using to test. I need to get them from dateTimePicker controls.
var currentServerOffset = TimeZone.CurrentTimeZone.GetUtcOffset(new DateTime(1972, 03, 19, 02, 00, 00);
using the above returns the wrong offset, but when you pass through 26th March it is corrected.
it seems that this is off for a whole week, in my tests this has been in 1972 and 1973 ref: https://greenwichmeantime.com/info/calendars/bst-dates/
I am thinking it could be something to do with GetDaylightChanges being wrong in these years ref: https://learn.microsoft.com/en-us/dotnet/api/system.timezone.getdaylightchanges?view=netframework-4.8#remarks
as when i call this passing in 1972 or 1973 the start is off by 7 days.
How do i get around this?
We couldn't get GetUtcOffset to work with historic dates. Instead we used Noda Time which should be as easy as:
var dt = Instant.FromUtc(1972, 03, 19, 02, 00, 00);
DateTimeZone zone = DateTimeZoneProviders.Tzdb["Europe/London"];
Offset offset = zone.GetUtcOffset(dt);
I have a System.DateTime which is already in a specific timezone but does not specify a DateTime.Kind, and a string that represents an IANA timezone. I want to convert this to a NodaTime.ZonedDateTime.
For example:
var original = new DateTime(2016, 1, 1, 12, 0, 0);
var timezone = "America/Chicago";
I want a ZonedDateTime that represents 01/01/2016 - 12PM in Chicago time. How do I do this?
If I understand you correctly, you want to take a standard DateTime with a known time in Chicago and get a ZonedDateTime representing this Date + Time + Time Zone, correct?
If so, I believe this will do it. The intermediate conversion from DateTime to LocalDateTime might not be necessary if you can construct it directly.
var dotNetDateTime = new DateTime(2016, 1, 1, 12, 0, 0);
var localDate = LocalDateTime.FromDateTime(dotNetDateTime);
var chicagoTime = DateTimeZoneProviders.Tzdb["America/Chicago"];
var chicagoZonedDateTime = chicagoTime.AtStrictly(localDate); // Will contain 2016 01 01 noon
// now you can convert to time in Los Angeles
var laTime = DateTimeZoneProviders.Tzdb["America/Los_Angeles"];
var laZonedDateTime = chicagoZonedDateTime.WithZone(laTime); // will contain 2016 01 01 10am
I have a server running in New York that saves a DateTime into the database in local time.
I then have a client application running in GMT timezone that needs to save down a DateTime in the same local New York time
Can I do using:
TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"))
Or I need to consider daylight savings?
Now in my UI I need to display 2 columns. 1) the DateTime from the database and the DateTime in the database converted to GMT
How can I do this?
The TimeZoneInfo class should give you what you need.
var date = (DateTime)reader[0]; // Retrieve data from database however you normally get it
var est = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
var utcTime = TimeZoneInfo.ConvertTimeToUtc(date, est);
var localTime = utcTime.ToLocalTime();
Edit: You modified your question to indicate you have looked at TimeZoneInfo, so this doesn't add much anymore. However to resolve the ambiguous time issue mentioned by Jon, take a look at the IsAmbiguousTime() and GetAmbiguousTimeOffsets() methods to be able to detect values that can't be definitively converted.
Here's a sample to show that it should work as expected. That is, it will do the DST conversion.
DateTime dt1 = new DateTime(2016, 06, 01, 08, 00, 00, DateTimeKind.Unspecified); // Daylight saving time
DateTime dt2 = new DateTime(2016, 12, 01, 08, 00, 00, DateTimeKind.Unspecified); // Standard time
var dt1Converted = TimeZoneInfo.ConvertTimeToUtc(dt1,
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));
var dt2Converted = TimeZoneInfo.ConvertTimeToUtc(dt2,
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));
var diff1 = (dt1Converted - dt1).TotalHours;
var diff2 = (dt2Converted - dt2).TotalHours;
Console.WriteLine($"dt1: {dt1}, converted: {dt1Converted}, diff: {diff1}");
Console.WriteLine($"dt2: {dt2}, converted: {dt2Converted}, diff: {diff2}");
Output:
dt1: 2016-06-01 08:00:00, converted: 2016-06-01 12:00:00, diff: 4
dt2: 2016-12-01 08:00:00, converted: 2016-12-01 13:00:00, diff: 5
Is there any chance you can change this to use DateTimeOffset ? It was designed to resolve all of these problems