# Date intervals spanning across days

**URL:** <https://forum.inductiveautomation.com/t/date-intervals-spanning-across-days/83263>\
**Category:** Ignition\
**Created:** [December 18, 2023, 3:59pm UTC](https://forum.inductiveautomation.com/t/date-intervals-spanning-across-days/83263 "2023-12-18T15:59:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![HelloWorld](https://avatars.discourse-cdn.com/v4/letter/h/b782af/32.png) [@HelloWorld](https://forum.inductiveautomation.com/u/HelloWorld)\
**Post date:** [December 18, 2023, 3:59pm UTC](https://forum.inductiveautomation.com/t/date-intervals-spanning-across-days/83263/1 "2023-12-18T15:59:44Z")

</div>

Hello I have this code -

```auto
# Function to create SampleEvent entries
def create_sample_events(data):
	
	# current_Ttime
	current_time = datetime.datetime.now()
	
	# Get IDs for ProductionEvent, ProductSample, Product, Sample, SampleSchedule for each row 
	for row in data:
	    pe_id = row['ProductionEventID']
	    ps_id = row['ProductSampleID']
	    p_id = row['ProductID']
	    s_id = row['SampleID']
	    ss_id = row['SampleScheduleID']
	    
	    # Get the time for last sample taken for that ProductionEvent
	    last_sample_time_dataset = system.db.runNamedQuery("Scheduling/GetLastSampleEventTime",parameters={"productionEventID": pe_id})
	    last_sample_time = last_sample_time_dataset.getValueAt(0, 0)
	    	
	    # Get Interval and Offset for the SampleSchedule 
	    intervalOffset = system.db.runNamedQuery("Scheduling/GetIntervalOffsetBySampleScheduleID",parameters = {"sampleScheduleID": ss_id})

	    # Get Interval value
	    sample_interval = intervalOffset.getValueAt(0,"Interval")
		
		# If there is no last_sample_time use offset 
	    if last_sample_time == None:
	    		print("No last")
	    # If there is a last_sample_time 
	    else:
	    	# Calculate the next_sample_time by adding the interval to the last_sample_time
	    	next_sample_time = system.date.addHours(last_sample_time, sample_interval)

```

I am running into an issue where if the last\_sample\_time takes place a day(s) before. The next sample\_time will be set to day(s) earlier than it should be. I want the next\_sample\_time to be the last\_sample\_time but for the current date (or potentially the day after).

---

<div class="post-metadata">

**Author:** ![HelloWorld](https://avatars.discourse-cdn.com/v4/letter/h/b782af/32.png) [@HelloWorld](https://forum.inductiveautomation.com/u/HelloWorld)\
**Post date:** [December 18, 2023, 4:17pm UTC](https://forum.inductiveautomation.com/t/date-intervals-spanning-across-days/83263/2 "2023-12-18T16:17:07Z")

</div>

Update: could potentially just get the minutes between last\_sample\_time and current time and add that to the last\_sample\_time + interval ??

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [December 19, 2023, 6:34am UTC](https://forum.inductiveautomation.com/t/date-intervals-spanning-across-days/83263/3 "2023-12-19T06:34:15Z")

</div>

It's there still a question here, or was this question answered sufficiently by [this post?](https://forum.inductiveautomation.com/t/convert-older-date-to-current-date-with-old-time/83271/8)

---

<div class="post-metadata">

**Author:** ![HelloWorld](https://avatars.discourse-cdn.com/v4/letter/h/b782af/32.png) [@HelloWorld](https://forum.inductiveautomation.com/u/HelloWorld)\
**Post date:** [December 19, 2023, 1:54pm UTC](https://forum.inductiveautomation.com/t/date-intervals-spanning-across-days/83263/5 "2023-12-19T13:54:39Z")

</div>

It was answered sufficiently in that post.
