date interval

Hello,
I want to get difference in seconds between to instants in different dates.
like “29/08/2017 16:28:45”, this date time is extracted from listBox.row as string.
I tried with this example :

[code] Using Xojo.Core

Dim d1 As New Date(2017, 5, 1, TimeZone.Current)
Dim d2 As New Date(2030, 1, 1, TimeZone.Current)
Dim interval As DateInterval
interval = d2 - d1

// interval.Years = 12
[/code]
This code return the interval only in years !
interval.Months , interval.Days, … , gives 0.

I have found solution by using a special Julian_day function, looking for more simple solution.
Some one has been done this task ?

days=floor(d2.SecondsFrom1970-d1.SecondsFrom1970)/86400)

With Xojo.Core.Date, if you want the difference in seconds, just subtract the SecondsFrom1970 property:

[code]Using Xojo.Core

Dim d1 As New Date(2017, 5, 1, TimeZone.Current)
Dim d2 As New Date(2030, 1, 1, TimeZone.Current)
Dim seconds As Double = d2.SecondsFrom1970 - d1.SecondsFrom1970[/code]

Using DateInterval you’ll get 12 years and 8 months.

isn’t that pretty much what I had just said? :slight_smile:

It’s ok for seconds interval , very good.
my date time string extracted form LB Cell with this format “29/08/2017 17:07:30” not supported by Date(…) !

Using Xojo.Core Dim d As Date = Date.Now dim Loc as Locale Dim t As Text = d.ToText(loc.Current, Date.FormatStyles.Short)

This code return the date and time in current format in my case “dd/mm/yyyy”.
with loc=nil, the format date would be “yyyy/mm/dd hh:mm:ss” , but it is not !

[code]Using Xojo.Core

Dim d1 As New Date(2017, 5, 1, TimeZone.Current)
Dim d2 As New Date(2030, 1, 1, TimeZone.Current)
Dim seconds As Double = d2.SecondsFrom1970 - d1.SecondsFrom1970[/code]

As i see, this function handles the date only in format: yyy,mm,dd, without a Time ?

[quote=348181:@Djamel AIT AMRANE][code]Using Xojo.Core

Dim d1 As New Date(2017, 5, 1, TimeZone.Current)
Dim d2 As New Date(2030, 1, 1, TimeZone.Current)
Dim seconds As Double = d2.SecondsFrom1970 - d1.SecondsFrom1970[/code]

As i see, this function handles the date only in format: yyy,mm,dd, without a Time ?[/quote]
it handles it down to the second… perhaps you might want to experiment with the code a bit

I want to get a difference between to events (minutes are enough) when the events are not in same day.
It means when the next event is out of midnight, like diff(31/08/2017 00:30:00 , 30/08/2017 22:00:00) …

I am not sure what about that equation you do not understand?
it returns the total number of SECONDS between the two date objects.
What you do with it (or the ultimate resolution you require) is up to you

Years ago I solved that for one of my projects with a new class in MBS Xojo Plugins:

Please try DateDifferenceMBS class.

like this example:

[code]// calculate difference between now and a date in 2008.

dim d as date
dim r as DateDifferenceMBS
dim s as string
dim c as Clipboard

d=new date
d.Year=2008
d.Month=7
d.Day=2
d.Hour=10
d.Minute=48
d.Second=22

dim e as new date // today

r=new DateDifferenceMBS(d, e)

s= "Years: "+str(r.Year)+EndOfLine
s=s+"Months: "+str(r.month)+EndOfLine
s=s+"Days: "+str(r.day)+EndOfLine
s=s+"Hours: "+str(r.hour)+EndOfLine
s=s+"Minutes: "+str(r.Minute)+EndOfLine
s=s+"Seconds: "+str(r.Second)+EndOfLine

MsgBox s[/code]

Seriously? a plugin to replace a simple subtraction?

Well, the plugin does not just make a subtraction.
When we had to make deltas, we quickly discovered that just calculating difference was not enough.

please… educate me… how so?
what can be more accurate that the simple number of seconds between two date/times?
from there you simply divide by 60 to get minutes, 3600 to get hours, 86400 to get days (or fractions there of)
no need to spend $$$$ on a plugin to encapsulate that exact process

convert to same gmt offset first since 17:00 @ GMT = 15:00 @ GMT -2
but yes there’s no need for a plugin to do this

I wrote it ten years ago and it loops from day to day to count up how many days are needed and how many months/years it crosses. That is not always the same as what you guys calculate with totalSeconds difference!

Or how would you know how many months and days are between two dates?
Tip: Dividing by 30 will not do it!

1 Like

[quote=348197:@Christian Schmitz]I wrote it ten years ago and it loops from day to day to count up how many days are needed and how many months/years it crosses. That is not always the same as what you guys calculate with totalSeconds difference!

Or how would you know how many months and days are between two dates?
Tip: Dividing by 30 will not do it![/quote]

I’m using your date difference class to drive a display that is tracking a countdown in Months, Days, Minutes, and Seconds from the current time to the time that a major server will be retired.

The class is super easy to work with and made the “doomsday clock” simple to implement.

Just wanted to thank you for the plugin. :slight_smile:

Thanks

[quote=348197:@Christian Schmitz]I wrote it ten years ago and it loops from day to day to count up how many days are needed and how many months/years it crosses. That is not always the same as what you guys calculate with totalSeconds difference!

Or how would you know how many months and days are between two dates?
Tip: Dividing by 30 will not do it![/quote]
Month differences is like counting “car lengths” - since its not a standard unit of measure its ambiguous what it means
3 cars lengths using a For2 as the unit is about the same as “1 car length” when you use my truck as the standard
Months have the same problem

Ad still none of that requires a plugin
I wrote mine in 2005 and it works to this day
Pure Xojo code

[quote=348188:@Dave S]I am not sure what about that equation you do not understand?
it returns the total number of SECONDS between the two date objects.
What you do with it (or the ultimate resolution you require) is up to you[/quote]

It does not take in account the time, in my case i take events from two LB cell in format dd/mm/yyyy hh:mn:ss to compute elapsed time (seconds or minutes) between two events (t2-t1).