# Not understanding difference between system.date.now() and datetime.datetime.now()

**URL:** <https://forum.inductiveautomation.com/t/not-understanding-difference-between-system-date-now-and-datetime-datetime-now/71803>\
**Category:** General Discussion\
**Tags:** ignition80, vision\
**Created:** [March 29, 2023, 10:06pm UTC](https://forum.inductiveautomation.com/t/not-understanding-difference-between-system-date-now-and-datetime-datetime-now/71803 "2023-03-29T22:06:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Whitt](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@Mark\_Whitt](https://forum.inductiveautomation.com/u/Mark_Whitt)\
**Post date:** [March 29, 2023, 10:06pm UTC](https://forum.inductiveautomation.com/t/not-understanding-difference-between-system-date-now-and-datetime-datetime-now/71803/1 "2023-03-29T22:06:43Z")

</div>

I'm trying to modify an existing script that has the following date manipulations:

```auto
now = system.date.now()
end = system.date.midnight(system.date.addDays(now, -system.date.getDayOfMonth(now)+1)) # 1st of this month
start = system.date.addMonths(end, -1) # 1 month prior to end

```

And I need to add code that will allow me to print out each of the day numbers for the entire month between the 'start' date and the 'end' date, along with doing some database manipulations for each date.

There seems to be plenty of sample code out there that shows how I could start with a date and step through the days like I want to do. But I'm having trouble getting it to work with those first three lines above.

For example the following works fine with 'd' set to datetime.datetime.now() as it is below:

```auto
"""Script to add days to a time returned by the system"""

import datetime
from datetime import date, timedelta
from time import gmtime

now = system.date.now()
end = system.date.midnight(system.date.addDays(now, -system.date.getDayOfMonth(now)+1)) # 1st of this month
start = system.date.addMonths(end, -1) # 1 month prior to end

print now
print start
print end

#get current time
d = datetime.datetime.now()
#d=start

#print date
print(d)

#get the day of month
print(d.strftime("%d"))

modified_date = d + timedelta(days=1)
print(modified_date.strftime("%d"))

```

But when I comment out the 'd = datetime.datetime.now() and uncomment the d=start so that I can use the date range from the existing script, I get the following error:

```auto
Traceback (most recent call last):
  File "<input>", line 23, in <module>
AttributeError: 'java.util.Date' object has no attribute 'strftime'

```

Where line 23 is the 'print(d.strftime("%d"))' line.

I'm thinking this is a simple conversion issue between the java.util.Date format to datetime, but I'm not seeing how to make that conversion.

Suggestions and an 'education' on the topic would be appreciated.

Many thanks in advance!

---

<div class="post-metadata">

**Author:** ![WillMT10](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@WillMT10](https://forum.inductiveautomation.com/u/WillMT10)\
**Post date:** [March 29, 2023, 10:23pm UTC](https://forum.inductiveautomation.com/t/not-understanding-difference-between-system-date-now-and-datetime-datetime-now/71803/2 "2023-03-29T22:23:10Z")

</div>

My recommendation would be to stick with using `system.date.*` unless there is some more advanced functionality that you need from java.util.Date or datetime

```auto
now = system.date.now()
end = system.date.midnight(system.date.addDays(now, -system.date.getDayOfMonth(now)+1)) # 1st of this month
start = system.date.addMonths(end, -1) # 1 month prior to end

print now
print start
print end

#get current time
d = start

#print date
print(d)

#get the day of month
print(system.date.format(d, "d"))

modified_date = system.date.addDays(d, 1)
print(system.date.format(modified_date, "d"))

# Alternative for getting the day of the month, just note that it returns an int rather than a str
print(system.date.getDayOfMonth(d)) 

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 29, 2023, 10:26pm UTC](https://forum.inductiveautomation.com/t/not-understanding-difference-between-system-date-now-and-datetime-datetime-now/71803/3 "2023-03-29T22:26:36Z")

</div>

One is jython/python. The other is java.

Use java objects and classes in Ignition for best results. Yes, converting from python code you found on the internet is harder this way, but jython has a bunch of pitfalls. And it runs slower.

---

<div class="post-metadata">

**Author:** ![Mark\_Whitt](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@Mark\_Whitt](https://forum.inductiveautomation.com/u/Mark_Whitt)\
**Post date:** [March 30, 2023, 12:45pm UTC](https://forum.inductiveautomation.com/t/not-understanding-difference-between-system-date-now-and-datetime-datetime-now/71803/4 "2023-03-30T12:45:34Z")

</div>

I wasn't understanding that one was Java, and the other was jython, and that I wanted to stick with the Java. That's the nudge I needed.

Thanks to both of you!

---

<div class="post-metadata">

**Author:** ![Alex\_Leech](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/alex_leech/32/39391_2.png) [@Alex\_Leech](https://forum.inductiveautomation.com/u/Alex_Leech)\
**Post date:** [March 30, 2023, 4:53pm UTC](https://forum.inductiveautomation.com/t/not-understanding-difference-between-system-date-now-and-datetime-datetime-now/71803/5 "2023-03-30T16:53:51Z")

</div>

I see this mentioned a lot! Is there a cheatsheet/list of jython Vs java equivalents somewhere?

I.e X in jython is better used as Y in java?

---

<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:** [March 30, 2023, 5:14pm UTC](https://forum.inductiveautomation.com/t/not-understanding-difference-between-system-date-now-and-datetime-datetime-now/71803/6 "2023-03-30T17:14:50Z")

</div>

My hot take is to avoid the Jython standard library completely. I'm pretty sure Phil agrees with me.

If it's not available as a system.\* function, there's hopefully a Java dependency available you can pull in. If not, I would literally write a module to bring in a Java lib before I relied on the Jython stdlib for anything non-trivial.

---

<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:** [March 30, 2023, 5:20pm UTC](https://forum.inductiveautomation.com/t/not-understanding-difference-between-system-date-now-and-datetime-datetime-now/71803/7 "2023-03-30T17:20:57Z")

</div>

> [@paul-griffith](#):
>
> My hot take is to avoid the Jython standard library completely. I'm pretty sure Phil agrees with me.

Also agree, unfortunately...

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 30, 2023, 5:51pm UTC](https://forum.inductiveautomation.com/t/not-understanding-difference-between-system-date-now-and-datetime-datetime-now/71803/8 "2023-03-30T17:51:46Z")

</div>

> [@paul-griffith](#):
>
> My hot take is to avoid the Jython standard library completely. I'm pretty sure Phil agrees with me.

I do. I hardly even use `sys` and `traceback` any more, since the error-handling functions I care about are embedded in `later.py`. (via `PythonAsJavaException`)
