# How to replace the time in a datetime object in python

**URL:** <https://forum.inductiveautomation.com/t/how-to-replace-the-time-in-a-datetime-object-in-python/89039>\
**Category:** Ignition\
**Created:** [April 18, 2024, 12:57pm UTC](https://forum.inductiveautomation.com/t/how-to-replace-the-time-in-a-datetime-object-in-python/89039 "2024-04-18T12:57:59Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![bwhite](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bwhite/32/7112_2.png) [@bwhite](https://forum.inductiveautomation.com/u/bwhite)\
**Post date:** [April 18, 2024, 12:57pm UTC](https://forum.inductiveautomation.com/t/how-to-replace-the-time-in-a-datetime-object-in-python/89039/1 "2024-04-18T12:57:59Z")

</div>

I'm trying to set the time in a datetime object in a python script. I've tried using the datetime.replace and also the datetime.combine and neither one will work.

checkDate = datetime.date(2024,3,1)  
startTime = checkDate.replace(hour=6)  
endTime = checkDate.replace(hour=18)

in the script console I get the error:

TypeError: replace() got an unexpected keyword argument 'hour'

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [April 18, 2024, 1:05pm UTC](https://forum.inductiveautomation.com/t/how-to-replace-the-time-in-a-datetime-object-in-python/89039/2 "2024-04-18T13:05:13Z")

</div>

Don't use `datetime`, use java's built-in objects `java.util.Date`, it's what ignition's `system.date.*` functions use.  
So then, you can use [system.date.setTime | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-date/system-date-setTime)

```python
checkDate = system.date.parse('2024/03/01', 'yyyy/MM/dd')
startTime = system.date.setTime(checkDate, 6, 0, 0)
endTime = system.date.setTime(checkDate, 18, 0, 0)

```

---

<div class="post-metadata">

**Author:** ![bwhite](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bwhite/32/7112_2.png) [@bwhite](https://forum.inductiveautomation.com/u/bwhite)\
**Post date:** [April 18, 2024, 1:18pm UTC](https://forum.inductiveautomation.com/t/how-to-replace-the-time-in-a-datetime-object-in-python/89039/3 "2024-04-18T13:18:47Z")

</div>

I'm trying to do a manipulation with the dates and I found a way to do that with the python datetime.

I have a start date and end date that may be a month or several months. I need to loop through each day from 6am to 6pm and run another script on each day.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [April 18, 2024, 1:25pm UTC](https://forum.inductiveautomation.com/t/how-to-replace-the-time-in-a-datetime-object-in-python/89039/4 "2024-04-18T13:25:08Z")

</div>

Again: Don't do this with python's datetime, use Java's dates instead.

If you can describe what you're trying to do, we can help you.  
But as it is, it's confusing to say the least.  
Do you need to run the script once per day ? Then what are the times for ?  
Do you have a start and end DATES (not TIMES) ?  
Do you have a number of days instead ?

In the end, what is the whole thing supposed to do ?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 18, 2024, 1:25pm UTC](https://forum.inductiveautomation.com/t/how-to-replace-the-time-in-a-datetime-object-in-python/89039/5 "2024-04-18T13:25:14Z")

</div>

While the advice to use Ignition's date/time functions stands, your issue here is that:

```auto
checkDate = datetime.date(2024,3,1)

```

Gives you a `datetime.date`, not a `datetime.datetime`, and a `date` has no hour on it.

---

<div class="post-metadata">

**Author:** ![bwhite](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bwhite/32/7112_2.png) [@bwhite](https://forum.inductiveautomation.com/u/bwhite)\
**Post date:** [April 18, 2024, 1:33pm UTC](https://forum.inductiveautomation.com/t/how-to-replace-the-time-in-a-datetime-object-in-python/89039/6 "2024-04-18T13:33:53Z")

</div>

The start date and end date are picked from a calendar component in vision for a report viewer. The example I gave was just my trying things out in the script console.

If the start date is 1/1/2024 and the end date is 3/31/2024, I need to go through these dates in a script for each day isolating each shift (6am-6pm and 6pm-6am) to get data from several different data sources.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [April 18, 2024, 2:07pm UTC](https://forum.inductiveautomation.com/t/how-to-replace-the-time-in-a-datetime-object-in-python/89039/7 "2024-04-18T14:07:31Z")

</div>

Try this

```python
date = system.date.parse('2024/01/01', 'yyyy/MM/dd')
end_date = system.date.parse('2024/01/11', 'yyyy/MM/dd')

def do_your_thing(date):
	print "doing stuff for {}".format(date, 'yyyy/MM/dd HH:mm:ss')

while system.date.isBefore(date, end_date):
	do_your_thing(date)
	date = system.date.addDays(date, 1)

```

I'm not sure what you mean by "isolating each shift", but you can easily use `setTime` on the `date` object to get whatever time of day you want

edit:  
damn it's been a _while_ since i've used a while loop

---

<div class="post-metadata">

**Author:** ![bwhite](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bwhite/32/7112_2.png) [@bwhite](https://forum.inductiveautomation.com/u/bwhite)\
**Post date:** [April 18, 2024, 3:13pm UTC](https://forum.inductiveautomation.com/t/how-to-replace-the-time-in-a-datetime-object-in-python/89039/8 "2024-04-18T15:13:05Z")

</div>

That worked. I hesitate to use a while loop, but I put in a counter to break if it gets too large.

---

<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:** [April 18, 2024, 3:24pm UTC](https://forum.inductiveautomation.com/t/how-to-replace-the-time-in-a-datetime-object-in-python/89039/9 "2024-04-18T15:24:50Z")

</div>

You can make it iteratable:

```python
startDate = system.date.parse('2024-01-01 00:00:00')
endDate = system.date.parse('2024-01-10 00:00:00')

dates = iter([system.date.addDays(startDate, n) for n in xrange(system.date.daysBetween(startDate, endDate)+1)])

for date in dates:
	do_your_thing(date)

```

It will work without the iter() function, but if there are a lot of dates, then making an iterator instead of an iterable list will help save memory.

---

<div class="post-metadata">

**Author:** ![zacht](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@zacht](https://forum.inductiveautomation.com/u/zacht)\
**Post date:** [April 18, 2024, 3:31pm UTC](https://forum.inductiveautomation.com/t/how-to-replace-the-time-in-a-datetime-object-in-python/89039/10 "2024-04-18T15:31:31Z")

</div>

Do you still get the memory benefits if you use a list comprehension? I would have expected the `dates = ...` expression without the square brackets.

```auto
dates = iter(system.date.addDays(startDate, n) for n in xrange(system.date.daysBetween(startDate, endDate)+1))

```

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [April 18, 2024, 4:04pm UTC](https://forum.inductiveautomation.com/t/how-to-replace-the-time-in-a-datetime-object-in-python/89039/11 "2024-04-18T16:04:10Z")

</div>

You don't even need `iter()`, the `(x for x in something)` syntax is a generator expression and can be used as is for iteration.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [April 18, 2024, 6:14pm UTC](https://forum.inductiveautomation.com/t/how-to-replace-the-time-in-a-datetime-object-in-python/89039/12 "2024-04-18T18:14:55Z")

</div>

Generator, iterator, list comprehension - it's all still a while loop under the hood. Be careful with the dates you pass into this function.
