Date Printing as 00 when a new month starts

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.


pic2

Pleas find the attached pictures for reference,

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.

1 Like

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

Otherwise we can't read it.

Unrelated, but you should use:

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

3 Likes

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
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)
print(currentHour)
print(vDOPFull)
print(vDOP)

returns

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

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.

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!

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

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.

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.

#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]
1 Like

Hi,

Thank you. Hope it helps.