Page 1 of 2

Request for new webtag - days Cumulus has been running

Posted: Tue 18 Dec 2012 6:20 pm
by RayProudfoot
Hi Steve,

I would like to display the number of days Cumulus has been running. At present we have the tag <#recordsbegandate> which should be the date Cumulus started and <#date> which is today's date.

Would it be much work to deduct one from the other and present the answer as a new tag?

Thanks.

Re: Request for new webtag - days Cumulus has been running

Posted: Tue 18 Dec 2012 6:53 pm
by steve
No, that wouldn't be too hard; I could probably add it in 1.9.4 at some point. Very easy to do with javascript, of course, as Mark suggested in the recent thread about days since 1899:

var daysSince1899 = (new Date() - new Date(1899, 11, 30)) / (24 * 60 * 60 * 1000);

Just put your start date in instead of 30/11/1899.

Re: Request for new webtag - days Cumulus has been running

Posted: Tue 18 Dec 2012 7:40 pm
by RayProudfoot
My knowledge of Javascript is less than nothing but I'm sure there will be a tutorial out there I can learn from.

Thanks Steve.

Re: Request for new webtag - days Cumulus has been running

Posted: Tue 18 Dec 2012 8:29 pm
by RayProudfoot
I've got this far but can't work out how to display only the integer part.

<!DOCTYPE html>
<html>
<body>

<script>
var CumulusRunDays=(new Date() - new Date(2009, 05, 10)) / (24 * 60 * 60 * 1000);
document.write(CumulusRunDays);
</script>
</body>
</html>

I'm sure it's very simple. :)

Re: Request for new webtag - days Cumulus has been running

Posted: Tue 18 Dec 2012 8:45 pm
by RayProudfoot
Adding this line seems to have done the trick...

CumulusRunDays=parseInt(CumulusRunDays)

I shall continue this tommorow.

Re: Request for new webtag - days Cumulus has been running

Posted: Tue 18 Dec 2012 9:13 pm
by mcrossley
That will do it Ray, or use Math.floor() as the value is already a number.

parseInt() is really for string to number conversion, and it is good practice to specify the base (xxx,10) when using parseInt() - numbers with leading zeros will be converted to octal without it.

Re: Request for new webtag - days Cumulus has been running

Posted: Tue 18 Dec 2012 11:10 pm
by RayProudfoot
Thank you Mark. This is how the code looks now.

<!DOCTYPE html>
<html>
<body>

<script>
var CumulusRunDays=(new Date() - new Date(2009, 04, 10)) / (24 * 60 * 60 * 1000);
CumulusRunDays=Math.floor(CumulusRunDays)-1

document.write(CumulusRunDays);
</script>
</body>
</html>

I saw on one website that the month of the starting date has a base value of 0 so 1 should be deducted from it for the correct number of days to be calculated. Then 1 day has to be deducted to arrive at the correct number of days.

Tomorrow I will embed this into my HTML page.

Re: Request for new webtag - days Cumulus has been running

Posted: Wed 19 Dec 2012 7:38 pm
by steve
Ray, I know you're having fun with javascript and all that, and I don't want to spoil it, but Arthur has found a serious bug in the realtime.txt code for (some) people with 'extra' sensors. I was hoping not to do another build of 1.9.3, but I have to fix this bug, and as your web tag is only five lines of code, I'll sneak that in too.

Re: Request for new webtag - days Cumulus has been running

Posted: Wed 19 Dec 2012 7:56 pm
by RayProudfoot
That's nice of you Steve, thanks. :)

I have made good progress and may well complete it just so I can say I've dipped my toe into Javascript.

Re: Request for new webtag - days Cumulus has been running

Posted: Wed 19 Dec 2012 9:56 pm
by RayProudfoot
I've completed my first attempt at Javascript and you can see the result on my home page just below the links to other webpages. http://www.cheadlehulmeweather.co.uk

I'd appreciate any advice on how I've written the code and whether it conforms to Javascript standards.

Steve, I'll also include the new webtag soon so I can check I have calculated the same number of days as you.

Re: Request for new webtag - days Cumulus has been running

Posted: Wed 19 Dec 2012 10:12 pm
by mcrossley
Steve, I haven't tried it yet, but does the new tag return whole days or decimal days?

Re: Request for new webtag - days Cumulus has been running

Posted: Wed 19 Dec 2012 11:02 pm
by steve
Whole days. And I think it truncates rather than rounding.

Re: Request for new webtag - days Cumulus has been running

Posted: Thu 20 Dec 2012 11:18 pm
by RayProudfoot
A strange problem with my Javascript for counting the days Cumulus has been recording. It's incrementing the day counter soon after 23:00hrs rather than midnight. As I have removed the decimal element of the calculation I'm perplexed why this is happening.

I haven't yet tried Steve's new webtag for comparison. Any ideas anyone?

I'm about to hit the sack and am out for much of tomorrow.

Re: Request for new webtag - days Cumulus has been running

Posted: Thu 20 Dec 2012 11:52 pm
by mcrossley
Its a summer time 'thing' as your start date is in summer time, and we aren't now. Also to get the code to work in any 'clients' TZ (not GMT/BST), further manipulation is required. The start date needs to take the client TZ into account, and both start and now need to take summer time into account.

Cross TZ and DST Date/Time manipulations drive me mad :bash:

It will all get pretty messy, better to use the webtag.

Re: Request for new webtag - days Cumulus has been running

Posted: Fri 21 Dec 2012 9:40 am
by RayProudfoot
Thanks Mark. Why is everything so complicated? :( The webtag sounds like a good alternative.