开发者

Parse time data from HTML using jQuery

I've got a javascript widget on my page that outputs times for Sunrise and S开发者_运维技巧unset. The following code is what gets output (assume that these times change every day). I have no control over the output from the widget.

<div id="sun_container">
    <div class="details">
        Sunrise: <strong>7:00AM</strong> |
        Sunset: <strong>4:30PM</strong>
    </div>
</div>

Elsewhere on my page I have an image tag that looks like this:

<div id="sun_button">
    <img src="images/day.png">
</div>

I want to parse out the sunrise time and the sunset time and compare these against the current server time (which I can output via PHP if necessary).

If the current server time is not between the sunrise and sunset times, then I want to change the image src to "images/night.png".

Any ideas on how I could do this?

EDIT: I'm now outputting the server time in the page <head> using:

var server_time = "<?=date("H:i:s", time())?>";

which outputs something like this:

var server_time = "17:07:41";


You can do it very easily with the DateJS JavaScript library:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="http://www.datejs.com/build/date.js"></script>
<script type="text/javascript">
$(document).ready(function(){
 var sunrise = Date.parse($(".details strong:eq(0)").text());
 var sunset = Date.parse($(".details strong:eq(1)").text());
    if(Date.parse("" + server_time).between(sunrise, sunset)) { 
       $("img").attr("src","images/day.png")
    }
    else {
       $("img").attr("src","images/night.png")
    }
});
</script>

Try it here!


I think you're looking for this post: What is the best way to parse a time into a Date object from user input in Javascript?

Same premise, it's javascript, but this seems to be the best solution.

EDIT Also, see @Ender's solution on how to retrieve the data.


Well, you can get the sunrise/sunset times by doing something like the following:

var sunrise = $('#sun_container .details strong:eq(0)').text();
var sunset = $('#sun_container .details strong:eq(1)').text();

From there probably write the server time into a JS var, do your comparison, and then:

if (isNight) {
    $('#sun_button img').attr('src', 'images/night.png');
}

That should get you started.


Assuming you can generate a container like this with 24 hour time:

<div id="server_time">18:00</div>
<script>
$(function() {
// Assume server_time is 24hr
var server = new Date(Date.parse("2000-01-01 " + $('#server_time').text()));
var sunrise = $('#sun_container .details strong:eq(0)').first().text();
var sunset = $('#sun_container .details strong:eq(1)').last().text();

// strip off AM/PM
sunrise = sunrise.substring(0, sunrise.length-2);
sunset = sunset.substring(0, sunset.length-2);

// Parse to standard dates
sunrise = new Date(Date.parse("2000-01-01 " + sunrise)) ;
sunset = new Date(Date.parse("2000-01-01 " + sunset));
// Add tweleve hours to compensate for PM
sunset.setHours(sunset.getHours() + 12);

if (server < sunrise || server > sunset)
    $('#sun_button > img').attr('src', 'images/night');
});
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜