# Convert older Date to current Date with old time

**URL:** <https://forum.inductiveautomation.com/t/convert-older-date-to-current-date-with-old-time/83271>\
**Category:** Ignition\
**Created:** [December 18, 2023, 4:58pm UTC](https://forum.inductiveautomation.com/t/convert-older-date-to-current-date-with-old-time/83271 "2023-12-18T16:58:23Z")\
**Posts on this page:** 12\
**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, 4:58pm UTC](https://forum.inductiveautomation.com/t/convert-older-date-to-current-date-with-old-time/83271/1 "2023-12-18T16:58:23Z")

</div>

Is there anyway to convert say Dec 12 08:26:03 EST 2023 to the current Date with the old time (Dec 18 08:26:03 EST 2023)??

---

<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:** [December 18, 2023, 5:00pm UTC](https://forum.inductiveautomation.com/t/convert-older-date-to-current-date-with-old-time/83271/2 "2023-12-18T17:00:30Z")

</div>

Yes there is.  
Where are you trying to do this? In an expression or in a script?

---

<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, 5:06pm UTC](https://forum.inductiveautomation.com/t/convert-older-date-to-current-date-with-old-time/83271/3 "2023-12-18T17:06:30Z")

</div>

A script

---

<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:** [December 18, 2023, 5:14pm UTC](https://forum.inductiveautomation.com/t/convert-older-date-to-current-date-with-old-time/83271/4 "2023-12-18T17:14:21Z")

</div>

Something like this should work:

```auto
daysDiff = system.date.daysBetween(date1, date2)
newDate = system.date.addDays(date1, daysDiff)

```

---

<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, 5:18pm UTC](https://forum.inductiveautomation.com/t/convert-older-date-to-current-date-with-old-time/83271/5 "2023-12-18T17:18:46Z")

</div>

What if I do not know that date2, for example if I have date1 = DEC 12 8:26:03 - I want to dynamically turn that into DEC 18 8:26:03 - I do not have DEC 18 8:26:03

---

<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, 5:20pm UTC](https://forum.inductiveautomation.com/t/convert-older-date-to-current-date-with-old-time/83271/6 "2023-12-18T17:20:32Z")

</div>

```auto
inBetween = system.date.minutesBetween(last_sample_time, system.date.now())
next_sample_time = system.date.addMinutes(last_sample_time, inBetween)

```

this would end up comparing to the current date and give me a result that is not accurate

---

<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:** [December 18, 2023, 5:24pm UTC](https://forum.inductiveautomation.com/t/convert-older-date-to-current-date-with-old-time/83271/7 "2023-12-18T17:24:14Z")

</div>

Why are you calculating in minutes? Calculate in days.  
You can use the `system.date.midnight()` function if you want to bring both dates to midnight before calculating `daysBetween`.

---

<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:** [December 18, 2023, 5:26pm UTC](https://forum.inductiveautomation.com/t/convert-older-date-to-current-date-with-old-time/83271/8 "2023-12-18T17:26:40Z")

</div>

```python
system.date.setTime(
    system.date.now(),
    system.date.getHour24(last_sample_time),
    system.date.getMinute(last_sample_time),
    system.date.getSecond(last_sample_time)
)

```

[https://docs.inductiveautomation.com/display/DOC81/system.date.setTime](https://docs.inductiveautomation.com/display/DOC81/system.date.setTime)

[https://docs.inductiveautomation.com/pages/viewpage.action?pageId=58603995](https://docs.inductiveautomation.com/pages/viewpage.action?pageId=58603995)

---

<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, 5:29pm UTC](https://forum.inductiveautomation.com/t/convert-older-date-to-current-date-with-old-time/83271/9 "2023-12-18T17:29:08Z")

</div>

Ah!

```auto
inBetween = system.date.daysBetween(system.date.midnight(last_sample_time), system.date.midnight(system.date.now()))
next_sample_time = system.date.addDays(last_sample_time, inBetween)

```

This should achieve what I want. Does this work for the edge case of it being AM or PM ?

---

<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, 5:31pm UTC](https://forum.inductiveautomation.com/t/convert-older-date-to-current-date-with-old-time/83271/10 "2023-12-18T17:31:27Z")

</div>

Would this be a better solution for the edge cases of daylight savings, different month lengths, time zone changes ???

---

<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:** [December 18, 2023, 5:34pm UTC](https://forum.inductiveautomation.com/t/convert-older-date-to-current-date-with-old-time/83271/11 "2023-12-18T17:34:37Z")

</div>

I would expect there to be less "surprises" with the `setTime` approach - it's going to give you the time you "expect" with the given algorithm. Subtracting days _may_ have DST/timezone complications. In general, though, date math is complicated, and there is no single perfect solution. You're always leaving _something_ off the table.

---

<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, 5:35pm UTC](https://forum.inductiveautomation.com/t/convert-older-date-to-current-date-with-old-time/83271/12 "2023-12-18T17:35:28Z")

</div>

Thanks! @paul-griffith and @Transistor
