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
Related
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.
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.
I've tried searching for this but it's a pretty specific question. I have a React application where users will be looking at a calendar that can be switch between multiple timezones. I'm using Moment with it's Timezone plugin to try and standardize these times as much as possible.
There is a use case with this application where, for example, a user may be in the US/Eastern (UTC -05:00) timezone viewing a calendar that is set in the US/Mountain (UTC -07:00) timezone.
Right now, I'm keeping my dates in UTC to try and minimize the confusion, however I will need this user, in the Eastern timezone, to be able to see the date in the Mountain timezone. For instance, clicking on a calendar appointment, which is set in Mountain time, will display at 3pm MT. However, right now, when I take the UTC date, it's being converted into the user's local time. A 3pm MT appointment is displaying as 5pm ET.
// I'd like it to display 2017-12-20T15:00:00-07.00 to the end user
moment('2017-12-20T22:00:00Z').format() // returns 2017-12-20T17:00:00-05.00
My question is, using the calendar offset (UTC -07:00), how can I display that time in MT as opposed to the user's local time using Moment and Moment Timezone? I don't have access to any other timezone information, besides the offset. I'm tried going through Moment Timezone's docs but it seems I need a name to create the time in a timezone different from the user's local timezone.
To use moment-timezone, you will need moment#2.9.0+, moment-timezone.js, and the moment-timezone data.
I think this will helps you.
https://momentjs.com/timezone/docs/
i am using momentJS library for timezone conversion logic in javascript. i am getting User Preference Timezone abbreviation value from the web service response. I need to convert date using Timezone abbreviation but it is not working for certain timezone.
var Date = moment(dateObject).tz("CST").format(getDateFormat.defaultDateFormat());
Is there any way to convert a date using Timezone abbreviation in javascript?
Note: Need to convert date using Timezone abbreviation and It should also handle daylight saving time (DST)
Appreciate for your help.
This is not possible with moment library. You will need full timezone name e.g. America/Chicago , while converting the date.
If you use abbreviation, you will get error : Moment Timezone has no data for CST. See http://momentjs.com/timezone/docs/#/data-loading/.
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/