问题
I am working with nodejs. I am getting the startDate as
startDate: '2020-06-14T18:30:00.000Z'
from the client side. I need to get the timezone offset from the above date. It should be +05:30. How can I get that?
Edit:
Server always works on UTC. So can I get the timezone offset by subtracting the date coming from the client by the UTC one?
i.e.
'2020-06-14T18:30:00.000Z' - moment.utc().startOf("day") = +05:30
回答1:
If the node.js syntax is the same as plain JS; declare a date object with:
var date = new Date();
then use the getTimezonOffset method to get the difference in minutes to GMT.:
var difInMinutesToGMT = date.getTimezoneOffset();
From there on it's a matter of converting the positive/negative minutes into the format you want.
回答2:
You can use this below method in order to get the offset in the required format.
let d = '2020-06-14T18:30:00.000Z'
let date = new Date(d);
let offsetInMinutes = date.getTimezoneOffset()
let formattedOffset = `${parseInt(offsetInMinutes/60)}:${Math.abs(parseInt(offsetInMinutes%60))}`
console.log(formattedOffset)
回答3:
You cannot get the user's time zone.
User/Client sent you date-time in ISO8601 format, or to be more precise Zulu time (UTC).
You might consider 2 options:
- Forcing clients to send you time in a format which will include their timezone and grab it from there (
'2020-06-14T19:30:00.000+01:00') - more like a workaround - Or to have user preferences in you database where you will have their zone (somehow stored there, via dedicated endpoint or so...) - cleaner solution
Answering to edit:
moment(''2020-06-14T18:30:00.000Z').substract(moment.utc().startOf("day")).toHours() == 18.5
moment('2020-06-14T19:30:00.000+01:00').substract(moment.utc().startOf("day")).toHours() == 18.5
So, the timezone flag (Z, 01:00...) will just give info about timezone of given date-time. The client can send any of variation and it's valid. And in the end you still don't know clients timezone.
My 1st is just a workaround, but I wouldn't suggest you do that, that might be a hotfix, but 2nd is a more appropriate solution.
Anyway:
Sometimes, server/API/be doesn't have to know users timezone at all, if that is used just for read/write (because client app, will adjust ZULU time to the client zone in the UI and vice versa), but when you have some logic, for example google calendar features you will need user timezone for sure.
回答4:
the date you have is in universal central timezone UTC supposing you have that datestring on your client, you can use
from moment.js moment.utc(datestring).utcOffset()
to get the offset from that string, I think the return value is in minutes.
Edit: thank you for clarifying, the Z at the end of your datestring means that the date is already in UTC time zone. If you want to have a timezone offset, you will always get 0. you could possibly send as well the locale from the client using moment.tz.guess() and send the timezone as well to the server. or simply like others have stated, use on client side the Date.prototype.getTimezoneOffset method:
The getTimezoneOffset() method returns the time zone difference, in minutes, from current locale (host system settings) to UTC.
来源:https://stackoverflow.com/questions/62383797/how-to-get-timezone-offset-from-the-date-getting-from-client-side