dynamically create table with time increments
I need to create a table with just 1 column containing times (starting from 4hrs) which increase in increments of 10 seconds for each row. So it needs to look something like this:
04hrs 00mins 00secs - 04hrs 00mins 09secs
04hrs 00mins 10secs - 04hrs 00mins 19secs
04hrs 00mins 20secs - 04hrs 00mins 29secs
.....
06hrs 59mins 50secs - 06hrs 59mins 59secs
This will obviously take a long long time to hard code so I'm looking to create it dynamically. Based on what i'm currently trying to learn I'd like to be able to do this using jquery or asp.net (vb) but an开发者_开发百科ything will do as long as it works!
Thanks
Basic date-time arithmetic.
// format the given date in desired format
// ignores date portion; adds leading zeros to hour, minute and second
function fd(d){
var h = d.getHours();
var m = d.getMinutes();
var s = d.getSeconds();
return (h < 10 ? '0'+h : h) + 'hrs ' +
(m < 10 ? '0'+m : m) + 'mins '+
(s < 10 ? '0'+s : s) + 'secs';
}
// 1) 10800 seconds = 3600 * 3 = 3 hours
// 2) a+=10 increments seconds counter in 10-second interval
for ( var a = 0; a < 10800; a+=10 ) {
// an arbitrary date is chosen, we're more interested in time portion
var b = new Date('01/01/2000 04:00:00');
var c = new Date();
b.setTime(b.getTime() + a*1000);
// c is b + 9 seconds
c.setTime(b.getTime() + 9*1000);
$("#table1").append(
"<tr><td>" + fd(b) + ' - ' + fd(c) + "</td></tr>"
);
}
See the output here. I think you can port this code example to ASP.Net/VB.Net/C# easily.
精彩评论