Question

I have 2 dates with hours like : '2013-01-01 12:25:32' (yyyy-mm-dd hh:mm:ss) and I should find the difference between DateA and DateB in minutes . How can I do this?

Thank you

Was it helpful?

Solution

VFP handles date and datetime math easily as long as both are same date or date/time format.

Oleg is correct about converting the data types if they are in a string format vs an actual date or date/time format, but you obviously have VFP readily available. Try some of these to get a feel on what is going on.

IF BOTH FIELDS ARE DATE ONLY, the addition/subtraction is based on DAYS

ldStartDate = date( 2013, 12, 5 )
ldOtherDate = date( 2013, 11, 24 )

nDaysDiff = ldStartDate - ldOtherDate

resulting in 11 days.

Compute first of a given month?

ldFirstOfMonth = ldStartDate - DAY( ldStartDate ) +1

since the example day() would return 5 (5th of December), so Dec 5 - 5 days = Last Day of November +1 brings back to Dec 1.

Now, with date/time fields -- date/time is based on SECONDS difference

ldStartTime = DATETIME( 2013, 12, 5, 8, 56, 00 )
ldOtherTime = DATETIME( 2013, 11, 24, 12, 00, 00 )
nSecDiff = ldStartTime - ldOtherTime

? nSecDiff
result = 939360 seconds

? nSecDiff / 60
result = 15656 minutes

? nSecDiff / 60 / 60
result = 260.93 hours

? nSecDiff / 60 / 60 / 24
result is 10.87 days

Oh... And forgot... If dealing with numeric values to date/datetime, they are respective to days (as in sample to compute first of a month) and seconds result of date/time fields.

So

? ldStartDate + 10 
would result in Dec 5 + 10 days or Dec 15th

? ldStartDate - 3
would result in Dec 5 - 3 days or Dec 2nd

** from when ldStartTime initially set to Dec 5 at 8:56:00am
? ldStartTime + 20
would be Dec 5, 8:56:20am

? ldStartTime + (20*60)
would be Dec 5, 8:56:00am + 20 MINUTES = 9:16am

? ldStartTime + (1*60*60)
would be Dec 5, 8:56:00am + 1 HOUR = 9:56am

? ldStartTime + (1*60*60*24)
would be Dec 5, 8:56:00am + 1 DAT = Dec 6, 8:56am

OTHER TIPS

You could use CTOT() function, but since your string contains whitespace symbol instead of 'T' symbol, it should be replaced first:

?CTOT(CHRTRAN('2013-01-01 12:25:35', ' ', 'T'))-CTOT(CHRTRAN('2013-01-01 12:25:32', ' ', 'T'))

The result will be 3.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top