Question

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! :)

8 answers to this question

Recommended Posts

  • 0

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();

  • 0

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...

  • 0
  On 07/10/2011 at 12:24, TheLegendOfMart said:

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.

  • 0
  Quote
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!

  • 0

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! :)

  • 0

This any help ?

http://www.michaelapproved.com/articles/daylight-saving-time-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?

  • 0

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>
[/CODE]

This topic is now closed to further replies.
  • Posts

    • Occasionally crazy tyrants occupy palaces using swords.  
    • Switch to another OS if you're into tinfoil hat mentality, or just uninstall the app. Oh, wait, even after uninstalling it, can Microsoft still be looking at everything? We'll never know, so, yeah, that's that. If you use Windows, you need to live with those options.
    • Minecraft Chase the Skies update and Vibrant Visuals graphics overhaul lands next week by Pulasthi Ariyasinghe The second major game update of the year for Minecraft is almost here. Mojang today announced that its next Game Drop, Chase the Skies, is landing on June 17. Alongside it, the highly anticipated graphics overhaul, dubbed Vibrant Visuals, will be available too, making the day a rather big occasion for Minecraft fans. The Chase the Skies update has a large number of gameplay features and content, but one of the biggest is the expansion of the Ghast. Now, players will be able to find Happy Ghasts as a tameable mob, letting up to five players ride them on journeys across the skies. To get one, players will have to find small, dehydrated Ghasts near fossil structures in the Nether, then bring it to the overworld and feed it plenty of water. It is also possible to craft dried Ghasts from Nether materials. Other features of this update include a handy player locator bar, better leads functionality, craftable saddles, a new music disc, and more. As for the Vibrant Visuals upgrade, Mojang is touting a visual refresh that lets players witness how lights and shadows affect the Minecraft world. "Fly high with your happy ghast to watch the sunrise reflect on the rippling ocean, make art with shifting shadows, descend into dark depths, or simply sit back and enjoy the vivid beauty of each biome," says the studio. "With updated in-game visuals you’ll see how light and shadow transform the Overworld – water reflects, forests become shaded, fog drifts, and waterfalls glimmer in the light." The currently supported devices for Vibrant Visuals include Xbox Series X|S, Xbox One, PlayStation 4, PlayStation 5, PC, as well as Android (Android: Adreno 640, Mali-G68, Mali-G77, or Xclipse 530 or higher) and iOS (A12 or M1 or higher.)   Unfortunately, the Vibrant Visuals upgrade will only be available on the Bedrock Edition of Minecraft, at least for now. Mojang has said that it plans to bring the visual overhaul to the Java Edition of Minecraft at a later date, but no information or release window has been announced just yet.
    • Once people get use to the square-ish case, they'll probably switch the display to square as well.
  • Recent Achievements

    • One Month Later
      POR2GAL4EVER earned a badge
      One Month Later
    • One Year In
      Orpheus13 earned a badge
      One Year In
    • One Month Later
      Orpheus13 earned a badge
      One Month Later
    • Week One Done
      Orpheus13 earned a badge
      Week One Done
    • Week One Done
      serfegyed earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      560
    2. 2
      ATLien_0
      255
    3. 3
      +Edouard
      163
    4. 4
      +FloatingFatMan
      157
    5. 5
      Michael Scrip
      110
  • Tell a friend

    Love Neowin? Tell a friend!