Asked — Edited

Date And Time Question

Can anyone point me in the direction to find a way to tell how much time there is between two dates.

For example Date1 = 10/29/2016 10:30:54 PM and Date2 = 10/28/2016 7:00:01 AM

How many days, hours, minutes, and seconds before date 1.


ARC Pro

Upgrade to ARC Pro

Get access to the latest features and updates before they're released. You'll have everything that's needed to unleash your robot's potential!

#1  

You'd need to break down the days and time separately then subtract. Have you got a script example you are working on?

PRO
USA
#2  

@Dbeard,

EZ-Script has a few limitations and some parser bugs.
If you are doing complex or repetitive calculations, I would invest my time in a Plugin.


$fromDate = "10/27/2016 1:00:00 PM"
#$fromDate = now()
$toDate   = "10/28/2016 1:00:00 AM"

DefineArray($tmp_ar, 22, 0)
# Working Array 
# pos description
#  0 - fromDate - year
#  1 - fromDate - month
#  2 - fromDate - day
#  3 - fromDate - hour
#  4 - fromDate - minutes
#  5 - fromDate - seconds
#  6 - fromDate - leap year : 1=yes 0=no
#  7 - fromDate - month's days
#  8 - toDate   - year
#  9 - toDate   - month
# 10 - toDate   - day
# 11 - toDate   - hour
# 12 - toDate   - minutes
# 13 - toDate   - seconds
# 14 - toDate   - leap year : 1=yes 0=no
# 15 - toDate   - month's days
# 16 - total    - year(s)
# 17 - total    - month(s)
# 18 - total    - day(s)
# 19 - total    - hour(s)
# 20 - total    - minute(s)
# 21 - total    - second(s)


#parse fromDate
$ParseDate_IN_DateTime=$fromDate
$ParseDate_IN_StartIndex=0
Goto(ParseDate)

#parse toDate
$ParseDate_IN_DateTime=$toDate
$ParseDate_IN_StartIndex=8
Goto(ParseDate)
  
Goto(CalcDiff)

Print("fromDate:" + $fromDate)
Print("  toDate:" + $toDate)
Goto(PrintDiff)  
  
Halt()  
  
  
: PrintDiff  
  #ARC bug don't use ( in a string
  if ($tmp_ar[16]>0)
    Print($tmp_ar[16] + " year[s]" )
  endif 
  if ($tmp_ar[17]>0)
    Print($tmp_ar[17] + " month[s]" )
  endif 
  if ($tmp_ar[18]>0)
    Print($tmp_ar[18] + " day[s]" )
  endif 
  if ($tmp_ar[19]>0)
    Print($tmp_ar[19] + " hour[s]" )
  endif 
  if ($tmp_ar[20]>0)
    Print($tmp_ar[20] + " minute[s]" )
  endif 
  if ($tmp_ar[21]>0)
    Print($tmp_ar[21] + " second[s]" )
  endif 
  
  Return()
  
:CalcDiff
  #expects:
  #  tmp_ar : array with at least 22 elements (0..7=fromDate, 8..15=toDate, 16..21=#totals)
  
  #Note:  EZ-Script parser bug (need spaces when combining multiple inline operations)
  
  #tmp1 is used to flag an overflow
  
  #calculate #seconds
  if ($tmp_ar[5] > $tmp_ar[13])
    $tmp_ar[21]=$tmp_ar[13] + 60 - $tmp_ar[5]
    $tmp1=1 
  else
    $tmp_ar[21]=$tmp_ar[13] - $tmp_ar[5]
    $tmp1=0
  endif
  
  #calculate #minutes
  if ($tmp_ar[4] + $tmp1>$tmp_ar[12])
    $tmp_ar[20]=$tmp_ar[12] + 60 - $tmp_ar[4] - $tmp1 
    $tmp1=1
  else 
    $tmp_ar[20]=$tmp_ar[12] - $tmp_ar[4] - $tmp1 
    $tmp1=0
  endif

  #calculate #hours
  if ($tmp_ar[3] + $tmp1>$tmp_ar[11])
    $tmp_ar[19] = $tmp_ar[11] + 24 - $tmp_ar[3] - $tmp1
    $tmp1 = 1
  else
    $tmp_ar[19] = $tmp_ar[11] - $tmp_ar[3] - $tmp1
    $tmp1 = 0
  endif

  #calculate #days
  if ($tmp_ar[2] + $tmp1> $tmp_ar[10])
    $tmp_ar[18]=$tmp_ar[10] + $tmp_ar[7] - $tmp_ar[2] - $tmp1
    $tmp1=1 
  else
    $tmp_ar[18]=$tmp_ar[10] - $tmp_ar[2] - $tmp1
    $tmp1=0
  endif

  #calculate #months
  if ($tmp_ar[1] + $tmp1>$tmp_ar[9])
    $tmp_ar[17]=$tmp_ar[9] + 12 - $tmp_ar[1] - $tmp1 
    $tmp1=1
  else 
    $tmp_ar[17]=$tmp_ar[9] - $tmp_ar[1] - $tmp1 
    $tmp1=0
  endif

  #calculate #years
  $tmp_ar[16] = $tmp_ar[8] - $tmp_ar[0] - $tmp1
  
  Return()


  
  
:ParseDate
  #expects: 
  #   $ParseDate_IN_DateTime  : variable with the datetime EN-US format
  #   ParseDate_IN_StartIndex : Index to store the datetime in the array (0=first date, 8=second date, 16=next date) 
   
  $tmp1=split($ParseDate_IN_DateTime, " ", 0)
  $tmp_ar[$ParseDate_IN_StartIndex]=cint(split($tmp1, "/", 2))
  $tmp_ar[$ParseDate_IN_StartIndex+1]=cint(split($tmp1, "/", 0))
  $tmp_ar[$ParseDate_IN_StartIndex+2]=cint(split($tmp1, "/", 1))

  $tmp1=split($ParseDate_IN_DateTime, " ", 1)
  $tmp_ar[$ParseDate_IN_StartIndex+3]=cint(split($tmp1, ":", 0))
  $tmp_ar[$ParseDate_IN_StartIndex+4]=cint(split($tmp1, ":", 1))
  $tmp_ar[$ParseDate_IN_StartIndex+5]=cint(split($tmp1, ":", 2))

  #convert am/pm to 24 hours (0..23)
  $tmp1=split($ParseDate_IN_DateTime, " ", 2)
  if ($tmp1="AM" AND $tmp_ar[$ParseDate_IN_StartIndex+3]=12)
    $tmp_ar[$ParseDate_IN_StartIndex+3]=0
  elseif ($tmp1="PM" AND $tmp_ar[$ParseDate_IN_StartIndex+3]<12)
    $tmp_ar[$ParseDate_IN_StartIndex+3]=$tmp_ar[$ParseDate_IN_StartIndex+3]+12  
  endif
  
  #leap year calculation
  $tmp1=$tmp_ar[$ParseDate_IN_StartIndex] % 4
  $tmp2=$tmp_ar[$ParseDate_IN_StartIndex] % 100
  $tmp3=$tmp_ar[$ParseDate_IN_StartIndex] % 400
  
  if ($tmp1=0 AND $tmp2!=0)
    $tmp_ar[$ParseDate_IN_StartIndex+6]=1
  elseif ($tmp3=0)
    $tmp_ar[$ParseDate_IN_StartIndex+6]=1
  else
    $tmp_ar[$ParseDate_IN_StartIndex+6]=0
  endif

  #number of days in the month calculation
  $tmp1=$tmp_ar[$ParseDate_IN_StartIndex+1]
  if ($tmp1=2)
    $tmp_ar[$ParseDate_IN_StartIndex+7]=28+$tmp_ar[$ParseDate_IN_StartIndex+6]
  elseif ($tmp1=4 OR $tmp1=6 OR $tmp1=9 OR $tmp1=11)
    $tmp_ar[$ParseDate_IN_StartIndex+7]=30
  else
    $tmp_ar[$ParseDate_IN_StartIndex+7]=31
  endif
  Return()

c# version:


var fromDate = DateTime.ParseExact("10/28/2016 7:00:01 AM", "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
var toDate = DateTime.ParseExact("10/29/2016 10:30:54 PM", "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
Console.WriteLine("{0:%d} days, {0:%h} hours, {0:%m} minutes, {0:%s} seconds", toDate - fromDate);

output:

User-inserted image

PRO
USA
#3  

Thanks, ptp. I appreciate the code, not sure my skills are up to coding a plugin. I have trouble manipulating a date. I will start reviewing the plugin documentation and tutorial to see if I can figure it out.

PRO
USA
#4  

@dbeard,

fixed a bug and added comments to explain my logic.

PRO
Synthiam
#5  

[feature] DateTime Functions MinDate() MaxDate() MonthName() AddDays() AddMonths() AddYears() AddHours() AddMinute() AddSeconds() FmtNum()

Casting Functions These functions are to cast objects from one datatype to another To Double: CDBL() To Integer: CInt() To Long: CLong() To Unsigned Integer: CUint() To Unsigned Long: CULong() To DateTime: CDateTime() [/feature]


$startDate = "10/29/2016 1:00:00 PM"
$endDate = "10/28/2016 2:02:02 PM"

$diff = CDateTime($startDate) - CDateTime($endDate)

print($diff)

PRO
USA
#6  

@DJ,

That's a major refactoring! I tried that before but does not work as the OP wants. does not handle days, months, years.


$fromDate = "11/17/1974 10:00:00 PM"
$toDate   = "10/27/2016 4:29:00 PM"
$diff = CDateTime($toDate) - CDateTime($fromDate)
print($diff)

Check the output:

User-inserted image

PRO
Synthiam
#7  

Your code is very fun and impressive! You could save a bit of code by parsing the output of this...


$startDate = "10/29/2016 1:00:00 PM"
$endDate = "5/28/2014 2:02:02 PM"

$diff = CDateTime($startDate) - CDateTime($endDate)

The $diff in this case is 884.22:57:58. That's

  • 884 days
  • 22 hours
  • 57 minutes
  • 58 seconds

I just added a fmttimespan() to EZ-Script for the next release. It will allow formatting of a timespan (which is what a datetime subtraction/addition creates)

PRO
USA
#8  

@DJ,

yes it was more an exercise i can't do that in my day job:)

I like to test concepts: Goto / Return (like a function call) Working with an array to minimize variable usage Reusing a (function call)

everything went well.

yes i could parse the big number (days), but i would need to calculate the leap years, to extract years, then how many leap years in between, so i decided to do it from scratch.