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/.
Related
I'm processing some data with dates in the format YYYY-MM-DD hh:mm:ss zz. For example:
2019-04-06 08:24:51 Central Daylight Time
2018-09-06 12:16:12 Central Standard Time
2020-02-14 17:57:33 Central Standard Time
I want to be able to convert these dates to a Date object in browser JavaScript. However, the Date constructor does not recognize this sort of date format, and moment.js isn't much help (unless I'm missing something).
In practice, I'll probably only ever deal with Central Standard Time and Central Daylight Time, but is there a general solution that would allow me to convert this date format to a Date object?
Assuming that Central Standard Time can also be written as CST, you can try
new Date('2020-02-14 17:57:33 CST'); // default date output
which will give you the full date representation.
Or you could use Date.parse() to get the actual timestamp
Date.parse('2020-02-14 17:57:33 CST'); // 1581724653000 etc
Another thought - have you tried Moment timezone?
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.
I have a legacy web app that stores dates as a UNIX timestamp (seconds since the epoch 1970). Usually a timestamp like this represents UTC, however these timestamps are UTC-8. It doesn't look like it ever accounts for Daylight Savings Time (DST). I could convert it on the server to UTC and send to the client, but I would like to know if there is a javascript only solution.
Example Input:
1399335987
Example Output:
"2014-05-05T16:26:27-07:00" // Pacific Daylight Time (PDT)
The client should display the date/time according to their local machine. I looked into using momentjs but I could not find how to construct a date from a number without the number being UTC already. Is this possible?
Yes, it's possible given the unix timestamps are in UTC, with Moment Timezone (http://momentjs.com/timezone/)
moment
.unix(1399335987)
.tz('MST')
.format('YYYY-MM-DDTHH:mm:ssZ');
And you get
"2014-05-05T17:26:27-07:00"
Note here I'm using MST, and you should be able to use whatever timezone you want, via Timezone Data Builder (http://momentjs.com/timezone/data/)
Actually, by default, moment parses and displays in local time.
This means, only if you're in a different timezone (offset really) and still want to get the local time in MST, it's necessary to set the timezone as MST.
Otherwise, moment.unix(1399335987).format('YYYY-MM-DDTHH:mm:ssZ') is good to go.
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/