1

I am developing an web based interface for users that are based in UK. Currently we are saving times in GMT in our data base. But on the front end the user is entering UK date time that needs to converted to GMT. But the problem comes in conversion.

So what I want to do is develop a function in Javascript that determines on a given date(UK time zone) if daylight saving was on or not.

I am currently working with moment.js liberary but have no idea how to calculate it. Any help would be much appreciated.

umair.ashfr
  • 1,171
  • 13
  • 32
  • 2
    did you look at isDST from momentjs. I thought it is straightforward and easy to use. – D. Seah Jul 15 '18 at 06:16
  • I did take a look. But the problem is I am working from a place outside UK. Unless there is a way to set my default time zone to UK(I guess currently moment.js takes the OS time as default time zone. ) isDST doesn't return it for UK. – umair.ashfr Jul 15 '18 at 06:51
  • @Emissary, I did take a look at that thread. May be I really don't understand DST. But again, please correct me if I am wrong. Those functions would only work for browsers current time zone and not UK. My whole problem is that I want same type of function which doesn't return DST for browsers time zone but for UK – umair.ashfr Jul 15 '18 at 07:35
  • @umair.ashfr, Did you find any solution for this? – BVS Oct 28 '19 at 14:58
  • @BVS, the solution I worked with was to determine DST on server side rather than client side. – umair.ashfr Oct 28 '19 at 15:17
  • @umair.ashfr, oh Okay, did you see any difficulty in implementing the same logic at client side? Could you please share the logic implemented at server side for reference? – BVS Oct 28 '19 at 15:27

2 Answers2

0

I have an npm module of my own that does this, but I can't recommend it since I've never gotten around to documenting it very much, so it's useful to no one but me for now.

Here's one approach you can take, however, using moment.js:

1) Get the time that midnight at the start of the day in question occurs.

2) Get the time that midnight of the next day occurs.

3) Subtract the first time from the second time. If it's less than 24 hours, that's when the clock was turned forward, and DST began. If it's less than 24 hours, that's when the clock was turned back, and DST ended. Anything else is an ordinary day.

Update:

The other answer provides a good way to determine if DST is being observed at a particular moment in time (not quite perfect in a few odd cases). I thought you were getting at a different question -- whether or not the clocks got changed on a particular date. In case that matters to you, it can be done this way:

function checkDate(year, month, day) { // Month from 1-12, not the weird 0-11 month
  const startOfDay = +new Date(year, month - 1, day, 0, 0, 0, 0);
  const endOfDay = +new Date(year, month - 1, day + 1, 0, 0, 0, 0);
  const hours = (endOfDay - startOfDay) / 1000 / 3600;

  if (hours < 24)
    console.log('DST started');
  else if (hours > 24)
    console.log('DST ended');
  else
    console.log('No DST change');
}
kshetline
  • 12,547
  • 4
  • 37
  • 73
0

you don't need to use momentjs for this. you can use below code

Date.prototype.stdTimezoneOffset = function () {
var jan = new Date(this.getFullYear(), 0, 1);
var jul = new Date(this.getFullYear(), 6, 1);
return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
}  

Date.prototype.isDstObserved = function () {
return this.getTimezoneOffset() < this.stdTimezoneOffset();
}

var today = new Date();
if (today.isDstObserved()) { 
alert ("Daylight saving time!");
}

see How to check if the DST (Daylight Saving Time) in javascript for more details

happy coding

Code_Worm
  • 4,069
  • 2
  • 30
  • 35
  • thanks for the answer, I did take a look at it. But I have a question(may be I am still confused about this DST concept). Are the above functions valid for UK timezone. By this I mean, if my browser is outside UK(lets say India), How do I set the timezone to UK. It is really messing up my mind. – umair.ashfr Jul 15 '18 at 07:17
  • 1
    JavaScript only deals natively with either UTC (not quite UK time, since UTC has no DST) or the one local timezone of the browser. You definitely need something like moment.js if you want to display UK time for someone in New York or Tokyo. – kshetline Jul 15 '18 at 08:05