Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session

Elapsed Days between Two Date Fields

Avatar

Level 9

I have two date/time objects that record the current time and date when a button is clicked (two different buttons, one for each date/time object). I would like to calculate the elapsed days in another field. Ideally it would calculate the elapsed days, hours and minutes.

I have tried for hours to figure this out. I can make it work when it is just two dates without the time. I found using Date2Num and FormCalc I can get the elapsed number of days. Is it possible to get the elapsed number of days, hours and minutes?

All of the date/time expressions I try do not work and I can't find additional examples. Currently my date and time is formated this way but can be changed if needed:

TimeStampToFT.rawValue = Concat(Num2Date(date(), "EEE M/D/YYYY "), Num2Time(time(), "h:MM A"))

and displays like this:  Tue 3/5/2013 10:33 AM

I unsuccessfully tried to get just the date to convert to a number using this script: (FormCalc and Textfield)

this.rawValue = Date2Num(DateField1.formattedValue, "EEE M/D/YYYY")

Help with this would be appreciated.

6 Replies

Avatar

Level 5

Hi,

at the moment I have (only) a script to calculate the days between two days. Maybe you could combine with the timestamp.

The follwoing script you have to use in the calculate-event your targetfield which shows the number of days between.

Important - You have to change

  • DatumsUhrzeitfeld2 change to the objectname from your second date field
  • DatumsUhrzeitfeld1 change to the objectname from your first date field

if (Date2Num(DatumsUhrzeitfeld2, "YYYY-MM-DD") and Date2Num(DatumsUhrzeitfeld1, "YYYY-MM-DD")) then

    if (Date2Num(DatumsUhrzeitfeld2, "YYYY-MM-DD") < Date2Num(DatumsUhrzeitfeld1, "YYYY-MM-DD")) then

        xfa.host.messageBox("The second date is earlier then the first date.\u000aThe date fields will reset.", "Information", 3, 1)

        DatumsUhrzeitfeld1 = null

        DatumsUhrzeitfeld2 = null

        $ = null

        xfa.host.setFocus("DatumsUhrzeitfeld1")

    else

        $ = Date2Num(DatumsUhrzeitfeld2, "YYYY-MM-DD") - Date2Num(DatumsUhrzeitfeld1, "YYYY-MM-DD") + 1

    endif

else

    $ = null

endif

Maybe you could combine the above script with one of these timestamp scripts:

TextField1.rawValue = util.printd("dd.mm.yyyy HH:MM", new Date());

TextField2.rawValue = util.printd("dddd d. mmmm yyyy HH:MM ", new Date());

TextField3.rawValue = util.printd("date{EEEE, D. MMMM YYYY} time(de){HH:MM:SS Z}", new Date(), true)

I hope it will helps,

Mandy

Avatar

Level 9

Mandy,

Thank you for your reply. I tried converting the date format to a number using Date2Num but it still will not work. My timestamp has a date and time (Example: Mon 3/4/2013 3:47 PM).

This is what I have so far. The first line of code is the timestamp. The second line of code is in a textfield trying to convert the date/time field to number of days.  Ideally I would like to subtract two timestamps and have a result of days, hours and minutes elapsed.

TimeStamp2.rawValue = util.printd("date{EEE M/D/YYYY}time{ h:MM A}", new Date(),true)

this.rawValue = Date2Num(TimeStamp2.formattedValue, "date{EEE M/D/YYYY}")

Avatar

Level 1

DKinsley,

This is something that I am currently trying to figure out, and although you worked on this almost 2 years ago, I was wondering if you ever were able to figure out this problem. If so, could you possibly help me?

Thanks!

Avatar

Level 9

KZ1030:

I was never able to figure this one out.

Sorry I am not able to help you out.

-Don

Avatar

Level 1

Mandy,

I tried to use your directions for a form in which I need to calculate the total days between 2 dates as well. However it wouldn't work for me.

I'm not sure what I'm doing wrong. Initially, before I saw your post, I was this .....

form1.P1.ShipAVAILs.AvailBinder.AvailInstance.ShipSpacer.ShipBoarder.NumericField102::calculate - (FormCalc, client)

if (Date2Num(AvailEndDate, "MM/DD/YY") and (Date2Num(AvailStartDate, "MM/DD/YY"))) then

         NumericField102 = (Date2Num(AvailEndDate, "MM/DD/YY") - Date2Num(AvailStartDate, "MM/DD/YY")) + 1

else

     NumericField102 = "0"

endif

Would you mind taking a look please and provide any feedback possible?

Avatar

Level 1

Adam,


Since the Date2Num function takes string as argument you need to convert the date value of the field to string. This can be done using the Format function as below:


NumericField102 = (Date2Num(Format("MM/DD/YY", AvailEndDate), "MM/DD/YY") - Date2Num(Format("MM/DD/YY", AvailStartDate), "MM/DD/YY")) + 1

-fjahan