Normal Topic Calculate years, months, and days between dates. (Read 3454 times)
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2480
Joined: Aug 20th, 2003
Calculate years, months, and days between dates.
Mar 15th, 2004 at 2:22pm
Print Post Print Post  
Here is a program I just wrote. It figures Years, Months and Days from one date to the next. As always long lines will need to be unwrapped.  To use this code in your application simple remove the following two lines from the code below.

vPromisedShipDate = "4/19/1985"
vActualShipDate = "6/26/1985"

and replace them with

vPromisedShipDate = MyFirstDateField
vActualShipDate = MySecondDateField

where MyFirstDateField and MySecondDateField are the names of two date elements on your form. vActualShipDate must be the most recent date. If you are not sure which date will always be the biggest you could use an If statement like the following one to make sure that vActualShipDate is always the biggest.

If MyFirstDateField > MySecondDateField then
{
vPromisedShipDate = MySecondDateField
vActualShipDate = MyFirstDateField
}
Else
{
vPromisedShipDate = MyFirstDateField
vActualShipDate = MySecondDateField
}


***Actaul code is below***


Var vPromisedShipDate as Date
Var vActualShipDate as Date
Var vPromisedShipMonth as int
Var vActualShipMonth as int
Var vPromisedShipYear as int
Var vActualShipYear as int
Var vPromisedShipDay as int
Var vActualShipDay as int
var vYearsPassed as int
var vMonthsPassed as int
var vDaysPassed as int

vPromisedShipDate = "4/19/1985"
vActualShipDate = "6/26/1985"

vPromisedShipYear = @Year(vPromisedShipDate)
vActualShipYear = @Year(vActualShipDate)

vPromisedShipMonth = @Month(vPromisedShipDate)
vActualShipMonth =  @Month(vActualShipDate)

vPromisedShipDay = @DOM(vPromisedShipDate)
vActualShipDay =  @DOM(vActualShipDate)

If vActualShipYear >= vPromisedShipYear then
{
if vActualShipMonth >= vPromisedShipMonth then
{
  if vActualShipDay > vPromisedShipDay then
  {
   vYearsPassed = vActualShipYear - vPromisedShipYear
   vMonthsPassed = vActualShipMonth - vPromisedShipMonth 
   vDaysPassed  =  vActualShipDay - vPromisedShipDay
   WriteLn("This Product Shipped " + @str(vYearsPassed) + " Years, " + @str(vMonthsPassed) + " Months, and " + @str(vDaysPassed) + " Days later than it was supposed to")
  }
  else if vActualShipDay = vPromisedShipDay then
  {
   //The product shipped exactly on the day it was supposed to
   WriteLn("We should buy from this company more often. Product was shipped on date it was supposed to be shipped on.")
  }
  else
  {
   //Product shipped sooner than expected same month but earlier day
   WriteLn("This product was shipped at least a day before it was expected to be.") 
  }

}
else
{
  //Product was shipped atleast a month before it was expected to
  WriteLn("This product was shipped at least a month before it was expected to be") 
}
}
else
{
//Product was shipped at least a year before it was expected to
WriteLn("This product was shipped at least a year before it was expected to be")
}
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged