Jump to content



Photo

[Javascript] Is British Summer Time in effect?

javascript british summer time bst daylight savings time dst

  • Please log in to reply
8 replies to this topic

#1 -Alex-

-Alex-

    Noob Hunter

  • Joined: 08-August 06
  • Location: Oslo, Norway

Posted 06 October 2011 - 13:38

Hi all,

So I'm scratching my head here... British Summer Time runs from the last Sunday of March til the last Sunday of October. My JS-skills are not uber-fantastic.

I don't suppose any of you have a pre-written function (or feel like having a go at writing a function) to get the user's current date, check if it's between the 2 above dates, and return 0 or 1?

I'm struggling here, and it'd be a huge help if someone could point me to a relevant link or an even bigger help if somebody feels like writing a quick function for me!

Thanks guys! :)


#2 Mattytommo

Mattytommo

    Neowinian

  • Joined: 01-September 10
  • Location: Middlesbrough, UK

Posted 06 October 2011 - 19:53

You can use the getTimezoneOffset() function in Javascript to determine what you need. The first line below gets the user's current date, the second line gets how offset they are to the current GMT :)

var d = new Date();
var offset = d.getTimezoneOffset();

#3 OP -Alex-

-Alex-

    Noob Hunter

  • Joined: 08-August 06
  • Location: Oslo, Norway

Posted 07 October 2011 - 12:22

Thanks for that, but that's not what I asked for ;)

The user could be 8 hours behind, 6 hours ahead, on GMT, I don't really care... I just need to know whether BST is in effect.

I'm almost done writing a Wordpress plugin, so I guess I'll have to have another go at writing this function myself when it's the last thing left to do...

#4 TheLegendOfMart

TheLegendOfMart

    Neowinian Senior

  • Joined: 01-October 01
  • Location: England

Posted 07 October 2011 - 12:24

Yes it is till Oct 30th 2am where the clocks will go back an hour.

#5 Sarm

Sarm

    Neowinian

  • Joined: 27-March 05
  • OS: Windows 8 64bit
  • Phone: Nexus 4

Posted 07 October 2011 - 12:34

Yes it is till Oct 30th 2am where the clocks will go back an hour.

LOL ... he read the title and not the question ;)

Try this:-
function dateWithin(beginDate,endDate,checkDate) {
var b,e,c;
b = Date.parse(beginDate);
e = Date.parse(endDate);
c = Date.parse(checkDate);
if((c <= e && c >= b)) {
return true;
}
return false;
}


You just need to hard code the start & end dates for BST.

#6 OP -Alex-

-Alex-

    Noob Hunter

  • Joined: 08-August 06
  • Location: Oslo, Norway

Posted 07 October 2011 - 12:49

You just need to hard code the start & end dates for BST.

Exactly, that's where I got stuck last time! :p There is no 'hard coded date' for when BST starts or ends. It's the last Sunday of March til the last Sunday of October :p

Thanks though, I appreciate the effort!

Edit: I think this is going to help me (merged with the above code), having a little go of it now: http://www.irt.org/articles/js050/

Edit 2: Actually, that isn't all of the code. The author's referencing numerous other bits of code, and some of it doesn't exist... i.e. that link doesn't give a complete working function.... I'm on the right lines though now at least!

#7 OP -Alex-

-Alex-

    Noob Hunter

  • Joined: 08-August 06
  • Location: Oslo, Norway

Posted 07 October 2011 - 14:07

Well in the end, I just sent the finished plugin to my client using GMT times. It's what he requested after all! No doubt he'll come back to me complaining that he needs support for BST added, and sadly, the bit of code it affects must be written in JS, and I don't like JS :p

Oh well, cheers for the help though guys! If anyone has a working solution though, feel free to post it up... and if my client comes back and moans, then I'll post the working solution here for everyone too! :)

#8 Sarm

Sarm

    Neowinian

  • Joined: 27-March 05
  • OS: Windows 8 64bit
  • Phone: Nexus 4

Posted 07 October 2011 - 15:11

This any help ?
http://www.michaelap...ime-dst-detect/

To be honest, I'd do it server side, but then I have the luxury of having my own server and BST is automatically set and the server clock is set to GMT or BST accordingly, I assume you can't do this using PHP/ASP etc?

#9 OP -Alex-

-Alex-

    Noob Hunter

  • Joined: 08-August 06
  • Location: Oslo, Norway

Posted 06 November 2011 - 16:16

Well as expected, my client eventually came back and asked for BST support to be added. So I went ahead and wrote the function. Feel free to scavenge, rip apart etc :)


<script>

/*
  A function that determines whether BST is currently in effect in London,
  based on the date on the user's PC, but regardless of whether the user
  is in London, or somewhere else. BST runs between the last Sunday of
  March, and the last Sunday of October, with a varying date each year.
*/
function isBSTinEffect()
{
var d = new Date();

// Loop over the 31 days of March for the current year
for(var i=31; i>0; i--)
{
var tmp = new Date(d.getFullYear(), 2, i);

// If it's Sunday
if(tmp.getDay() == 0)
{
// last Sunday of March
lSoM = tmp;

// And stop the loop
break;
}
}

// Loop over the 31 days of October for the current year
for(var i=31; i>0; i--)
{
var tmp = new Date(d.getFullYear(), 9, i);

// If it's Sunday
if(tmp.getDay() == 0)
{
// last Sunday of October
lSoO = tmp;

// And stop the loop
break;
}
}

// 0 = DST off (GMT)
// 1 = DST on  (BST)
if(d < lSoM || d > lSoO) return 0;
else return 1;
}

alert(isBSTinEffect());
</script>




Click here to login or here to register to remove this ad, it's free!