I need to be able to get the difference between now and a past date in year months days. So I have a method that accepts a date and returns a string with the result.
Its seems to work but I feel there should be a more elegant way.
I also realised I should comment my code, …
datediff(sd as date )as string
dim now as new date //date,time now
dim rs as string //return string
dim t,ddays,dmonths as integer
dim mth() as integer
mth = array(31,28,31,30,31,30,31,31,30,31,30,31) //days in each month
t=(now.TotalSeconds-sd.TotalSeconds) //number of second between now and the selected date
t = t/(365.24*24*60*60) //seconds divided by number seconds in a year to give years
if t>0 then
rs =str(t)+" Year"
if t >1 then
rs=rs+"s"
end if
end if
// now get the days
t=0
if sd.day <> now.day then
ddays=now.day-sd.day
if ddays<0 then
ddays=ddays+mth(((now.Month+11)mod 12)+1) // If
t=1 //carry to use for month calc if we have borrowed due to the sd.day being larger than the now.day
end if
end if
// now the months
if sd.month<>now.month or t=1 then
dmonths=now.month-sd.month-t
if dmonths<0 then
dmonths=dmonths+12
end if
rs=rs+" "+str(dmonths)+" Month"
if dmonths>1 then
rs=rs+"s" // if more than 1 month then add an s to make months
end if
end if
rs=rs+" "+str(ddays)+" day"
if t>0 then
rs=rs+"s" //if more than 1 day make pleural ie days
end if
return rs
dim today as new date
dim past as new date
dim yr as integer
dim mt as integer
dim dy as integer
call ParseDate("01/01/2013",past)
past.totalseconds=today.TotalSeconds-past.TotalSeconds
yr=past.Year-1900
mt=past.month
dy=past.day
this seems to work… yr is # of years, mt=# of months dy=#days
might need a bit of tweaking… but that should be the gist of it
dim today as new date
dim past as new date
dim yr as integer
dim mt as integer
dim dy as integer
call ParseDate("01/01/2013",past)
past.totalseconds=today.TotalSeconds-past.TotalSeconds
yr=past.Year-1904
mt=past.month-1
dy=past.day-1
No need to buy an expensive 3rd party plugin to do simple math
True, but I really like finding clean short code that does something that I have struggled with. If I needed it to just work MBS is the go, if I want to understand it the forums are great.
No offense to third party tool developers who often come up with features not possible otherwise, but a lot of plugins are just like TV dinners versus cooking at home. In the very case of this topic, applying MBS solution is just like instant coffee : drop the darn thing into water, and forget about dates. The ‘committee discussion’ gives a chance for participants to better understand the Date object and all its features.
There is something enjoyable in the process of grasping a new concept, that an instant solution will never provide. If the only thing that counts was just the result, a lot of us would have absolutely no reason to code.
Point wasn’t that this is a difficult piece of code to put together - it’s not!
Point was the OP may already have the MBS suite and not be aware of that class - therefore not an expensive solution and may suit their needs - that’s all.
Some people don’t have the luxury of time for finding solutions - sometimes you need to “drop the darn thing into water, and forget about dates”. This may be a poor example though as it’s a straightforward piece of code.
dim d1 as new date(2012, 9, 10)
dim d2 as new date(2014, 8, 1)
dim d3 as date
dim dyear, dmonth, dday as integer
dyear= d2.year- d1.year
dmonth= d2.month- d1.month
dday= d2.day- d1.day
if dmonth< 0 then
dyear= dyear- 1
dmonth= dmonth+ 12
end
if dday< 0 then
dmonth= dmonth- 1
d3= new date(d1)
d3.day= 1
d3.month= d3.month+ 1
d3.day= d3.day- 1
dday= dday+ d3.day
end
Things don’t have to be either/or. Instant coffee is fine when you are short on time. Taking the time of a patient brew has rewards of its own. Both are possible and valid solutions.
From what I can read in his original post, though, Hamish seemed more looking for some constructive propositions to improve his own code than asking for an instant solution. Dave and Tim code look to me more in line with the OP.
Synchronicity is an amazing thing : last week the very same need to measure time difference between dates arrised in the French channel.
[quote=126347:@Patrick Delaney]But Dave is right though - this is simple enough though…
[/quote]
Have 100 programmers write binary search on an array of numbers with pencil and paper. Type them in letter for letter, and compile. You’ll be lucky if 1 has a correctly working solution. Binary search is simple enough that any competent programmer should be able to get it right eventually.
I have Dave on ignore because of ignorant swipes he’s been prone to make at 3rd party plugins, much like the one Pat quoted. Dave, if you read this, it’s way better form when you make those swipes if your solution actually works. Just saying. Extending this, if the OP wanted a time difference that includes hours, minutes, and seconds, and the Dates could be in different time zones, there are deficiencies in the Date documentation that make that far more complicated than it might appear.
If Tim doesn’t come in with actual knowledge on this, you guys get nowhere helpful with the discussion. A thank you to him seems more in order than cheering on the mob for not figuring out any salient points of the issue.
Wow… ignorant now am I?
Because I propose a learning experience as opposed to as Michel called it a “instant coffee” approach…
Sure MBS has its place… for those where time is money, but many of us (and I bet the OP is one), like to expand OUR knowledge.
Am sorry to see the “community spirit” of this forum begin to fail so quickly.
And in my opinion if you give 100 programmers a binary search problem. and all 100 don’t come up with almost identical answers than I would fire them all. (HI+LO)/2 … seriously?