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

    • Now you know how many would-be Windows 11 users felt. I’m not justifying any of it, but MS and Apple both do it.
    • Most of what you asked was in the article, what do you get for wasting my time? This is a “deal”’article, not a review of the machine.
    • An Air India plane with 242 people on board crashed Thursday near a major international airport in the western Indian city of Ahmedabad, the airline and the country's government said. https://www.nbcnews.com/world/asia/air-india-plane-crashes-indias-ahmedabad-airport-rcna212545
    • NTLite 2025.06.10473 by Razvan Serea NTLite is a Windows configuration tool that allows you to modify your existing Windows install or an image yet to be deployed, remove Windows components, configure and integrate, speed up the Windows deployment process. Reduce Windows footprint on your RAM and storage drive memory. Remove components of your choice, guarded by compatibility safety mechanisms, which speed up finding that sweet spot. Windows Unattended feature support, providing many commonly used options on a single page for easy setup. Easily integrate a single or multiple drivers, update or language packages. Package integration features smart sorting, enabling you to seamlessly add packages for integration and the tool will apply them in the appropriate order, keeping hotfix compatibility in check. One of the important new features of NTLite (compared to its predecessors) is the ability to modify an already installed the operating system, by removing unnecessary components. Supports Windows 11, 10, 8.1 and 7, x86 and x64, live and image. Server editions of the same versions, excluding support for component removals and feature configuration. ARM64 image support in the alpha stage. Does not support Checked/Debug, Embedded, IoT editions, nor Vista or XP. NTLite 2025.06.10473 changelog: Upgrade UI: Text size now set as host, more readable on some systems Post-setup: Loading progress improvement for the 99% delay Components: ‘Snipping tool’ compatibility option UI-Translation: Thanks for Romanian (Coman) Fix Updates: Parallel WinRE integration getting stuck Updates: Download overwrite previous Defender updates Settings: Win10 News widget disabling Download: NTLite 64-bit | 21.5 MB (Free, paid upgrade available) Download: NTLite 32-bit | 19.3 MB Link: NTLite Home Page | NTLite Features | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • You will be on builds 26100.4349 (24H2) No, some machines are on 26100.4351 updated 12 hours after the official release
  • Recent Achievements

    • Week One Done
      elsafaacompany earned a badge
      Week One Done
    • Week One Done
      Yianis earned a badge
      Week One Done
    • Veteran
      Travesty went up a rank
      Veteran
    • One Month Later
      somar86 earned a badge
      One Month Later
    • Week One Done
      somar86 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      507
    2. 2
      ATLien_0
      261
    3. 3
      +Edouard
      186
    4. 4
      +FloatingFatMan
      180
    5. 5
      snowy owl
      132
  • Tell a friend

    Love Neowin? Tell a friend!