Skip to content
CCPEDIAby Unity Nodes
App Development

Is there any method to get current Date, similar to getTime method

7 posts982 views6 likesLast activity May 2021

  1. Muhammad_MoizOP 1

    I want to fetch only current Date when the contract is created, is there any , method to get Date similar to getTime method.

  2. cocreature 2

    There is no getDate but you can use getTime and then call toDateUTC to get the date of the timestamp. So as an example in a DAML Script, something like this:

    import  DA.Time
    import DA.Date
    testScript = do
      time <- getTime
      let date = toDateUTC time
      debug date
    
  3. Muhammad_Moiz

    But I want to set a variable “punchInDate” when contract is created automatically
    sample code

    controller createdBy can
                PunchIn
                    : ContractId Attendance
                    with
                        _associateId       : ContractId Associate
                        _communityId       : ContractId Community
                      
                    do
                        _punchInTime  <-  getTime
                        --punchInDate          =    toDateUTC time                                            
    
                        create this with
                            communityId       = _communityId
                            associateId          = _associateId
                            punchInTime        = _punchInTime
                            --punchInDate        = _punchInDate
  4. cocreature 1

    Not sure I’m following. The code you’ve written already seems to do what you are asking for if you include the uncommented code (and change punchInDate = toDateUTC time to let punchInDate = toDateUTC time).

    However, I would recommend against storing both time and date in the contract. If you need the time as well, store that and call toDateUTC when you use it. That way time and date can never get out of sync. If you only need the date, just remove the time field.

  5. Muhammad_Moiz

    --punchInDate = toDateUTC time
    This line show me error
    parse error on input ‘=’parser

  6. cocreature 1

    Right, you need the let before that as I suggested above

  7. quidagis 1
    cocreature:

    However, I would recommend against storing both time and date in the contract. If you need the time as well, store that and call toDateUTC when you use it.

    That’s a great Time/Date tip, thanks @cocreature