How to correct Timezone offset for the future dates that falls under Daylight Saving Time

I’m trying to schedule an event on 02-May-2021 1:30 PM which falls in Central Daylight Time and I’m scheduling it Today 25-FEB-2021 which is Central Standard Time. but the time 1:30 PM shifts to 12:30 PM.

Is there any way to prevent this shift? My Application is in angular Js and using Bootstrap-angular Datetime picker

Answer

You can use moment-timezone https://github.com/moment/moment-timezone you can add the offset at your date based of timezone, ici +01:00 in France.

<script type="text/javascript" src="./moment-timezone-with-data.js"></script>

let offsetFrance = moment().tz("Europe/Paris").format('Z'); //+01:00
let eventWithOffset = moment().format('YYYY-MM-DDTHH:mm:ss[' + offsetFrance + ']');
let eventGmt = moment.tz(eventWithOffset, 'Europe/Paris').toISOString();

Otherwise try with moment in app angularjs https://raw.githubusercontent.com/gdi2290/angular-momentjs/master/angular-momentjs.js

Load the script

<script type="text/javascript" src="./angular-momentjs.js"></script>

Inject dependance in you app module and controller

var app = angular.module('myApp', ['angular-momentjs']);

app.controller('myCtrl', function($scope, $moment) {
  let now = $moment().toISOString();
  let myEvent = $moment('02/05/2021 13:30', 'DD/MM/YYYY HH:mm').toISOString();

  if($moment(now).isAfter(myEvent)) alert('Time to event');
  else alert('ok');
});

Leave a Reply

Your email address will not be published. Required fields are marked *