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,"")
 

wheretheidivides

Active Member
Licensed User
Longtime User
So, getting back to the issue, how does one get the days, hours, mins from 2 different input times? Not the time right now? Is the wiki wrong? I tried it and it got some info wrong. I tried this and it does 2 dates. It's a jumble. Could someone just post the code to calculate the days, mins, secs between 2 dates and times including going over different days.

It'd be easier if this was added to the library. Something like this.

DateTime.EventLength = (DateTime1, dateTime2) 'dt1 holds date and time of 1st event and dt2 hold date and time of 2nd event

and then these variables would hold the info
DateTime.EventLength.Secs
DateTime.EventLength.Mins
DateTime.EventLength.Hours
DateTime.EventLength.Days
DateTime.EventLength.Years
DateTime.EventLength.IsPositive (returns a "1" means date 2 >date 1, returns a "2" means date1=date2 and retunrs a "3" means date2<date3)

=======
anyways, this is my retarded solution. I never said I was a good programmer. I just get the job done. The big issue was clocking in at 10 pm and working to 1am (2 days). The parse only does the vurrent day. Also, the formats of strings, int and long were an issue.

These are strings
CalcDateIn (First date)
CalcTimeIn (first time)
CalcDateOut (second date)
CalcTimeOut (second time)

and time converted to mins for comparison. Long
CTImins (first time)
CTOmins (second time)


B4X:
'=======================================================
'calc dates
'-----------------------------
'1 day
   Dim date1, date2, DateX, elapsed As Long
   date1 = DateTime.DateParse(CalcDateIn) + DateTime.TimeParse(CalcTimeIn)
   date2 = DateTime.DateParse(CalcDateOut) + DateTime.TimeParse(CalcTimeOut)
   
   '------------------------
   'CTImins is long(CalcTimeIn is String)
      CTImins = (IncTimeInHour * 60) + IncTimeInMinute '(give total mins for hours plus mins)
      
   'CTOmins is long(CalcTimeOut is string)
      CTOmins = (IncTimeOutHour * 60) + IncTimeOutMinute'(give total mins for hours plus mins)

   '---------------------------------------------
   DateTime.DateFormat = "M/d/yyyy"
    DateTime.TimeFormat = "HH:mm:ss"

   Dim DateDOYin, DateDOYout As Int 
   Dim SomeDateIn, SomeDateOut As Long 
   
   SomeDateIn = DateTime.DateParse(CalcDateIn)
   SomeDateOut = DateTime.DateParse(CalcDateOut)

   DateDOYin = DateTime.GetDayOfYear(SomeDateIn)
   DateDOYout = DateTime.GetDayOfYear(SomeDateOut)
      Msgbox("STI-" & SomeDateIn & CRLF & "STO" & SomeDateOut & CRLF & "DOYin-" & DateDOYin & CRLF & "DOYout-" & DateDOYout, "OK")

   '----------------------------------------------------------
   If DateDOYin = DateDOYout AND CTImins = CTOmins Then
      'same date/// start time is same as out time (Error) (tues 3pm - tues 3pm) (-0 hour)
      InOutOK = False
      Msgbox("Days are same OK, but start time & out time are the same.  Go back and fix.", "OK")
      '
      
   Else If DateDOYin = DateDOYout AND CTImins < CTOmins Then
      'same date/// start time BEFORE out time (Good) (tues 3pm - tues 4pm) (1 hour)
      InOutOK = True
      '
      elapsed = date2 - date1
      
   Else If DateDOYin = DateDOYout AND CTImins > CTOmins Then
      'same date/// start time AFTER out time (Error) (tues 3pm - tues 2pm) (-1 hour)
      InOutOK = False
      Msgbox("Days are same OK, but start time is AFTER out time.  Go back and fix.", "OK")
      '
   '-----------------------------------
   Else If DateDOYin < DateDOYout AND CTImins < CTOmins  Then
      'In date BEFORE out date/// Start time BEFORE= out time (Error) (tues 3pm - wed 4pm) (25 hours)
      InOutOK = False
      Msgbox("You worked over 24 hours.  Create 2 entries (1st entry until midnight & 2nd entry is after midnight).  Go back and fix.", "OK")
      '
      
   Else If DateDOYin < DateDOYout AND CTImins >= CTOmins Then
      'In date BEFORE out date/// Start time AFTER out time (tues 3pm - wed 2pm) (23 hours)
      InOutOK = True
      '
      elapsed = (date2 - date1) + 24
   '-----------------------------------
   Else If DateDOYin > DateDOYout Then
      'In date AFTER out date/// (Error)
      InOutOK = False
      Msgbox("Start date is AFTER end date.  Go back and fix.", "OK")
      '

   End If
   '--------------------------------------------------------------
   If InOutOK = True Then
      '--------------------------------------
      seconds = Round(elapsed / 1000)
      minutes = Floor(seconds / 60) Mod  60
      hours = Floor(seconds / 3600)
      days = Floor(hours / 24)
      hours = hours Mod 24
      seconds = seconds Mod 60
      '----------------
      'Put up dates
         Label1803a.Text = CalcDateIn
         Label1803b.Text = CalcDateOut
      '----------------
      'put up times
         Label1803aa.Text = CalcTimeIn
         Label1803bb.Text = CalcTimeOut
         Label1803cc.Text = hours  & " Hrs, " & minutes & " Mins"
   Else
      '----------------
      'Put up dates
         Label1803a.Text = "Error"
         Label1803b.Text = "Error"
      '----------------
      'put up times
         Label1803aa.Text = "Error"
         Label1803bb.Text = "Error"
         Label1803cc.Text = "Error"
   End If

'====================================================================================
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
My version:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   DateBetweenTwoDates("01/20/13", "22:00:00", "01/23/14", "08:12:34")
End Sub

Sub DateBetweenTwoDates(D1 As String, T1 As String, D2 As String, T2 As String)
   Dim start, endTime, t As Long
   start = DateTime.DateTimeParse(D1, T1)
   endTime = DateTime.DateTimeParse(D2, T2)
   Dim days, hours, minutes, seconds As Int
   t = endTime - start
   days = Floor(t / DateTime.TicksPerDay)
   hours = Floor((t Mod DateTime.TicksPerDay) / DateTime.TicksPerHour)
   minutes = Floor((t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute)
   seconds = Floor((t Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond)
   
   Log("Days = " & days)
   Log("Hours = " & hours)
   Log("Minutes = " & minutes)
   Log("Seconds = " & seconds)
End Sub
 
Upvote 0

MLDev

Active Member
Licensed User
Longtime User
:signOops: You got a bug. This:

B4X:
t = endTime - start

Should be this:

B4X:
t = Abs(endTime - start)

You never know they might have a time machine and endTime is before the start.
 
Upvote 0

wheretheidivides

Active Member
Licensed User
Longtime User
Erel, here are the results of my tests of your formula code for 2 dates. all had 00 for seconds. results are days, hours, mins, seconds. I you look at these you will see that the times are incorrect on some of these(and thus my issues with time calculations)

==========================
same day, same time
1/21/2013 3:01am
1/21/2013 3:01am
0,0,0,0 (correct)
========================
same day, later end time
1/21/2013 3:01am
1/21/2013 4:02am
0,1,1,0 (correct)
==========================
same day earlier end time
1/21/2013 3:01am
1/21/2013 2:02 am
-1, -1, -59, 0 (WRONG-IT'S THE SAME DAY. SHOULD BE 0,-1,-59,0)==========================


End day is earlier than start day
1/21/2013 3:00am
1/20/2013 2:00 am
-1, -1, 0,0 (CORRECT)
===============================
End day is earlier than start day
1/21/2013 3:01am
1/20/2013 3:01 am
0, 0, 0, 0 (Wrong, it's a day earlier- should be -1,0,0,0)
========================
End day is earlier than start day
1/21/2013 3:01am
1/20/2013 6:01 am
0,3,0,0(wrong, it's a day earlier. should be 0,21,0,0 )


==========================
End day is after start day
1/21/2013 3:01am
1/22/2013 6:04 am
0,3,3,0 (wrong, should be 1,3,3,0)
==========================
End day is after start day
1/21/2013 3:01am
1/22/2013 3:01 am
0,0,0,0 (wrong, should be 1,0,0,0)
==========================
End day is after start day
1/21/2013 3:01am
1/22/2013 2:01 am
-1, -1,0, 0 (correct)
==========================
 
Upvote 0

MLDev

Active Member
Licensed User
Longtime User

There's a bug in Erel's code. See my post.
 
Upvote 0

wheretheidivides

Active Member
Licensed User
Longtime User
MLdev. here are results using t = Abs(endTime - start). it still doesn't give correct results. I have 3 sets of days (end date before, end day same and end day after) and times (end time before start time, same and end time after start time). The ones that were the same I omitted.

==========================
same day earlier end time
1/21/2013 3:01am
1/21/2013 2:02 am
0, 0, 59, 0 (WRONG-IT'S THE SAME DAY. SHOULD BE 0,0,-59,0)
==========================
End day is earlier than start day
1/21/2013 3:00am
1/20/2013 2:00 am
0,1,0,0 (wrong.)
===============================
End day is earlier than start day
1/21/2013 3:01am
1/20/2013 3:01 am
0, 0, 0, 0 (Wrong, it's a day earlier- should be -1,0,0,0)
========================
End day is earlier than start day
1/21/2013 3:01am
1/20/2013 6:01 am
0,3,0,0(wrong, it's a day earlier. should be 0,-21,0,0 )


==========================
End day is after start day
1/21/2013 3:01am
1/22/2013 6:04 am
0,3,3,0 (wrong, should be 1,3,3,0)
==========================
End day is after start day
1/21/2013 3:01am
1/22/2013 3:01 am
0,0,0,0 (wrong, should be 1,0,0,0)
==========================
End day is after start day
1/21/2013 3:01am
1/22/2013 2:01 am
0,1,0,0 (wrong should be 0,23,0,0)
==========================
 
Last edited:
Upvote 0

MLDev

Active Member
Licensed User
Longtime User
I don't know how you're using this subroutine but I'm getting the correct results.

results using t = Abs(endTime - start)
==========================
same day earlier end time
1/21/2013 3:01am
1/21/2013 2:02 am
0, 0, 59, 0 (WRONG-IT'S THE SAME DAY. SHOULD BE 0,0,-59,0) (That's correct. The sub name is DateBetweenTwoDates not DateBetweenStartDateAndEndDate. See below.)
==========================
End day is earlier than start day
1/21/2013 3:00am
1/20/2013 2:00 am
0,1,0,0 (wrong.)
With DateBetweenTwoDates("1/21/2013", "03:00:00", "1/20/2013", "02:00:00")
1,1,0,0 (Correct)
===============================
End day is earlier than start day
1/21/2013 3:01am
1/20/2013 3:01 am
0, 0, 0, 0 (Wrong, it's a day earlier- should be -1,0,0,0)
With DateBetweenTwoDates("1/21/2013", "03:01:00", "1/20/2013", "03:01:00")
1, 0, 0, 0 (Correct)
========================
End day is earlier than start day
1/21/2013 3:01am
1/20/2013 6:01 am
0,3,0,0(wrong, it's a day earlier. should be 0,21,0,0 )
With DateBetweenTwoDates("1/21/2013", "03:01:00", "1/20/2013", "06:01:00")
0,21,0,0 (Correct)
==========================
End day is after start day
1/21/2013 3:01am
1/22/2013 6:04 am
0,3,3,0 (wrong, should be 1,3,3,0)
With DateBetweenTwoDates("1/21/2013", "03:01:00", "1/22/2013", "06:04:00")
1,3,3,0 (Correct)
==========================
End day is after start day
1/21/2013 3:01am
1/22/2013 3:01 am
0,0,0,0 (wrong, should be 1,0,0,0)
With DateBetweenTwoDates("1/21/2013", "03:01:00", "1/22/2013", "03:01:00")
1,0,0,0 (Correct)
==========================
End day is after start day
1/21/2013 3:01am
1/22/2013 2:01 am
0,1,0,0 (wrong should be 0,23,0,0)
DateBetweenTwoDates("1/21/2013", "03:01:00", "1/22/2013", "02:01:00")
0,23,0,0 (Correct)
==========================

B4X:
Sub DateBetweenStartDateAndEndDate(D1 As String, T1 As String, D2 As String, T2 As String)
   Dim start, endTime, t As Long
   start = DateTime.DateTimeParse(D1, T1)
   endTime = DateTime.DateTimeParse(D2, T2)
   Dim days, hours, minutes, seconds As Int
   t = Abs(endTime - start)
   days = Floor(t / DateTime.TicksPerDay)
   hours = Floor((t Mod DateTime.TicksPerDay) / DateTime.TicksPerHour)
   minutes = Floor((t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute)
   seconds = Floor((t Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond)
   If endTime < start Then
      days = -days
      hours = -hours
      minutes = -minutes
      seconds = -seconds
   End If
   Log("Days = " & days)
   Log("Hours = " & hours)
   Log("Minutes = " & minutes)
   Log("Seconds = " & seconds)
End Sub
 
Last edited:
Upvote 0

wheretheidivides

Active Member
Licensed User
Longtime User
"0, 0, 59, 0 (WRONG-IT'S THE SAME DAY. SHOULD BE 0,0,-59,0) (That's correct. The sub name is DateBetweenTwoDates not DateBetweenStartDateAndEndDate. See below.)"

I though Erel was responding back to my post about trying to get days, hours, mins and seconds between 2 dates. I thought that was the point.
======================================

I use strings in DateBetweenTwoDates(CalcDateIn, CalcTimeIn, CalcDateOut, CalcTimeOut). the actual stings are listed in the pictures attached.

These are in pairs of 2, The first picture is the raw data supplied the subroutine and the second picture is the data sent to it (first date, first time, second date, second time)
 
Last edited:
Upvote 0

MLDev

Active Member
Licensed User
Longtime User
Something wrong with your code. You're getting different results then I'm getting. Can you post your code?
 
Upvote 0

wheretheidivides

Active Member
Licensed User
Longtime User
Something wrong with your code. You're getting different results then I'm getting. Can you post your code?


The input are strings as shown in the pictures (CalcDateIn, CalcTimeIn, CalcDateOut, CalcTimeOut). The actual format are shown in the pictures. Maybe that has something to do with it. Maybe the format had a 2/2/2012 and needs 02/02/2012? Heck if I know, I'm a beginner.

B4X:
   DateBetweenTwoDates(CalcDateIn, CalcTimeIn, CalcDateOut, CalcTimeOut)
   
   Msgbox(CalcDateIn & CRLF & CalcTimeIn & CRLF & CalcDateOut & CRLF & CalcTimeOut, "OK")

   'Put up dates
      Label1803a.Text = CalcDateIn
      Label1803b.Text = CalcDateOut


The times input came from a input dialog (one for time and 1 for date). This code doesn't matter and they just give a string with the date and another string with the time (both for start and end). These strings are put into (CalcDateIn, CalcTimeIn, CalcDateOut, CalcTimeOut). You can see the input string on the pictures.
 
Last edited:
Upvote 0

MLDev

Active Member
Licensed User
Longtime User

This doesn't help. You're not showing me how you're getting and displaying the results. If you can't post the code PM it to me.
 
Upvote 0

wheretheidivides

Active Member
Licensed User
Longtime User
"This doesn't help. You're not showing me how you're getting and displaying the results. If you can't post the code PM it to me."

Well I get the results from that subroutine and am just displaying on a message box. The 4 strings input are shown on the pictures. These are the 2 dates and 2 times.

B4X:
   DateBetweenTwoDates(CalcDateIn, CalcTimeIn, CalcDateOut, CalcTimeOut)
   
   Msgbox(CalcDateIn & CRLF & CalcTimeIn & CRLF & CalcDateOut & CRLF & CalcTimeOut, "OK")

Here's erel's code.
B4X:
Sub DateBetweenTwoDates(D1 As String, T1 As String, D2 As String, T2 As String)
    Dim start, endTime, t As Long
    start = DateTime.DateTimeParse(D1, T1)
    endTime = DateTime.DateTimeParse(D2, T2)
    Dim days, hours, minutes, seconds As Int
    t = Abs(endTime - start)
    days = Floor(t / DateTime.TicksPerDay)
    hours = Floor((t Mod DateTime.TicksPerDay) / DateTime.TicksPerHour)
    minutes = Floor((t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute)
    seconds = Floor((t Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond)
    
    Log("Days = " & days)
    Log("Hours = " & hours)
    Log("Minutes = " & minutes)
    Log("Seconds = " & seconds)
   Msgbox("Days = " & days & CRLF & "Hours = " & hours & CRLF & "Minutes = " & minutes & CRLF & "Seconds = " & seconds, "OK")
   
End Sub

It's just that. The message box displays the input and another the output.
Now, where the 4 input strings come from is another thing. They are in those variables and the actual strings are displayed in the pictures.
 
Upvote 0

wheretheidivides

Active Member
Licensed User
Longtime User
Now here is the date and time that is supplies to those 4 variables. It's kind of stupid, but seems to wrok on getting the 4 strings.

This is first panel to set up stuff
B4X:
=========================================================================================
'----------------------------------------
'put today's date into current variables so when auser open up income, it always shows today's date
   IncYearIn = TodayYear
   IncMonthIn = TodayMonth
   IncDayOfMonthIn = TodayDayOfMonth
   
   IncDayStringIn = TodayDayString
   IncDayNumIn = TodayDayNum
      
   IncMMDDYYYYIn = TodayMMDDYYYY
   IncComboIn = TodayCombo
'----------------------------------------------
   IncYearOut = TodayYear
   IncMonthOut = TodayMonth
   IncDayOfMonthOut = TodayDayOfMonth
   
   IncDayStringOut = TodayDayString
   IncDayNumOut = TodayDayNum
      
   IncMMDDYYYYOut = TodayMMDDYYYY
   IncComboOut = TodayCombo
'--------------------------------------------
'put day and date on label
   Label1701DateIn.text = IncDayStringIn & ", " & IncMMDDYYYYIn
   Label1701DateOut.text = IncDayStringOut & ", " & IncMMDDYYYYOut

'-----------------------------------------
'Clear In time
   IncTimeInHour = 0 '24 hour true time
   IncTimeInHourB = 12 '12 hour
   
   IncTimeInMinute = 00
   IncTimeInMinuteString = "00"  
   
   IncTimeInAMPM = "AM"
   
'---------------------------------------------
'Clear Out time
   IncTimeOutHour = 0 '24 hour true time
   IncTimeOutHourB = 12 '12 hour
   
   IncTimeOutMinute = 00
   IncTimeOutMinuteString = "00"
   
   IncTimeOutAMPM = "AM"
   
'---------------------------------------------------------------
'put into time adn date string for calc in case use doesnt change day or time
   CalcTimeIn = IncTimeInHour & ":" & IncTimeInMinute & ":" & "00"
   CalcTimeOut = IncTimeOutHour & ":" & IncTimeOutMinute & ":" & "00"
   
   CalcDateIn = IncMonthIn & "/" & IncDayOfMonthIn & "/" & IncYearIn
   CalcDateOut = IncMonthOut & "/" & IncDayOfMonthOut & "/" & IncYearOut

'---------------------------------------------
'Put variables into label
   Label1702ClockIn.text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM 'default In time
   Label1703ClockOut.text = IncTimeOutHourB & ":"  & IncTimeOutMinuteString & IncTimeOutAMPM 'default out time 


'=========================================================================================

Then these are the 2nd panel that has buttons to click for date and time dialog boxes
B4X:
Sub Button1701DateIn_Click
'---------------------------------------------
'show date dialog box in pop up box.
   Dim RetDateIn As Int 
   
   DdIn.Year = DateTime.GetYear(DateTime.now)
   DdIn.Month = DateTime.GetMonth(DateTime.now)   
   DdIn.DayOfMonth = DateTime.GetDayOfMonth(DateTime.now)
'-----------------------------
   IncDayNumIn = DateTime.GetDayOfWeek(DdIn.DateTicks)
   
   Select IncDayNumIn
         Case 1
            IncDayMDYIn = "Right Now, it is Sunday: " & DdIn.Month & "/" & DdIn.DayOfMonth & "/" & DdIn.Year
         Case 2
            IncDayMDYIn = "Right Now, it is Monday: " & DdIn.Month & "/" & DdIn.DayOfMonth & "/" & DdIn.Year
         Case 3
            IncDayMDYIn = "Right Now, it is Tuesday: " & DdIn.Month & "/" & DdIn.DayOfMonth & "/" & DdIn.Year
         Case 4
            IncDayMDYIn = "Right Now, it is Wednesday: " & DdIn.Month & "/" & DdIn.DayOfMonth & "/" & DdIn.Year
         Case 5
            IncDayMDYIn = "Right Now, it is Thursday: " & DdIn.Month & "/" & DdIn.DayOfMonth & "/" & DdIn.Year
         Case 6
            IncDayMDYIn = "Right Now, it is Friday: " & DdIn.Month & "/" & DdIn.DayOfMonth & "/" & DdIn.Year
         Case 7
            IncDayMDYIn = "Right Now, it is Saturday: " & DdIn.Month & "/" & DdIn.DayOfMonth & "/" & DdIn.Year
      End Select
   '---------------------------------------------------------
   RetDateIn = DdIn.Show(IncDayMDYIn, "Start of Workday?", "Save", "", "", Bmp)
   
'============================================================================================
'change top line with new day and date
   IncDayNumIn = DateTime.GetDayOfWeek(DdIn.DateTicks)
   
   Select IncDayNumIn
         Case 1
            Label1701DateIn.text = ("Sunday, " & DdIn.Month & "/" & DdIn.DayOfMonth & "/" & DdIn.Year)
            IncDayStringIn = "Sunday"
         Case 2
            Label1701DateIn.text = ("Monday, " & DdIn.Month & "/" & DdIn.DayOfMonth & "/" & DdIn.Year)
            IncDayStringIn = "Monday"
         Case 3
            Label1701DateIn.text = ("Tuesday, " & DdIn.Month & "/" & DdIn.DayOfMonth & "/" & DdIn.Year)
            IncDayStringIn = "Tuesday"
         Case 4
            Label1701DateIn.text = ("Wednesday, " & DdIn.Month & "/" & DdIn.DayOfMonth & "/" & DdIn.Year)
            IncDayStringIn = "Wednesday"
         Case 5
            Label1701DateIn.text = ("Thursday, " & DdIn.Month & "/" & DdIn.DayOfMonth & "/" & DdIn.Year)
            IncDayStringIn = "Thursday"
         Case 6
            Label1701DateIn.text = ("Friday, " & DdIn.Month & "/" & DdIn.DayOfMonth & "/" & DdIn.Year)
            IncDayStringIn = "Friday"
         Case 7
            Label1701DateIn.text = ("Saturday, " & DdIn.Month & "/" & DdIn.DayOfMonth & "/" & DdIn.Year)
            IncDayStringIn = "Saturday"
      End Select

'-----------------------------------------------------------------------
'copy over variables
   IncYearIn = DdIn.Year
   IncMonthIn = DdIn.Month
   IncDayOfMonthIn = DdIn.DayOfMonth
   IncMMDDYYYYIn =  DdIn.Month & "/" & DdIn.DayOfMonth & "/" & DdIn.Year
   
   IncComboIn = IncDayStringIn & ", " & IncMMDDYYYYIn
   
   CalcDateIn = DdIn.Month & "/" & DdIn.DayOfMonth & "/" & DdIn.Year
   
'---------------------------------------------
End Sub

Sub Button1701DateOut_Click
'---------------------------------------------
'show date dialog box in pop up box.
   Dim RetDateOut As Int 
   
   DdOut.Year = DateTime.GetYear(DateTime.now)
   DdOut.Month = DateTime.GetMonth(DateTime.now)   
   DdOut.DayOfMonth = DateTime.GetDayOfMonth(DateTime.now)
'-----------------------------
   IncDayNumOut = DateTime.GetDayOfWeek(DdOut.DateTicks)
   
   Select IncDayNumOut
         Case 1
            IncDayMDYOut = "Right Now, it is Sunday: " & DdOut.Month & "/" & DdOut.DayOfMonth & "/" & DdOut.Year
         Case 2
            IncDayMDYOut = "Right Now, it is Monday: " & DdOut.Month & "/" & DdOut.DayOfMonth & "/" & DdOut.Year
         Case 3
            IncDayMDYOut = "Right Now, it is Tuesday: " & DdOut.Month & "/" & DdOut.DayOfMonth & "/" & DdOut.Year
         Case 4
            IncDayMDYOut = "Right Now, it is Wednesday: " & DdOut.Month & "/" & DdOut.DayOfMonth & "/" & DdOut.Year
         Case 5
            IncDayMDYOut = "Right Now, it is Thursday: " & DdOut.Month & "/" & DdOut.DayOfMonth & "/" & DdOut.Year
         Case 6
            IncDayMDYOut = "Right Now, it is Friday: " & DdOut.Month & "/" & DdOut.DayOfMonth & "/" & DdOut.Year
         Case 7
            IncDayMDYOut = "Right Now, it is Saturday: " & DdOut.Month & "/" & DdOut.DayOfMonth & "/" & DdOut.Year
      End Select
   '---------------------------------------------------------
   RetDateOut = DdOut.Show(IncDayMDYOut, "Start of Workday?", "Save", "", "", Bmp)
   
'============================================================================================
'change top line with new day and date
   IncDayNumOut = DateTime.GetDayOfWeek(DdOut.DateTicks)
   
   Select IncDayNumOut
         Case 1
            Label1701DateOut.text = ("Sunday, " & DdOut.Month & "/" & DdOut.DayOfMonth & "/" & DdOut.Year)
            IncDayStringOut = "Sunday"
         Case 2
            Label1701DateOut.text = ("Monday, " & DdOut.Month & "/" & DdOut.DayOfMonth & "/" & DdOut.Year)
            IncDayStringOut = "Monday"
         Case 3
            Label1701DateOut.text = ("Tuesday, " & DdOut.Month & "/" & DdOut.DayOfMonth & "/" & DdOut.Year)
            IncDayStringOut = "Tuesday"
         Case 4
            Label1701DateOut.text = ("Wednesday, " & DdOut.Month & "/" & DdOut.DayOfMonth & "/" & DdOut.Year)
            IncDayStringOut = "Wednesday"
         Case 5
            Label1701DateOut.text = ("Thursday, " & DdOut.Month & "/" & DdOut.DayOfMonth & "/" & DdOut.Year)
            IncDayStringOut = "Thursday"
         Case 6
            Label1701DateOut.text = ("Friday, " & DdOut.Month & "/" & DdOut.DayOfMonth & "/" & DdOut.Year)
            IncDayStringOut = "Friday"
         Case 7
            Label1701DateOut.text = ("Saturday, " & DdOut.Month & "/" & DdOut.DayOfMonth & "/" & DdOut.Year)
            IncDayStringOut = "Saturday"
      End Select

'-----------------------------------------------------------------------
'copy over variables
   IncYearOut = DdOut.Year
   IncMonthOut = DdOut.Month
   IncDayOfMonthOut = DdOut.DayOfMonth
   IncMMDDYYYYOut =  DdOut.Month & "/" & DdOut.DayOfMonth & "/" & DdOut.Year
   
   IncComboOut = IncDayStringOut & ", " & IncMMDDYYYYOut
   
   CalcDateOut = DdOut.Month & "/" & DdOut.DayOfMonth & "/" & DdOut.Year

'---------------------------------------------
End Sub
Sub Button1702ClockIn_Click
'------------------------------------------
   Td.Hour = IncTimeInHour 'put hour and time in so clock starts with current time input
   Td.Minute = IncTimeInMinute
   
   RetModify = Td.Show("What time did you clock in at?", "Start Time", "", "Cancel", "", Bmp)

'--------------------------------------------------
'Add time to variables
   IncTimeInHour = Td.Hour 'new time user inputed
   IncTimeInMinute = Td.Minute 'new minute user inputes
   
'-----------------------------------------
'set to 2 places always
   IncTimeInMinuteString = NumberFormat2(IncTimeInMinute, 2, 0, 0, False) '2 digit left of decimal, 0 right of decimal

'------------------------------------------
'display correct hour
   Select IncTimeInHour
      Case 0
         IncTimeInAMPM = "AM"
         IncTimeInHourB = 12
         Label1702ClockIn.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM
      Case 1
         IncTimeInAMPM = "AM"
         IncTimeInHourB = 1
         Label1702ClockIn.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM
      Case 2
         IncTimeInAMPM = "AM"
         IncTimeInHourB = 2
         Label1702ClockIn.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM
      Case 3
         IncTimeInAMPM = "AM"
         IncTimeInHourB = 3
         Label1702ClockIn.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM
      Case 4
         IncTimeInAMPM = "AM"
         IncTimeInHourB = 4
         Label1702ClockIn.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM
      Case 5
         IncTimeInAMPM = "AM"
         IncTimeInHourB = 5
         Label1702ClockIn.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM
      Case 6
         IncTimeInAMPM = "AM"
         IncTimeInHourB = 6
         Label1702ClockIn.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM
      Case 7
         IncTimeInAMPM = "AM"
         IncTimeInHourB = 7
         Label1702ClockIn.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM
      Case 8 
         IncTimeInAMPM = "AM"
         IncTimeInHourB = 8
         Label1702ClockIn.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM
      Case 9
         IncTimeInAMPM = "AM"
         IncTimeInHourB = 9
         Label1702ClockIn.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM
      Case 10
         IncTimeInAMPM = "AM"
         IncTimeInHourB = 10
         Label1702ClockIn.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM
      Case 11
         IncTimeInAMPM = "AM"
         IncTimeInHourB = 11
         Label1702ClockIn.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM
      '---------------------------------------------------------
      Case 12
         IncTimeInAMPM = "PM"
         IncTimeInHourB = 12   
         Label1702ClockIn.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM
      Case 13
         IncTimeInAMPM = "PM"
         IncTimeInHourB = 1
         Label1702ClockIn.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM
      Case 14
         IncTimeInAMPM = "PM"
         IncTimeInHourB = 2   
         Label1702ClockIn.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM
      Case 15
         IncTimeInAMPM = "PM"
         IncTimeInHourB = 3   
         Label1702ClockIn.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM
      Case 16
         IncTimeInAMPM = "PM"
         IncTimeInHourB = 4
         Label1702ClockIn.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM
      Case 17
         IncTimeInAMPM = "PM"
         IncTimeInHourB = 5
         Label1702ClockIn.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM
      Case 18
         IncTimeInAMPM = "PM"
         IncTimeInHourB = 6
         Label1702ClockIn.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM
      Case 19
         IncTimeInAMPM = "PM"
         IncTimeInHourB = 7
         Label1702ClockIn.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM
      Case 20
         IncTimeInAMPM = "PM"
         IncTimeInHourB = 8
         Label1702ClockIn.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM
      Case 21
         IncTimeInAMPM = "PM"
         IncTimeInHourB = 9
         Label1702ClockIn.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM
      Case 22
         IncTimeInAMPM = "PM"
         IncTimeInHourB = 10
         Label1702ClockIn.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM
      Case 23
         IncTimeInAMPM = "PM"
         IncTimeInHourB = 11
         Label1702ClockIn.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM
      Case 24
         IncTimeInAMPM = "PM"
         IncTimeInHourB = 12         
         Label1702ClockIn.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & IncTimeInAMPM
      
   End Select
   
'--------------------------------------------------
'put into time for calc
   CalcTimeIn = Td.Hour & ":" & Td.Minute & ":" & "00"

'---------------------------------------------
End Sub

Sub Button1703ClockOut_Click
'------------------------------------------
   Td.Hour = IncTimeOutHour '24 hour format
   Td.Minute = IncTimeOutMinute
   
   RetModify = Td.Show("What time did you clock out at?", "End Time", "Save", "", "", Bmp)

'------------------------------------------
'Add time to variables
   IncTimeOutHour = Td.Hour 'new hour from user
   IncTimeOutMinute = Td.Minute 'new mins from user

'-----------------------------------------
'set to 2 places always
   IncTimeOutMinuteString = NumberFormat2(IncTimeOutMinute, 2, 0, 0, False) '2 digit left of decimal, 0 right of decimal

'------------------------------------------
'display correct hour
   Select IncTimeOutHour
      Case 0
         IncTimeOutAMPM = "AM"
         IncTimeOutHourB = 12 
         Label1703ClockOut.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & IncTimeOutAMPM
      Case 1
         IncTimeOutAMPM = "AM"
         IncTimeOutHourB = 1
         Label1703ClockOut.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & IncTimeOutAMPM
      Case 2
         IncTimeOutAMPM = "AM"
         IncTimeOutHourB = 2
         Label1703ClockOut.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & IncTimeOutAMPM
      Case 3
         IncTimeOutAMPM = "AM"
         IncTimeOutHourB = 3
         Label1703ClockOut.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & IncTimeOutAMPM
      Case 4
         IncTimeOutAMPM = "AM"
         IncTimeOutHourB = 4
         Label1703ClockOut.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & IncTimeOutAMPM
      Case 5
         IncTimeOutAMPM = "AM"
         IncTimeOutHourB = 5
         Label1703ClockOut.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & IncTimeOutAMPM
      Case 6
         IncTimeOutAMPM = "AM"
         IncTimeOutHourB = 6
         Label1703ClockOut.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & IncTimeOutAMPM
      Case 7
         IncTimeOutAMPM = "AM"
         IncTimeOutHourB = 7
         Label1703ClockOut.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & IncTimeOutAMPM
      Case 8 
         IncTimeOutAMPM = "AM"
         IncTimeOutHourB = 8
         Label1703ClockOut.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & IncTimeOutAMPM
      Case 9
         IncTimeOutAMPM = "AM"
         IncTimeOutHourB = 9
         Label1703ClockOut.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & IncTimeOutAMPM
      Case 10
         IncTimeOutAMPM = "AM"
         IncTimeOutHourB = 10
         Label1703ClockOut.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & IncTimeOutAMPM
      Case 11
         IncTimeOutAMPM = "AM"
         IncTimeOutHourB = 11
         Label1703ClockOut.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & IncTimeOutAMPM
      '---------------------------------------------------------
      Case 12
         IncTimeOutAMPM = "PM"
         IncTimeOutHourB = 12   
         Label1703ClockOut.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & IncTimeOutAMPM
      Case 13
         IncTimeOutAMPM = "PM"
         IncTimeOutHourB = 1
         Label1703ClockOut.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & IncTimeOutAMPM
      Case 14
         IncTimeOutAMPM = "PM"
         IncTimeOutHourB = 2   
         Label1703ClockOut.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & IncTimeOutAMPM
      Case 15
         IncTimeOutAMPM = "PM"
         IncTimeOutHourB = 3   
         Label1703ClockOut.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & IncTimeOutAMPM
      Case 16
         IncTimeOutAMPM = "PM"
         IncTimeOutHourB = 4
         Label1703ClockOut.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & IncTimeOutAMPM
      Case 17
         IncTimeOutAMPM = "PM"
         IncTimeOutHourB = 5
         Label1703ClockOut.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & IncTimeOutAMPM
      Case 18
         IncTimeOutAMPM = "PM"
         IncTimeOutHourB = 6
         Label1703ClockOut.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & IncTimeOutAMPM
      Case 19
         IncTimeOutAMPM = "PM"
         IncTimeOutHourB = 7
         Label1703ClockOut.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & IncTimeOutAMPM
      Case 20
         IncTimeOutAMPM = "PM"
         IncTimeOutHourB = 8
         Label1703ClockOut.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & IncTimeOutAMPM
      Case 21
         IncTimeOutAMPM = "PM"
         IncTimeOutHourB = 9
         Label1703ClockOut.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & IncTimeOutAMPM
      Case 22
         IncTimeOutAMPM = "PM"
         IncTimeOutHourB = 10
         Label1703ClockOut.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & IncTimeOutAMPM
      Case 23
         IncTimeOutAMPM = "PM"
         IncTimeOutHourB = 11
         Label1703ClockOut.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & IncTimeOutAMPM
      Case 24
         IncTimeOutAMPM = "PM"
         IncTimeOutHourB = 12         
         Label1703ClockOut.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & IncTimeOutAMPM 
   End Select

'--------------------------------------------------
'put into time for calc
   CalcTimeOut = Td.Hour & ":" & Td.Minute & ":" & "00"
   
'---------------------------------------------
End Sub

then this is the next panel that does the calcs
B4X:
   DateBetweenTwoDates(CalcDateIn, CalcTimeIn, CalcDateOut, CalcTimeOut)
   
   Msgbox(CalcDateIn & CRLF & CalcTimeIn & CRLF & CalcDateOut & CRLF & CalcTimeOut, "OK")

   'Put up dates
      Label1803a.Text = CalcDateIn
      Label1803b.Text = CalcDateOut
   '----------------
   'put up times
      Label1803aa.Text = IncTimeInHourB & ":" & IncTimeInMinuteString & " " & IncTimeInAMPM
      Label1803bb.Text = IncTimeOutHourB & ":" & IncTimeOutMinuteString & " " & IncTimeOutAMPM
      Label1803cc.Text = "(" & hours & " Hrs, " & minutes & " Mins)"
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
You cannot use Erel's code if the end date is earlier than the start date because the calculations make use of the Floor function. Here is an example:
B4X:
Msgbox(Floor(59.5),"")   'returns 59
Msgbox(Floor(-59.5),"")   'returns -60
If you want to use a start date bigger than end date you need to use the Absolute value like this: t = Abs(e - s)
 
Upvote 0

MLDev

Active Member
Licensed User
Longtime User

That's not the problem. See my post #23.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
That's not the problem. See my post #23.
I did see post #23. I was actually corroborating it. Could you concisely tell us what the problem is with one simple example. I have a routine I use to calculate days, hrs, mn, etc. I would like to verify my calculations.
 
Upvote 0

MLDev

Active Member
Licensed User
Longtime User
I did see post #23. I was actually corroborating it. Could you concisely tell us what the problem is with one simple example. I have a routine I use to calculate days, hrs, mn, etc. I would like to verify my calculations.

Erel's code works correctly with that bug fix. wheretheidivides is having problems. The code he posted works but I can't see where he's getting his results.
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
Hello Friends,
how can i use that sample for another case:

i have 4 Start days (i dont need hours, minutes & seconds) and each of them CAN have a different value.
For each of these 4 dates i want to add a timespan in full days (i.e. Number 1 = 90, Number 2 = 60 etc.)

Now i (simple) want to add the days to the relating start dates.

I code normally in visual basic and i am just a Newbie here :sign0013:.

I used that sample code from Erel with fixed times (00:00) but an error occured:
"unparseable date": 12.04.2013 (thats a start date)

Is it because i am using not the US-format 04/12/13 ?
And if, can i change (via B4A) the format so that it fits ?

Thx in advance
Gman

(Sorry for my english, its not my native language )
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
Hi NJDude,

of course not
But now i did

As i understand the system, i have to include the Module as a module in my B4A and simple call the routine
B4X:
'Returns the ticks value of the given date (the time will be 00:00:00).
Sub SetDate(Years As Int, Months As Int, Days As Int) As Long

Thx so far
Gman
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…