How To Calculate the Number of Days Between Two Dates

Mahares

Expert
Licensed User
Longtime User
I am puzzled by the following code I wrote to compute the number of days between the current date and time, and a previous date and time: It returns exactly 1 day less than it should be. But, when I replace the line that starts with MyDate with the commented line and insert the current date and time instead of the variables, it returns the correct numbers of days between. Any tips will be greatly appreciated. Thank you.
B4X:
Dim MyDateTime As Long
Dim txtDate, txtTime As String
Dim Datepr, MyDate As Long
Dim Ndays as double

  MyDateTime = DateTime.Now
  DateTime.DateFormat = "M/d/yyyy"     'example: 12/28/2011
  DateTime.TimeFormat="h:mm:ss a"      'example: 6:29:17 PM
  txtDate=DateTime.Date(MyDateTime)
  txtTime=DateTime.Time(MyDateTime)
  Msgbox(txtDate & "  " & txtTime,"")
  Datepr = DateTime.DateParse("12/24/2011") + DateTime.TimeParse("6:29:17 PM")
  'MyDate = DateTime.DateParse("12/28/2011") + DateTime.TimeParse("6:29:17 PM")   'You enter the current date and time manually here.
  MyDate = DateTime.DateParse(txtDate) + DateTime.TimeParse(txtTime)
  Ndays=Round2((MyDate - Datepr)/1000/3600/24,1)
  Msgbox(Ndays,"")
 

GMan

Well-Known Member
Licensed User
Longtime User
Dont work - tried several out.

AddPeriod(MyStartdatum1.Text,90) solves an loooong error code.

Has anyone a suggestion if it depends on the Date format (as written above) ?

GreeTZ
Gman
 
Upvote 0
Top