Check Day light saving time using moment.js

How to use moment.js for checking daylight saving time

I am using moment().isDST() but it is returning false always.

0 5 2,990
5 REPLIES 5

Hi

A few questions for you. 

1. Have you seen the momentjs docs page?  It includes these links:

2. Are you running this code within a JS callout within Apigee? 

3. Did you realize that moment() always returns the current time, and if the current time is not in DST, then ... isDST() will always return false, even if you wait a minute or an hour? 

1. I have seen the momentjs docs and I have used some of the functions but it is not helping me to check the DST time.

2. I am using Javascript policy within Apigee.

3. I just realize now about the isDST will always return false.

I wanted to know how can I check DST within Apigee using moment.js or  javascript code.

The question of whether DST is in effect is relative to a particular geography.  But Apigee systems that run in the cloud always use UTC.  They never are in "daylight savings time".  

What problem are you trying to solve? Step away from the momentjs and isDST, what is the real problem you are facing? 

 

I want to convert UTC time to EST time so i was checking isDST function in moment.js.

My requirement is to convert UTC timezone to EST. Day light saving is part of EST timezone so wanted to convert UTC to EST timezone accordingly.

OK, so the question "is it daylight savings time in EST?" is not the real question, I guess. 

The root question is "given a date (time), how can I convert it to EST?"  And by "convert" I assume you mean, to a human-readable string.  Is that right?  

In modern JavaScript (Node, or modern browsers), there is a method on the Date object, .toLocaleString(), which lets you convert an arbitrary date to an arbitrary timezone.  This works to give me "current time in EST": 

(new Date()).toLocaleString('en-US', { timeZone: 'America/New_York'}); 
// '4/29/2022, 11:15:03 AM'

That seems to solve your problem right there. It does not answer the question "is this date within DST in Eastern timezone?" But ... you don't NEED to answer that question in order to get a formatted time for Eastern timezone.  So that seems pretty neat and clean. But this JS  won't work in a JS Callout in Apigee.  

The JavaScript callout in Apigee depends on Rhino, which is not Node, and not v8.  It does have the .toLocaleString() method on the Date object, but Rhino's version of that method does not accept or use the options as described in the MDN documentation for the method. So you cannot use this simple clean approach in Rhino. 

You need something more capable that the JavaScript callout, something with better support for timezones.  You could do it in a simple Java callout.  I am not sure if you could do it in a Python callout. 

If you choose Java, the code is relatively simple. There are many suggestions and hints. Here's a good one. Which logic you choose depends on exactly what you want for input and output.