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/
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 have timestamp in milliseconds which I want to convert to human readable format as it is.But moment js convert the timestamp as per the server's timezone. In fact, the timestamp is already in UTC timezone only. moment js converts it again to UTC. How to inform moment js that the given timestamp is already in UTC and not convert it again based on server's timezone.
Please consider the given code:
moment(parseInt('1561407163043')).format("LLLL")
Use moment.utc
moment.utc(parseInt('1561407163043')).format("LLLL")
See https://momentjs.com/docs/#/parsing/ for more details
Sounds like you need to lock the offset to insure its the timezone you parse is on a GMT offset of your choice. If I understand correctly, I had a similar case where my resource had data on its server's time, but my client server had its own time and when I ran reports, I had to decide what time to use based on the logged time and my decided offset.
https://momentjs.com/docs/#/manipulating/utc-offset/
Here I got an input box on my web page for users to input a date. For some reason I can't use a datetime picker and I have to pass it to an .NET based service as a String via ajax.
Users may from different time zones. And the date is stored as UTC in database.
It seems I have 2 options to handle the timezone:
Convert the date string to UTC date string at frontend and pass to service.
Pass the UTC offset to service and convert the date string to UTC at backend.
However, neither option can handle the daylight savings time.
Could anyone give me some suggestions on this?
Javascript's toUTCString() and functions like getUTCDate() instead of getDate() will ignore the timezone offset, including I presume the DST offsets.
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().
I am trying to post a created date to an ASP.Net MVC controller without the date being modified by the serializer. I am looking for some kind of way to do this on the client.
The date is being constructed as follows:
var priceDate = new Date(name.split("-")[1], name.split("-")[0]-1, 1);
The date is valid and the problem is the serializer is adding the timezone offset which i don't want. The javascript date should be UTC but this doesn't seem possible.
This problem is expressed in different ways all over the place with all kinds of solutions that just seem way over the top. Is is possible to make a javascript date UTC or devoid of timezone information from the client?
There is toISOString() function which returns dates in standardized format and always does zero time zone.
Server accepts dates in local zone, so you need to do next date.ToUniversalTime().
So javascrript date.toISOString() equals to c# date.ToUniversalTime().