Javascript Send Schedule Date From Local Timezone To Server Time
Solution 1:
If I understand correctly, the problem is that your current approach on the client-side is just creating a Date
object like new Date("2021-03-17T16:30:00")
, which is going to use the local time zone to determine the point-in time you're talking about. You want it to always be interpreted as Pacific time, but you don't know which offset to use because of DST.
So then, the ultimate answer to your question is - YES, the code you wrote using Moment-timezone, would indeed apply the correct offset. However, it's a bit verbose. You don't actually need to do anything with regard to detecting the current local time zone. You can instead just build the Date
object with:
moment.tz('2021-03-17T16:30:00', 'America/Los_Angeles').toDate()
Given Moment's project status, it would be preferred to use Luxon instead (unless you have already extensively used Moment in your project). In Luxon, that would be:
luxon.DateTime.fromISO('2021-03-17T16:30:00', {zone: 'America/Los_Angeles'}).toJSDate()
Or you could use the zonedTimeToUtc
function from date-fns-tz.
The point is, since you're constructing a Date
object, you're always sending some string representation of it over the wire. You appear to be serializing those Date
objects with .toISOString()
, which will send the UTC equivalent "2021-03-18T02:30:00.000Z"
. Getting that UTC point in time correct is what matters most.
In your .NET code, if you are receiving that value into a DateTime
object, then its Kind
property will be set to Utc
because of the Z
at the end of the string. TimeZoneInfo.ConvertTime
will use the Kind
property to determine the source time zone for the conversion, and you've supplied the destination time zone as Pacific time.
A much simpler approach would be to not use a Date
object in your client-side code, but rather you would send the intended date and time over the wire instead, without the offset "2021-03-17T16:30:00"
. In your .NET code, the DateTime.Kind
would be Unspecified
. Then you wouldn't call TimeZoneInfo.ConvertTime
at all, because you already have the value in the desired time zone.
Alternatives:
For the TypeScript data type, You could use a string.
In the future, when Temporal is finished and fully integrated into ECMAScript, you could use a PlainDateTime
.
Post a Comment for "Javascript Send Schedule Date From Local Timezone To Server Time"