I am calculating the number of days between two dates(the 2 dates are in seconds). The following gives me the coorect result but it gives me a negative value.
For example -7.0. I am not sure why..
Also can I remove the decimal point and display?
set interval [expr { $start_date - $get_date }]
set days [expr { floor($interval / (60*60*24)) }]
puts "Start Date: $start_date <br>"
puts "Stop Date: $get_date <br>"
puts "Total number of dates betwen 2 days: $days"
You’re subtracting the end (assuming that’s what
$get_dateis) from the start. Think of it like numbers, where a later date is a bigger number – if you subtract a large number from a small number, you get a negative value, right?So you probably just want to reverse the arithmetic:
I’ve no idea about the decimal point part, I’m afraid… perhaps (based on this documentation) just:
EDIT: As noted in comments, some care is required when it comes to “days” between two events. Do you mean elapsed days, 24 hours per day, or “local” days? Are your input values in local time, UTC, or something else? Could you perhaps use a dedicated date/time library to handle this? (I have no idea whether tcl has such a thing, but I’d expect it to.)
You should think really carefully about exactly what behaviour you want in what situation, and write tests accordingly.