What's the correct format to set a default value to a DateTime aura:attribute?
Very interesting question.
I did some research on it and found this gitHub repository, someone has tried to play with default values of all kinds of basic types.
Scene 1:
Simply putting the below line doesn't work.
<aura:attribute type="Datetime" name="datetimeDefaultWithTimeString" default="2013-03-06T10:17:36.789Z"/>
however if you do the exact same line with type="Date"
, it takes the whole thing and displays as Datetime.
<aura:attribute type="Date" name="dateDefaultWithTimeString" default="2013-03-06T10:17:36.789Z"/>
Scene 2:
In the other hand, datetime attributes take string as default value and the string needs to be a digit.
<aura:attribute type="DateTime" name="dateTimeDefaultZero" default="0"/>
This would default the attribute to 1970-01-01T00:00:00.000Z
, start of calendar I guess.
Again, defaulting it to 1 will increase the mili second part by 1, i.e 1970-01-01T00:00:00.001Z
<aura:attribute type="DateTime" name="dateTimeDefaultWithString" default="1"/>
Similarly, defaulting it to 1000 would increase the time by 1 second. So in short, it takes millisecond value and adds to 1970-01-01T00:00:00.000Z
.
Scene 3:
Now again, the following is allowing me to provide a full length datetime string as default value and its displaying the same too.
<aura:attribute type="DateTime" name="datetimeDefaultWithTimeLiteralExp" default="{!'2016-03-06T10:17:36.789Z'}"/>
I could have just put the 3rd scene directly as an answer but I would like to see what other experts think about this case.