Perspective DateTime Picker "default"

I’d like to configure a DateTime Picker to start with the current datetime on startup. This would make sense as an onStartup System Event, so I figure, create an onStartup system event script and run self.props.value=now() or something like that. But now() doesn’t work. How do I do this?

from datetime import datetime


self.props.value = datetime.now()
2 Likes

Ah, thanks.

As a general rule I would avoid datetime. Everything using datetime returns a python object. It would be better, I think, to use system.date.now(). This will return a Java Date object which is the actual type that the Date Time Picker uses (no implicit conversion necessary).

For this specific case, you could use an expression binding on the value. Your expression would be now(0) which causes the component to take the current date when it first loads, but the binding doesn’t execute again.

5 Likes

Ah, I was thinking the expression would always update. So along this line of thought: dateArithmetic(now(0),-7,“day”) is a way to pick a day a week ago, and still lets the user pick another date after page load. Seems to work.

Thanks!

Yep. That’s what I do when I want a default that a user can change if they want. You can also use the add* expression functions. As far as I know they aren’t better than dateArithmetic, but I find them easier to remember than proper value for the field argument.

1 Like

They are better. They use java's date/time infrastructure directly. I can't recall the details, but jython's implementation is flawed.

1 Like

TIL. Thanks for the info.

Hi Andrew,

I am facing similar issue, and I tried the dateArithmetic function, and the picker picks the selected date, however it resets back to today’s date if there is a 0 in the dateArithmetic function in its second value. Not sure if needs some scripting

Post the binding expression (formatted in three backticks). That’ll be the easiest way to get help.

```
# Put code here.
```

Edited to better format the example.

Thanks. But I worked it out.