admin 管理员组文章数量: 1086019
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";
4 Answers
Reset to default 3You 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>
本文标签: javascriptParse time data from HTML using jQueryStack Overflow
版权声明:本文标题:javascript - Parse time data from HTML using jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744071222a2528521.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论