What causes momentjs isDST() function to provide inaccurate results when used in conjunction with zone()?

I want to verify if daylight saving time is active by using the isDST() function. When I check it with the current date and time using var isdst = moment().isDST(), it works correctly for my timezone and returns true. However, I need to first set the timezone offset before checking if daylight saving time is active in that specific timezone. The code I have written below shows what I have tried:

var isdst = moment('2014-03-28').zone('+01:00');
console.log('daylight savings for +0100 is ' + isdst); //this returns true instead of false

For example, considering the DST time for timezone +0100 (European Union countries), it should only be in effect starting March 30, 2014. Strangely, the code above returns true on March 28 when it shouldn't until 2 days later. You can confirm this information by checking the DST timings for different countries here.

Upon further testing, I observed that the code seems to be somehow referencing my own timezone (US Eastern Standard) even though I'm setting a different zone. For instance, since Eastern Standard daylight saving begins on March 9, 2014, the code will return false a few days before that date (such as on March 8) and true afterwards (like on March 11). This indicates that it's not recognizing the zone("+0100") I specified and is using my local timezone instead. Why is this happening? How can I accurately set the timezone for a momentjs date? I checked the documentation (here) and followed the suggested method, but it doesn't seem to work as expected.

Answer №1

An offset and a time zone are not interchangeable terms. UTC+01:00 may be used in multiple time zones, some observe daylight saving time while others do not. Furthermore, even among those that do, the implementation of DST may vary. For more information, refer to the "Time zone != Offset" section on the timezone tag wiki.

The purpose of Moment's "zone" function is primarily for specifying an offset. It will still adhere to your local DST rules.

If you wish to understand the DST rules of another time zone, you must indicate that specific zone using the moment-timezone add-on. For instance:

var tz = 'Europe/Paris';           // or any other time zone
var dt = '2014-05-14 12:34:56';    // or any date/time under consideration
moment.tz(dt,tz).isDST()           // would return true in this scenario

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with displaying current date and time dynamically (JavaScript/JQuery)

In order to create a real-time chat using websockets, I want to show the time of a message posted in this format: "% sec/min/hour/day now". After conducting some research, I came across two functions that I have modified : // Get the TimeStamp va ...

Locating Elements in Protractor: Exploring Nested Elements within an Element that is Also a Parent Element Elsewhere on the Page

<div class="base-view app-loaded" data-ng-class="cssClass.appState"> <div class="ng-scope" data-ng-view=""> <div class="ng-scope" data-ng-include="'partial/navigation/navigation.tpl.html'"> <div class="feedback-ball feedback- ...

What is the best approach for scaling @material-ui Skeleton in a grid row with variable heights?

I am working on creating a grid of Avatar images with a transition state where I want to show a skeleton representation of the image. To achieve this, I am using @material-ui/lab/Skeleton. The issue I'm facing is that since my images are set to autos ...

Using iteration and conditional statements to verify the data type of an array

I'm attempting to check the array for the same data types using a for loop and if conditionals. However, my code doesn't seem to be working as expected. Can someone please help me figure out why? Thank you in advance! function checkDataTypes( ...

Tips for loading a repeater on a div scroll instead of relying on the browser window scroll

Currently, I have a repeater that loads records as I scroll down the main browser window. However, I am looking to enhance this functionality by adding a scrollable div and loading the repeater data as I scroll down that specific div, instead of the entire ...

CodeIgniter PHP Auto Suggest

I've been attempting to implement autocomplete search functionality, and despite adding all the necessary jQuery code, it still isn't functioning correctly. Here is the snippet from my View : <script type="text/javascript"> $(document).re ...

Utilizing a global variable within a JavaScript function to enable its usage across different PHP pages

In my script, there is a code snippet that sets different grid sizes based on user selection. The goal is to store this selection in a variable that can be accessed in PHP code on another page to tailor the output accordingly. After attempting to add a ne ...

The jQuery application adds new HTML elements to the page and then reveals the code that follows

When making an Ajax call and rendering two templates based on the response, I encountered a problem where the correct template renders but the script following it appears on the page as plain text. Here's the code snippet with the issue: if (question ...

Encountering difficulty when trying to parse an HTML file as JSON using AJAX, resulting in an 'unexpected token' error

I have a Siemens s7 1200 PLC with a webpage on it that is structured like a json file (even though it is in html format to allow the PLC to modify variables). I am attempting to retrieve this data from an external web server using an ajax request. The only ...

Having trouble with jQuery's $.when.apply() function not firing the .done

I've encountered an issue with my code where the $.when.apply($, promises).done() function is not working as expected (I have console logging to track progress). I'm puzzled by why the .done function is not functioning properly. The code essent ...

Show the input from one HTML form on a different HTML form

Seeking guidance on utilizing local storage in HTML5. How can I extract information from one form and present it on another HTML form page? Picture this: two forms at play here. Upon populating form 1 and hitting the submit button, the content should be v ...

Grouping an array of dynamic objects by a property value in react js / javascript: A step-by-step guide

There is an object in the following format: [{"genericName":"genesName test","genericId":3,"code":"generics"},{"genericName":"genesName fhfghsd","genericId":8,"code": ...

The dojo component failed to be parsed after reloading jQuery

Hi everyone! I'm currently working on a project that involves using a DOJO tabbed pane to display data from a database. To keep the data in the tabbed pane up-to-date, I need to refresh a specific part of the page (a div with the id "refreshable" that ...

The usage of arrow functions in ReactJS programming

I'm working on a React component that has the following structure: import React, { PropTypes, Component } from 'react'; import { Accordion, Panel, PanelGroup, Table } from 'react-bootstrap'; const FormCell = ({ data }) => ( ...

Prevent jQuery toggle from animating content when toggling

When I use a simple jQuery .toggle function to show/hide my content on click, it slides in from left to right before reaching its final position. Is there a way to prevent this sliding effect? Is there a different jQuery method or property that I should be ...

Exiting a void method in JavaScript/Typescript using the return or break statement

I find myself dealing with a complex method in Typescript that contains multiple if else if else constructs. It's a void method, and I'm wondering how I can exit the method based on a specific if condition without having to execute the remaining ...

Tips for searching a text across various URLs

For a while now, I've been grappling with a challenge that I just can't seem to solve. I created a test with multiple YouTube URLs generated using random strings, following the format "https://www.youtube.com/watch?v=XXX" where XXX represents the ...

Unable to deactivate button using JavaScript following an AJAX request

Within my JS file, I am attempting to disable a button after making an AJAX call: $('document').ready(function() { const runField = $('input[id=run]') const runButton = $('.btn-run') const saveButton = $('.btn-save ...

Tips for determining the longest and shortest values for a field in a mongoDb database

In mongoDB, I am interested in determining the length of both the longest and shortest values for a field with a String data type. My collection contains a total of 500,000 documents. ...

Modify the css based on the user's input

<html lang="en"> <head> <link rel="stylesheet" href="style.css" /> <li id="visa"> <section class="credit-card visa gr-visa"> <div class="logo">visa</div> <form> <h2>Payment ...