Skip to content
Discussions/App Development/Is there any method to get current Date, similar to getTime methodForum ↗

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

App Development9 posts924 views6 likesLast activity May 2022
MU
Muhammad_MoizOP
Oct 2020

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

CO
cocreature
Oct 2020

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
MU
Muhammad_Moiz
Oct 2020

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
CO
cocreature
Oct 2020

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.

MU
Muhammad_Moiz
Oct 2020

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

CO
cocreature
Oct 2020

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

CO
cocreature
May 2021

A post was split to a new topic: Access to time during contract creation

QU
quidagis
May 2021
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

NE
nemanja
May 2022

A post was split to a new topic: Type mismatch error

← Back to Discussions