Script to extract weekday number from DateTime Input props.value?

I’ve spent too many hours at this!

Q1. Am I correct in thinking that the DateTime Input component props.value return a formatted date rather than the millisecond timestamp?

Q2. How do I script calculation of the weekday (0 = Monday) from the DateTime Input props.value? The format in use is YYYY-MMM-DD (at the moment but I’d hate the application to break if I changed this later).

Q3. I’m generating a millisecond timestamp from dates (for history binding transform comparison) using
millis = time.mktime(datetime.datetime.strptime(startDateTime, "%Y-%m-%d %H:%M").timetuple()) * 1000 # ms
Is there a better way?

Many thanks.

Can you not just use system.date.getDayOfWeek(myDateTimeInput.props.value)?

Edit: my suggestion would return Mondays = 2, so you would have to add your own offset if you wanted your result to be zero-based on on Monday. The system function is 1-based with the week starting on Sunday.

It is a class of java.util.Date, so none of your affirmations are correct. There is prop called formattedValue, that one is a string representation of a date with format, avoid using it.

For monday = 0 use this expression

if(getDayOfWeek({this.props.value})-2>=0,
getDayOfWeek({this.props.value})-2,
6)

I'm not sure if I understood, but I think you want to get a date from the millis returned by the history binding transform. Well that is not true, the historical bindings returns java.util.Date or even java.sql.Timestamp. What is happening is the perspective components decodes dates in Unix Based milliseconds.
That been said, if your object is a class of Date or Timestamp, use yourdate.getDate() method get unix based milliseconds. So, just use .getDate()

Are you sure about that?

I would use:

(getDayOfWeek({this.props.value})+6) % 7

Yes hehe. If Sunday is 1 and Monday is 2, so 2-2=0

In that case your expression should be:

(getDayOfWeek({this.props.value})+5) % 7
1 Like