最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Parse time data from HTML using jQuery - Stack Overflow

programmeradmin3浏览0评论

I've got a javascript widget on my page that outputs times for Sunrise and Sunset. 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 pare 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";

I've got a javascript widget on my page that outputs times for Sunrise and Sunset. 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 pare 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";

Share Improve this question edited Dec 8, 2010 at 22:09 WNRosenberg asked Dec 8, 2010 at 21:59 WNRosenbergWNRosenberg 1,9025 gold badges23 silver badges31 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

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

<script type="text/javascript" src="http://ajax.googleapis./ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="http://www.datejs./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 parison, 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 pensate for PM
sunset.setHours(sunset.getHours() + 12);

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

评论列表(0)

  1. 暂无评论