How do you get the current Date in Daml?
- amiracamOP 1
I checked the Daml docs for Date.
I can’t find what would instantiate the current date.
I’m using this in a scenario for which I have a defined model that specifies as part of the set of fields (“with s”) for the model class
I’m also not getting what I would expect to be the intellisense help from VSCode i.e. I have DA.Date imported which VS Code is not complaining about and yet its not picking up “Date” and thus I can’t readily find static side instance creation api etc, not that I see what I need in the docs
e.g. I have tried this , which doesn’t work
- cocreature 2
Hi @amiracam, take a look at a related question on this.
Basically it boils down to using
getTimeand extracting the date from tahtnew Date ()is Java syntax and not valid Daml. You also cannot do this at the top-level, so you have to move it in the do-block. Putting things together, your example could be something like this:repo_test_1 = scenario do jpm <- getParty "JPM" fed <- getParty " FED" nowTime <- getTime let nowDate = toDateUTC nowTime submit jpm do create Repo with owner = jpm regulator = fed par = 1.5 cusip = "9999999" trateDate = nowDateLastly, I recommend to use Daml Script instead of scenarios for all new projects. Your example translates to Daml Script with minimal changes:
repo_test_1 = script do jpm <- allocateParty "JPM" fed <- allocateParty " FED" nowTime <- getTime let nowDate = toDateUTC nowTime submit jpm do createCmd Repo with owner = jpm regulator = fed par = 1.5 cusip = "9999999" trateDate = nowDate - amiracam
k, now that i’m doing all “script”
it compiles but I get a strange bug namely what I expected to me “Date now” shows up as Epoch ?So why ?
and again how can I get the current timestamp so that I can extract the current day ?thanks
- amiracam 1
meant to include this:
Transactions:
TX 0 1970-01-01T00:00:00Z (TestRepo:13:13)
#0:0
│ known to (since): ‘FED’ (0), ‘JPM’ (0)
└─> create FixedInstruments:Repo
with
owner = ‘JPM’;
regulator = ‘FED’;
par = 1.5000000000;
cusip = “9999999”;
tradeDate = 1970-01-01TActive contracts: #0:0
Return value: #0:0
- quidagis 1
Thank you @cocreature this was a great tip

- cocreature 1
Daml Studio runs in static time mode, meaning it starts at unix epoch and time only advances if you call
passorsetTime. So in your example, nothing changes the time so you get the unix epoch.Real ledgers usually run in wallclock mode where
getTimewill provide you with the current time and time will advance automatically. Some ledgers, most notably sandbox also provide a--static-timeoption which causes the ledger to run in the same static time mode that you get in Daml Studio. - amiracam 1
ok, thanks, but then what’s the significance of specifying “wall clock time” in the daml.yaml ?
I’ll assume that only controls the sandbox ledger i.e. the generated dar ? - cocreature 1
Exactly, the
--wallclock-timeoption is only for Sandbox.
