Datetime format problem

Hello all,

I need add extra minutes on datetime.now(). I’m trying a script in the below but it doesn’t work because of time sctructure of datetime.

import datetime
time_format = '%Y-%m-%d %H:%M:%S'

pitchStartTime = datetime.datetime.utcnow()
print(pitchStartTime)
pitchEndTime = (pitchStartTime + datetime.timedelta(minutes=5))
print(pitchEndTime)

Output

2022-02-28 08:31:29.221000
2022-02-28 08:36:29.221000

I have to use without “miliseconds” part. How can I remove these part? I mean, it should be like 2022-02-28 08:31:29

Thanks

import datetime

pitchStartTime = datetime.datetime.utcnow()
p_time =pitchStartTime.strftime("%Y-%m-%d %H:%M:%S")
print(p_time)
pitchEndTime = (pitchStartTime + datetime.timedelta(minutes=5))
p_end = pitchEndTime.strftime("%Y-%m-%d %H:%M:%S")
print(p_end)

results

2022-02-28 08:50:38
2022-02-28 08:55:38

I couldn’t catch it because I did the conversion in the wrong place, thanks :slight_smile:

I would recommend not using the python/jython Datetime class. I would instead use Ignition’s builtin system.date.* functions. In this case,

pitchStartTime = system.date.now()
pitchEndTime = system.date.addMinutes(pitchStartTime,5)
print system.date.format(pitchEndTime,'yyyy-MM-dd hh:mm:ss')

returns 2022-02-28 08:59:18

If you go this way, the format string will use SimpleDateFormat structure as described here: SimpleDateFormat (Java Platform SE 7 )

4 Likes