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

**URL:** <https://forum.inductiveautomation.com/t/script-to-extract-weekday-number-from-datetime-input-props-value/56087>\
**Category:** Ignition\
**Tags:** python, perspective\
**Created:** [February 14, 2022, 12:08am UTC](https://forum.inductiveautomation.com/t/script-to-extract-weekday-number-from-datetime-input-props-value/56087 "2022-02-14T00:08:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [February 14, 2022, 12:08am UTC](https://forum.inductiveautomation.com/t/script-to-extract-weekday-number-from-datetime-input-props-value/56087/1 "2022-02-14T00:08:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dustin.cooper](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dustin.cooper/32/29963_2.png) [@dustin.cooper](https://forum.inductiveautomation.com/u/dustin.cooper)\
**Post date:** [February 14, 2022, 12:11am UTC](https://forum.inductiveautomation.com/t/script-to-extract-weekday-number-from-datetime-input-props-value/56087/2 "2022-02-14T00:11:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jespinmartin1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jespinmartin1/32/57175_2.png) [@jespinmartin1](https://forum.inductiveautomation.com/u/jespinmartin1)\
**Post date:** [February 14, 2022, 4:39am UTC](https://forum.inductiveautomation.com/t/script-to-extract-weekday-number-from-datetime-input-props-value/56087/3 "2022-02-14T04:39:23Z")

</div>

> [@Transistor](#):
>
> Q1. Am I correct in thinking that the DateTime Input component props.value return a formatted date rather than the millisecond timestamp?

It is a class of [java.util.Date](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Date.html), 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.

> [@Transistor](#):
>
> 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).

For monday = 0 use this expression

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

```

> [@Transistor](#):
>
> 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?

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()

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 14, 2022, 12:22pm UTC](https://forum.inductiveautomation.com/t/script-to-extract-weekday-number-from-datetime-input-props-value/56087/4 "2022-02-14T12:22:46Z")

</div>

> [@jespinmartin1](#):
>
> For monday = 0 use this expression

Are you sure about that?

I would use:

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

```

---

<div class="post-metadata">

**Author:** ![jespinmartin1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jespinmartin1/32/57175_2.png) [@jespinmartin1](https://forum.inductiveautomation.com/u/jespinmartin1)\
**Post date:** [February 14, 2022, 3:14pm UTC](https://forum.inductiveautomation.com/t/script-to-extract-weekday-number-from-datetime-input-props-value/56087/5 "2022-02-14T15:14:50Z")

</div>

> [@pturmel](#):
>
> Are you sure about that?

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

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/e/ae6cc7cd76540fae86053cfa3420ee119f0f6750.png)

In that case your expression should be:

```auto
(getDayOfWeek({this.props.value})+5) % 7

```
