meyerweb.com

Skip to: site navigation/presentation
Skip to: Thoughts From Eric

Hacking Weather Widget Hacking

John Gruber just posted a great article on how to take the Weather Dashboard widget in Tiger and hack it to add a “last updated” time.  It’s not only useful, but it’s also a wonderful introduction to the simplicity of widgets.  If you can hack on XHTML, CSS, and JavaScript—as I expect most visitors to this site can—then you can alter or create a Dashboard widget.

However, there was one thing I didn’t like about John’s hack: he converted the 24-hour time already stored by the widget into 12-hour AM/PM time.  I prefer 24-hour time, as do most people outside the United States (which I am not, but never mind that now), and sticking to 24-hour time makes the script addition even simpler.  So here’s my quick modification of John’s JavaScript to result in a time like “1450″ instead of “2:50 pm” or “0307″ instead of “3:07 am”.

// Format the time of the last data refresh
var h = object.time.hour;
var m = object.time.minute;
if (h < 10) {
   h = '0' + h;
}
if (m < 10) {
   m = '0' + m;
}
document.getElementById('updatetime').innerText =
   h + m;

Other than that, do everything just like John says to do.  Share and enjoy!

Five Responses»

    • #1
    • Comment
    • Mon 27 Jun 2005
    • 1934
    joshua wrote in to say...

    And if you prefer to leave in the “:” with the 24-hour display, change the last line to:

    h + ‘:’ + m;

    Anyway, thanks Eric (and John)!

    • #2
    • Comment
    • Tue 28 Jun 2005
    • 1522
    Neil M. wrote in to say...

    But does it work in metric time?

    • #3
    • Comment
    • Tue 5 Jul 2005
    • 1125
    Small Paul wrote in to say...

    I think most of us guys in the UK prefer 12 hour time too.

    • #4
    • Comment
    • Wed 6 Jul 2005
    • 1314
    John Hoare wrote in to say...

    I’m in the UK, and I much prefer the 24 hour clock.

    However, that’s because when I was younger I spent most of my time lying in front of the fire watching TV, and got my time from the VCR…

    • #5
    • Comment
    • Thu 28 Jul 2005
    • 1156
    Beau Hartshorne wrote in to say...

    Rather than display the date in 24-hour format, why not instead show something like “18 minutes ago”? Instructions and details here:
    http://hartshorne.ca/2005/07/25/more_weather_hacking/


June 2005
SMTWTFS
May July
 1234
567891011
12131415161718
19202122232425
2627282930  

Sidestep

Feeds

Extras