# Date Printing as 00 when a new month starts

**URL:** <https://forum.inductiveautomation.com/t/date-printing-as-00-when-a-new-month-starts/62615>\
**Category:** Ignition\
**Created:** [August 3, 2022, 6:21am UTC](https://forum.inductiveautomation.com/t/date-printing-as-00-when-a-new-month-starts/62615 "2022-08-03T06:21:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dharani\_T](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dharani\_T](https://forum.inductiveautomation.com/u/Dharani_T)\
**Post date:** [August 3, 2022, 6:21am UTC](https://forum.inductiveautomation.com/t/date-printing-as-00-when-a-new-month-starts/62615/1 "2022-08-03T06:21:56Z")

</div>

Hi all,

For a new month, we are facing an issue when we are trying to segregate the day as shifts.  
The date of production is printing as 00.

 ![pic1](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/1/b1ee6939cbdbdc5b1260a89573ec8fccb179f671.png)  
 ![pic2](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/b/4b388f36dc0a216fa85b62b9da881b5160e40cdc.png)

Pleas find the attached pictures for reference,

---

<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:** [August 3, 2022, 7:12am UTC](https://forum.inductiveautomation.com/t/date-printing-as-00-when-a-new-month-starts/62615/2 "2022-08-03T07:12:17Z")

</div>

Please post code rather than pictures of code. That way we can copy and edit in our answers without having to type it all out again. Use the \</\> button to format it as code. Thanks.

---

<div class="post-metadata">

**Author:** ![Dharani\_T](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dharani\_T](https://forum.inductiveautomation.com/u/Dharani_T)\
**Post date:** [August 3, 2022, 7:23am UTC](https://forum.inductiveautomation.com/t/date-printing-as-00-when-a-new-month-starts/62615/3 "2022-08-03T07:23:47Z")

</div>

currentDateAndTime = system.date.now()

system.tag.writeBlocking(“[default]BASE/Global/CurrentTime”, [currentDateAndTime])

currentHour = system.date.getHour24(currentDateAndTime)

if ( currentHour \>= 7 and currentHour \< 15 ):  
vDOP = system.date.format(currentDateAndTime, “yyyy-MM-dd”)  
vShiftID = “A”  
vShiftStartHour = 7  
if ( currentHour \>= 15 and currentHour \< 23 ):  
vDOP = system.date.format(currentDateAndTime, “yyyy-MM-dd”)  
vShiftID = “B”  
vShiftStartHour = 15  
elif ( currentHour == 23 ):  
vDOP = system.date.format(currentDateAndTime, “yyyy-MM-dd”)  
vShiftID = “C”  
vShiftStartHour = 23  
elif ( currentHour \>= 0 and currentHour \< 7 ):  
vDOP = system.date.format(system.date.addDays(currentDateAndTime, -1), “yyyy-MM-dd”)  
vShiftID = “C”  
vShiftStartHour = 23

vDOPFull = system.date.parse(system.date.format(currentDateAndTime, “yyyy-MM-dd 00:00:00”), “yyyy-MM-dd hh:mm:ss”)

#vDOPFull = time.strptime(vDOP, “%Y-%m-%d”)

vShiftStart = system.date.addHours(vDOPFull, vShiftStartHour)  
vShiftEnd = system.date.addSeconds(vShiftStart, 28799)  
vPreviousDOP= system.date.addDays(vDOPFull, -1)

system.tag.writeBlocking(“[default]BASE/Global/DOP”, [vDOP])  
system.tag.writeBlocking(“[default]BASE/Global/ShiftID”, [vShiftID])  
system.tag.writeBlocking(“[default]BASE/Global/ShiftStart”, [vShiftStart])  
system.tag.writeBlocking(“[default]BASE/Global/ShiftEnd”, [vShiftEnd])  
system.tag.writeBlocking(“[default]BASE/Global/PreviousDOP”, [vPreviousDOP])

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [August 3, 2022, 7:33am UTC](https://forum.inductiveautomation.com/t/date-printing-as-00-when-a-new-month-starts/62615/4 "2022-08-03T07:33:35Z")

</div>

> [@Transistor](#):
>
> Use the \</\> button to format it as code

Otherwise we can't read it.

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

Unrelated, but you should use:

```auto
vDOPFull = system.date.setTime(currentDateAndTime, 0, 0, 0)

```

Also, why are you converting a lot of your dates to strings? You should instead leave them as dates and handle the date formatting where you're viewing them

---

<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:** [August 3, 2022, 7:47am UTC](https://forum.inductiveautomation.com/t/date-printing-as-00-when-a-new-month-starts/62615/5 "2022-08-03T07:47:38Z")

</div>

You can use the 🖉 edit link to fix your post rather than post again.

I tried your original code in the Script Console and it returns `2022-08-03` correctly. I’m using Ignition 8.1.18.

> **Test code**
>
> ```auto
> currentDateAndTime = system.date.now()
> 
> # system.tag.writeBlocking("[default]BASE/Globa 1/CumentTime", [currentDateAndTime]) 
> currentHour = system.date.getHour24(currentDateAndTime)
> 
> if ( currentHour >= 7 and currentHour < 15 ):
> vDOP = system.date.format(currentDateAndTime, "yyyy-MM-dd") 
> vShiftID = "A" 
> vShiftStartHour = 7
> if ( currentHour >= 15 and currentHour < 23 ):
> vDOP = system.date.format(currentDateAndTime, "yyyy-MM-dd") 
> vShiftID = "B" 
> vShiftStartHour = 15
> elif ( currentHour == 23 ):
> vDOP = system.date.format(currentDateAndTime, "yyyy-MM-dd") 
> vShiftID = "C" 
> vShiftStartHour = 23
> elif ( currentHour >= 0 and currentHour < 7 ) :
> vDOP = system.date.format(system.date.addDays(currentDateAndTime, -1), "yyyy-MM-dd") 
> vShiftID = "C" 
> vShiftStartHour = 23
> 
> vDOPFull = system.date.parse(system.date.format(currentDateAndTime, "yyyy-MM-dd 00:00:00"), "yyyy-MM-dd hh:mm:ss")
> 
> print(currentHour)
> print(vDOPFull)
> print(vDOP)
> 
> ```

```auto
print(currentHour)
print(vDOPFull)
print(vDOP)

```

returns

```auto
8
Wed Aug 03 00:00:00 BST 2022
u'2022-08-03'

```

---

<div class="post-metadata">

**Author:** ![Dharani\_T](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dharani\_T](https://forum.inductiveautomation.com/u/Dharani_T)\
**Post date:** [August 3, 2022, 10:55am UTC](https://forum.inductiveautomation.com/t/date-printing-as-00-when-a-new-month-starts/62615/6 "2022-08-03T10:55:36Z")

</div>

Hi,

We are facing this error when a new month begins.  
For C shift (i.e, from 12:01 AM of 01-08-2022 to 06:59 AM) Date is returning as 2022-08-00.

---

<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:** [August 3, 2022, 11:32am UTC](https://forum.inductiveautomation.com/t/date-printing-as-00-when-a-new-month-starts/62615/7 "2022-08-03T11:32:41Z")

</div>

Can you reproduce it in the Script Console with a fixed date/time so that we can reproduce it? If so, then please post the \</\> formatted code.  
Which of your `if` branches is causing the problem?  
What version are you using?  
Can you format the code in your second post as requested? Make it easy for people to help you!

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [August 3, 2022, 3:05pm UTC](https://forum.inductiveautomation.com/t/date-printing-as-00-when-a-new-month-starts/62615/8 "2022-08-03T15:05:33Z")

</div>

Try this code and let us know if you still have the same issue?

```python
currentDateAndTime = system.date.now()
currentHour = system.date.getHour24(currentDateAndTime)

vShiftID = ""
vshiftStartHour = 0
vDOP = system.date.format(currentDateAndTime, 'yyyy-MM-dd')

if currentHour >= 7 and currentHour < 15:
    vShiftID = 'A'
    vShiftStartHour = 7
elif currentHour >= 15 and currentHour < 23:
    vShiftID = 'B'
    vShiftStartHour = 15
elif currentHour == 23:
    vShiftID = 'C'
    vShiftStartHour = 23
elif currentHour < 7:
    vDOP = system.date.format(system.date.addDays(currentDateAndTime, -1), 'yyyy-MM-dd')
    vShiftID = 'C'
    vShiftStartHour = 23

vDOPFull = system.date.setTime(currentDateAndTime,0,0,0)
vShiftStart = system.date.addHours(vDOPFull,vShiftStartHour)
vShiftEnd = system.date.addHours(vShiftStart,8)
vPreviousDOP = system.date.addDays(vDOPFull, -1)

parentPath = '[default]BASE/Global/'
system.tag.writeBlocking([parentPath + tag for tag in ('CurrentTime','DOP','ShiftID','ShiftStart','ShiftEnd','PreviousDOP')],[currentDateAndTime,vDOP,vShiftID,vShiftStart,vShiftEnd,vPreviousDOP])

```

Be careful with scope. You have variables that are defined in one scope and then used in another. That may not be the cause of this specific issue, but eventually it will bite you.

Also, always combine as many tag writes into a single call as you can. You can see here that I have combined all of your tag writes into just one write at the end of the script. It is best to read all tags that you will need the values for at the beginning of a script, and write them all at the end doing any processing in between. Each call to system.tag.readBlocking or system.tag.writeBlocking is a round trip to the gateway at best. It will cause performance issues.

All of that being said, I agree with @nminchin you should work with dates as dates and then do any formatting where you need to view the date.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [August 3, 2022, 3:52pm UTC](https://forum.inductiveautomation.com/t/date-printing-as-00-when-a-new-month-starts/62615/9 "2022-08-03T15:52:26Z")

</div>

> [@nminchin](#):
>
> Unrelated, but you should use:
> 
> ```auto
> vDOPFull = system.date.setTime(currentDateAndTime, 0, 0, 0)
> 
> ```

system.date.midnight() would be more readable.

Also, if you are going to format anything to a string, try to do it after any calculations occur.

Note: I deliberatly parsed the current Date to the beginning of the month.

```python
#Set the current datetime
currentDateAndTime = system.date.parse('2022-08-01 02:00:00')

#Extract the hour
currentHour = system.date.getHour24(currentDateAndTime)

# Initialize variables
vShiftID = ""
vshiftStartHour = 0
vDOP = currentDateAndTime

# Check for shift
if 7 <= currentHour < 15:
    vShiftID = 'A'
    vShiftStartHour = 7
    
elif 15 <= currentHour < 23:
    vShiftID = 'B'
    vShiftStartHour = 15
    
else:
    vShiftID = 'C'
    vShiftStartHour = 23

# If the hour is less than 7, subtract 1 day from vDOP
if currentHour < 7:
    vDOP = system.date.addDays(vDOP, -1)

# Convert to string
vDOP = system.date.format(vDOP, 'yyyy-MM-dd')

#Set everything else
vDOPFull = system.date.midnight(currentDateAndTime)
vShiftStart = system.date.addHours(vDOPFull,vShiftStartHour)
vShiftEnd = system.date.addHours(vShiftStart,8)
vPreviousDOP = system.date.addDays(vDOPFull, -1)

# Do something with this data
print[currentDateAndTime, vDOP, vShiftID, vShiftStart, vShiftEnd, vPreviousDOP]

```

---

<div class="post-metadata">

**Author:** ![Dharani\_T](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dharani\_T](https://forum.inductiveautomation.com/u/Dharani_T)\
**Post date:** [August 4, 2022, 4:36am UTC](https://forum.inductiveautomation.com/t/date-printing-as-00-when-a-new-month-starts/62615/10 "2022-08-04T04:36:10Z")

</div>

Hi,

Thank you. Hope it helps.
