Convert US Central Time to different time zones using moment JS - timezone

I have a Date Time(Friday, 27 October 2017 4:00:00 AM) in US Central Time zone (CDT). I want to convert this Date Time into different time zones. These are time zones i wanted to convert.
Eastern Time (EDT)
Pacific Time (PDT)
New Delhi, India (IST)
Central Europian Time (CET)
Riyadh, Saudi Arabia (AST)
Pakistan Standard Time (PKT)
Lagos, Nigeria (WAT)
Australian Standard Time (AET)
Greenwich Mean Time (GMT)
Moscow, Russia (MSK)
China Standard Time (CST)
This is how i am doing
var dateTime = moment.tz("2017-10-27 4:00:00 AM", "America/Chicago");
var IST = dateTime.tz('Asia/Calcutta').format('MMMM Do YYYY, h:mm:ss a');
console.log(IST) // October 27th 2017, 9:30:00 am
The returned Date Time is wrong. Because Indian Standard Time is 10 hours and 30 minutes ahead of Central Time.
It should be Friday, 27 October 2017 2:30 PM (IST)
Thanks!

The problem isn't with the conversion to the Indian time zone - it's the original parsing of the Chicago time.
This:
var dateTime = moment.tz("2017-10-27 4:00:00 AM", "America/Chicago");
... is treated as 4am UTC, and then converted to America/Chicago, so it ends up representing 11pm local time (on October 26th) in Chicago. You can see that by just logging the value of dateTime.
If you change the code to:
var dateTime = moment.tz("2017-10-27 04:00:00", "America/Chicago");
... then it's treated as 4am local time on the 27th, which is what I believe you expected. The result of the conversion to Asia/Calcutta is then 2:30pm as you expected.
So either change the format of your input, or specify that format. For example, this works fine too:
var dateTime = moment.tz("2017-10-27 4:00:00 AM", "YYYY-MM-DD h:mm:ss a", "America/Chicago");

Related

How to display timezones abbreviations?

I'm looking at the timezones and trying to figure out abbreviations. I'm using list of timezones and offsets to display them on my website.
Client asked to add abbreviation for each timezone. Once I listed abbreviations I realised that there are the same timezone names for different abbreviations such as:
'edt'
'offset' = -14400
'timezone_id' = 'America/New_York'
'ept'
'offset' = -14400
'timezone_id' = 'America/New_York'
'est'
'offset' = -18000
'timezone_id' = 'America/New_York'
'ewt'
'offset' = -14400
'timezone_id' = 'America/New_York'
Can someone please explain what is that exactly and how do I know which abbreviation to display?
US Timezones
If you're working with US Timezones they are broken down into 4 Timezones that cover the Mainland US (Except Alaska and Hawaii)
EST EASTERN STANDARD TIME UTC - 5
EDT EASTERN DAYLIGHT TIME UTC - 4
CST CENTRAL STANDARD TIME UTC - 6
CDT CENTRAL DAYLIGHT TIME UTC - 5
MST MOUNTAIN STANDARD TIME UTC - 7
MDT MOUNTAIN DAYLIGHT TIME UTC - 6
PST PACIFIC STANDARD TIME UTC - 8
PDT PACIFIC DAYLIGHT TIME UTC - 7
So you'd be using EST,CST,MST and PST for the Mainland US anyway. Hawaii and Alaska have different Timezones and off the top of my head am not sure exactly what they are

moment-timezone format doesn't return the expected result

Using the tz() function from moment-timezone as follow:
moment.tz('2017-10-15 13:53:43','Asia/Hong_Kong').format()
//returns '2017-10-15T13:53:43+08:00'
moment.tz('2017-10-15 13:53:43','Asia/Hong_Kong').format('h:m A')
//I expect to return '9:53 PM' but it returns '1:53 PM'
Ultimately, I want to apply the fromNow() function to format the result. But when I apply it, it uses the initial timestamp and ignore the timezone applied.
moment.tz('2017-10-15 13:53:43','Asia/Hong_Kong').fromNow()
//I expect to return '1 min ago' when actual time is 13:54 UTC (21:54 in HK) but it returns '8 hours ago'
What am I doing wrong here?
When you do:
moment.tz('2017-10-15 13:53:43','Asia/Hong_Kong');
You're creating a date/time that corresponds to October 15th 2017, at 1:53 PM in Hong Kong - which, in turn, corresponds to 2017-10-15T05:53:43Z (5:53 AM in UTC).
When you call the format() function:
moment.tz('2017-10-15 13:53:43','Asia/Hong_Kong').format();
It returns:
2017-10-15T13:53:43+08:00
The +08:00 part is just the UTC offset - it just tells that Hong Kong is 8 hours ahead UTC. But 2017-10-15T13:53:43+08:00 (1:53 PM in Hong Kong) is exactly the same instant as 2017-10-15T05:53:43Z (5:53 AM in UTC). That's why fromNow(), when the current time is 13:54 UTC, returns 8 hours.
If you want the date/time that corresponds to 1:53 PM in UTC, you should use the utc() function:
// October 15th 2017, 1:53 PM in UTC
moment.utc('2017-10-15 13:53:43');
Now, when the current time is 13:54 UTC, fromNow() will return 1 minute (because the date/time represents 1:53 PM in UTC).
To convert this to Hong Kong timezone, just use the tz() function:
// convert 1:53 PM UTC to Hong Kong timezone (9:53 PM)
moment.utc('2017-10-15 13:53:43').tz('Asia/Hong_Kong').format('h:m A');
This will convert 1:53 PM UTC to Hong Kong timezone (resulting in 9:53 PM):

Timezone issue in Blackberry

I am working on a project which requires me to create a Date value by setting it to a particular timezone .
I have used the following code
Date formatter1 = new Date(HttpDateParser.parse("2013-08-02 11:00:00"));
System.out.println("Date dd formatter1"+formatter1);
Result as follows:
Fri Aug 02 16:30:00 Asia/Calcutta 2013
After parsing, it is giving me time according to device time zone ...
adding 5:30 automatically if device time zone is set to India Kolkata.
I want result to be as :
Fri Aug 02 11:00:00 Asia/Calcutta 2013
I mean it should not add the offset as reference to GMT .
How could I work upon this code to set the data required to me as per the Timezone and not change it internally ?
One problem is that your original date string:
2013-08-02 11:00:00
does not include time zone information. So, it is being interpreted as a GMT time. Which then means that, displayed in Calcutta time, it will be
Fri Aug 02 16:30:00 Asia/Calcutta 2013
You want to specify that 11:00 is already in Calcutta time. To do that, use one of the formats defined in the HttpDateParser documentation:
// we make sure to specify time zone information "+05:30"!
long timeSinceEpoch = HttpDateParser.parse("2013-08-02T11:00:00+05:30");
Date date = new Date(timeSinceEpoch);
System.out.println("Date: " + date);
// use this to slightly change the date formatting ... same time zone
String pattern = "yyyy-MM-dd hh:mma";
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
String formattedDate = formatter.formatLocal(timeSinceEpoch);
System.out.println("SimpleDateFormat: " + formattedDate);
Note: that in addition to adding "+5:30" to the time string, you have to replace a space after the date with a 'T'.
This code will output:
[0.0] Date: Fri Aug 02 11:00:00 Asia/Calcutta 2013
[0.0] SimpleDateFormat: 2013-08-02 11:00am
if the device's time zone is actually set to Calcutta (Kolkata +5.5).
References
Have a look at this answer on Stack Overflow.
and maybe this one, too.

Timezone issues with timestamps in the FQL table 'event'

I'm having trouble with event timezones in the start_time and end_time fields in the event FQL table. I want to show the timestamp in a formatted date that accounts for the user timezone. Empirically, it appears the timestamps are in the PST timezone, so I convert them to GMT and add the user timezone, as contained in the timezone field in the user table. Conversion to GMT is performed by Java, with the following code (this should account for daylight saving time automatically):
long millis = 1000 * timestamp;
TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
int offset = tz.getOffset(millis);
long timestampGMT = millis + offset;
However, this approach produces different results than the Facebook site.
For example I have an event with start_time=1317416400 and a user with timezone=2. Converting to GMT and adding the user timezone I get September 30th 2011, 16:00:00, but the Facebook site shows September 30th 2011, 14:00:00.
How can I obtain the correct (as in FB site) result?
Examples:
Summer:
http://www.facebook.com/event.php?eid=234094353309432
Created by a user on GMT+2
Time shown on the site: Friday, September 30 · 2:00pm - 7:00pm
start_time (from FQL): 1317416400
If it was UTC, then: Friday, September 30th 2011, 21:00:00 (GMT)
If it was PDT (with DST, so GMT-7), then: Friday, September 30th 2011, 14:00:00 (GMT-7)
Winter:
http://www.facebook.com/event.php?eid=254174591293234
Created by a user on GMT+2
Time shown on the site: Sunday, December 25 · 1:00pm - 4:00pm
start_time (from FQL): 1324846800
If it was UTC, then: Sunday, December 25th 2011, 21:00:00 (GMT)
If it was PST (no DST, so GMT-8), then: Sunday, December 25th 2011, 13:00:00 (GMT-8)
Thanks

Quartz .NET MakeDailyTrigger

I am using Quartz framework, got bit confused with time generation. This is a simple code which generates daily trigger # 11:30 am. To test this out, i generated next 100 consecutive firing time using ComputeFireTimes query but time i get is wierd. May be i m missing something here.
Trigger trig = TriggerUtils.MakeDailyTrigger(11, 30);
var triggerList = TriggerUtils.ComputeFireTimes(trig, null, 100);
foreach (DateTime trigger in triggerList)
{
Console.WriteLine(trigger.ToString());
}
The output i get is
8/12/2011 3:30:00 PM
8/13/2011 3:30:00 PM
8/14/2011 3:30:00 PM
8/15/2011 3:30:00 PM
8/16/2011 3:30:00 PM
8/17/2011 3:30:00 PM
8/18/2011 3:30:00 PM
8/19/2011 3:30:00 PM
The time should have been 11:30 a.m. but it is showing 3:30 pm.
These are UTC (GMT) times, maybe your time zone is 4 hours different
from UTC? You would need to change the display to your local time zone
by calling Console.WriteLine(trigger.ToLocalTime().ToString());

Resources