This code:
DateTimeOffset testDateAndTime =
new DateTimeOffset(2008, 5, 1, 8, 6, 32, new TimeSpan(1, 0, 0));
//CLEAN TIME AND DATE
testDateAndTime = testDateAndTime.DateTime.Date;
var datesTableEntry = db.DatesTable.First(dt => dt.Id == someTestId);
datesTableEntry.test= testDateAndTime;
db.SaveChangesAsync();
...produces this result in my database: 2008-05-01 00:00:00.0000000 -04:00
How should I revise my code so that it changes the time zone offset from -4:00
to +00:00
in testDateAndTime
?
I have also tried:
public Task<DateTimeOffset> SetTimeZoneOffsetToZero(DateTimeOffset dateTimeOffSetObj)
{
TimeSpan zeroOffsetTimeSpan = new TimeSpan(0, 0, 0, 0, 0);
return dateTimeOffSetObj.ToOffset(zeroOffsetTimeSpan);
}
...but that code doesn't do anything.
My end goal is just to have a date without a time or a time zone offset. I do not want to convert the time to another time zone. (That is, I don't want to subtract 4 hours from the 00:00:00.0000000
time and remove set time offset to +00:00
. I just want to set the offset to +00:00
.)
Here is another approach that I came across elsewhere:
DateTimeOffset testDateAndTime =
new DateTimeOffset(2008, 5, 1, 8, 6, 32, new TimeSpan(1, 0, 0));
testDateAndTime = testDateAndTime.DateTime.Date; //Zero out time portion
testDateAndTime = DateTime.SpecifyKind(
testDateAndTime.Date, DateTimeKind.Utc); //"Zero out" offset portion
I was sure that SpecifyKind
would convert my DateTimeOffset
. That is, change both the time and the time zone offset. But, my test indicates that this code just changes the time zone offset, which is what I want. Is there a problem with doing it this way?
test
property? What code are you using to retrieve the data where you see the-04:00
offset? Likely the offset is being shown because there's a conversion to the local time zone during retrieval, but you didn't show that part. An MCVE.would be helpful. – Minta