I want to set current time zone with moment.js for user that runs my app. My problem is that many of momentjs question posted here have method called tz but I can't see it now in 2020.
Format date in a specific timezone
With that method you can set this:
moment().tz('America/Phoeinx')
But I don't want to set this manually. I found utc method but it returns me timezone +0000.
How to set current time zone with momentjs in 2020?
Ok I found an answer, all that we need is just get current utcOffset from momentjs library:
const currentUtcOffset = moment().utcOffset()
and pass it to utc method
moment(date).utc(currentUtcOffset).toISOString()
Now everything works perfect.
Related
I was developing a timetable using dayjs. I would like to set the timetable to show something likes UTC-9 date regardless of user's local time. I tried with dayjs's timezone and utc mode. Both change the time to the correct time but not the date. If I set PC time to other date, dayjs still showing that date instead of UTC-9 date. Thanks.
There is a nice example in the day.js documentation itself.
dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.tz("2014-06-01 12:00", "America/New_York")
dayjs("2014-06-01 12:00").tz("America/New_York")
day.js timezone documentation
I am working on customizing primeng component for my requirement I came across a problem where I had to select a time and also select the time zone it should not affect the time selected but has to update the timezone as shown in the picture
I want to update only the timezone which is GMT to UTC as per selection
is there any way to update timezone without changing the date I tried finding a solution but failed please help on the problem
NOTE: I have customized the TIMEZONE selection
you can use toISOString() of basic JS date object (check for date docs) and then change it as you want.
But I recommend you to use external library (if possible with immutable instances of datetime) even if you do not prefer to. Especially when your application will be strongly dependent on resolving multiple timezones.
What about setUTCHours()?
let today = new Date(Date.now());
console.log(today.toISOString());
today.setUTCHours(10);
console.log(today.toISOString());
Also using new Date(Date.UTC(year, month, day, hour, minute, second)) you can create a new date object from a specific UTC time.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC
This article can help you as well.
When I execute
myDate = new Date('2000-02-02 12:30:00')
I get a date object like this 2000-02-02T11:30:00.000Z because there is a difference of one hour between my timezone (Europe/Vienna) and UTC.
I can now change the hour by doing
myDate.setHours(10)
and the result will be a date object like this 2000-02-02T09:30:00.000Z because of the one hour difference.
I can also set the UTC hours by
myDate.setUTCHours(10)
to get a dateobject like this 2000-02-02T10:30:00.000Z
I'm looking for something similar to
myDate.setLocaleHours(10, "America/New_York")
(which doesn't exist)
What is the best way to set the hours to a specific value in a timezone which is not my current one and also not UTC?
How can I setLocaleHours() on a date object?
What is the best way to set the hours to a specific value in a timezone which is not my current one and also not UTC?
You can't. At least, not on the Date object. It has no ability to set time based on an arbitrary time zone.
There is work in progress to rectify this, by adding a new set of standard objects to ECMAScript. See the TC39 Temporal proposal for more details. The temporal ZonedInstant will have functionality to work with named time zones.
However, for now, you will need a library that understands time zones. Moment-timezone is one option, though, these days the Moment team recommends Luxon for modern app development. Another great option is js-Joda.
Say I had an ISO date string with embedded timezone information, like "2016-08-22T13:30:00-07:00" (Here, the -07:00 specifies PST). I'm looking for an elegant way to determine what day of the week it will be in that timezone on that date and time. I've tried moment.parseZone(datestring).weekday() to no avail. How can I make moment think in terms of the timezone specified in the string, instead of wherever the server happens to be?
You can try moment(datestring).utcOffset(datestring).weekday(), check the docs for more explanation. Also there is a part of the documntation on weekday which describes it as locale aware I am not sure if it maintains the time zone of the date while converting or defaults to your local/server location, I usually uses moment.day(), if it suits your use case, you can use it instead of weekday().
We are displaying schedules on our webpage which is build on GWT. Client system using different timezone from server and because of that, all the schedules were displaying wrong. Is there a way to set default time zone when we load the page? Like the way we do it in java:
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Kolkata"));
Thanks!!!
No, you can't set the timezone of Date objects in javascript. Usually you use only UTC and epoch-based timestamps.
Only when creating a Date from a string or from year, month etc. the local timezone will be used, you can only get the timezone offset.
Converting a timezone can only be done by re-setting the Hours of the Date object (example described here), creating a date which looks-like having an offset timezone but is just utc.
In case you are using moment.js for your dates, you can set the default timezone for all newly created moments with:
moment.tz.setDefault(String)
https://momentjs.com/timezone/docs/#/using-timezones/default-timezone/