❔ DateTime?
I am trying to convert both of these dates to local time. It works CreationTimestamp but not for TransmissionTimestamp. The error given is something along the line of: "DateTime? doesnt have any reference to ToLocalTime".
9 Replies
DateTime
is a value type, so you need use TransmissionTimestamp.Value.ToLocalTime()
because DateTime?
is actually Nullable<DateTime>
however, DateTime
is a problematic data type to begin with, as it carries no real information about timezones, so you need to make a lot of assumptions about what timezones you are usingIt works AND I understand it. Thx thx thx
alr. Be careful 🙂
and prefer
DateTimeOffset
for all code where you can use it$datetime
$datetimelie
$datetimelie
please please please get into the habit of using DateTimeOffset instead of DateTime in your code everywhere you possibly can. DateTimeOffset contains the UTC offset with the data so is lossless. DateTime is very very weird in how it stores data. DateTime only knows UTC and "local" time. But your local time zone can change! Laptops easily move across state lines, even while an app is currently running. (So you are in California running an app, put some DateTime instance in memory, hop on a plane to New York, then resume your app 3 time zones ahead. What on earth will be contained within the DateTime instance?) But wait, this was a lie. DateTime actually has 3 ways it keeps track of time: UTC, local (which can change while an app is running), and unspecified (unknown, essentially). (Unknown = I don't know if it's UTC or local, the app will figure it out later and call ToUniversal or ToLocal before calling ToString or any comparison routine.) But wait, this was a lie. DateTime actually has a secret fourth way of storing data that's not exposed in the standard API surface! It's truly an abomination of a type. All of this nonsense is irrelevant if we just pretend DateTime doesn't exist and we instead use DateTimeOffset everywhere.- GrabYourPitchforks (start: https://discord.com/channels/143867839282020352/143867839282020352/988353756108312607)
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.