7

I would like to set a default value to a DateTime aura:attribute, but could not find the correct format. I tried the following but it didn't work:

<aura:attribute name="myDateTime" type="DateTime" default="1981-08-26T00:00:00.000Z"/>

When I try to save, I receive the following error: Failed to save undefined: For input string: "1970-01-01T00:00:00.000Z".

It seems I can use a number that will end up being added to the Unix Epoch time but well, that's not really convenient.

2 Answers 2

11

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.

1
  • 2
    Thanks for the detailed answer ! I guess we would all expect to have "2016-03-06T10:17:36.789Z" to work directly, but at least it working with this workaround. Apr 7, 2017 at 10:10
0

Many Peoples searching for this, but this is the right place to comment.

Working Default format in Lightning design system: 2018-01-06T00:00

Javascript:

moment(new Date(), "DD-MM-YYYY").add(1,'days').format("YYYY-MM-DD")+'TT00:00')

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .