Time Problem

vikram7_dabas
vikram7_dabas Member Posts: 611
Dear Concern
I have 1 new tabel "Attendence" in which there r 4 fields:
1). Date
2).Start Time
3).End Time
4). Total Hours worked

Suppose i m filling the attendence of today(,say Date=30/05/2009),Start Time = 10:00 P.M(Night of 30/05/09) and "End Time" = 6:00 A.M (till moarning of 31/05/09), when user fill "End Time" field,then "Total Hours worked" should come automatically.But keep in mind "Total Hours worked" should come in Date '30/05/09'.How I will do this.Plzz explain the code in Detail?
Vikram Dabas
Navision Technical Consultant

Comments

  • kine
    kine Member Posts: 12,562
    Best will be to add the End Date to have Start date and End date fields to have enough details to calculate the difference (you can than create the DateTime variables for Start and End, subtract the value and you will receive the duration data type which you can recalculate to hours...).
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • bylle
    bylle Member Posts: 47
    I'm wondering why you just want to display Hours Worked as a Date. In this case, you could just add an End Date field ;-)

    If you really want to calculate the exact hours worked - you must also know the End Date. So like kine wrote, you should add an End Date field or you could also use DateTime as date type. In this way you should only have one field for Start Date and one for End Date.

    Ex.
    Variable:
    StartDateTime : DataType DateTime
    EndDateTime   : DateType DateTime
    HoursWorked   : Integer
    
    When inserting in the Attendence table:
    Attendence.StartDateTime := CURRENTDATETIME;
    Attendence.INSERT;
    
    On the Validate Trigger for EndDateTime add:
    HoursWorked := (EndDateTime - StartDateTime) DIV 3600000; 
    
    (EndDateTime - StartDateTime) is in milliseconds, so to get hours with have to do a division with first 1000 (=seconds), then with 60 (=minutes) and then with 60 (=hours). 
    
    So when you now add the EndDateTime, HoursWorked will be calculated automatically:
    Attendence.VALIDATE(EndDateTime, CURRENTDATETIME);
    Attendence.MODIFY(TRUE); 
    
    In case you would like to keep 2 fields per Start and End, you can still use the above example. Just change the HoursWorked calculation to the following:
    StartDateTime := CREATEDATETIME(StartDate, StartTime);
    EndDateTime := CREATEDATETIME(EndDate, EndTime);
    HoursWorked := (EndDateTime - StartDateTime) DIV 3600000;
    
  • devu_13
    devu_13 Member Posts: 101
    Hi
    You just take a Duration type variable it will give you time between StartTime to EndTime.
    Devendra Kr. Sharma
    IBIZ Consulting Services,India
  • bylle
    bylle Member Posts: 47
    Yes - Duration is also a great idea.

    Just notice, that you then will have the value in the following format: "13 hours 21 minutes 55 seconds 781 milliseconds"

    Depending on what you would like to use the result for - you can either use Duration or calculate HoursWorked by yourself.
    My example calculates whole hours, which means that HoursWorked will be 13 and not "13 hours 21 minutes 55 seconds 781 milliseconds".

    (If you like to have the decimals, then exchange DIV with / ;-) )
  • vikram7_dabas
    vikram7_dabas Member Posts: 611
    Dear Concern
    What should be the Duration Type?"Start Time" or "End Time" or "Total Hours" in Daily attendence table?
    Vikram Dabas
    Navision Technical Consultant
  • bylle
    bylle Member Posts: 47
    "Total Hours" should be Duration.

    But remember, that if you choose to use duration - you must use DateTime variables to calculate the duration.

    Check out Help (F1) ;-)
    Use this data type to represent the difference between two datetimes, in milliseconds.
    This value can be negative. It is a 64 bit integer.

    DATETIME-DATETIME=DURATION