# Change string data to time

**URL:** <https://forum.inductiveautomation.com/t/change-string-data-to-time/45510>\
**Category:** Ignition\
**Tags:** ignition81, scripting, database, ignition79, ignition80\
**Created:** [April 12, 2021, 10:23pm UTC](https://forum.inductiveautomation.com/t/change-string-data-to-time/45510 "2021-04-12T22:23:56Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![antonio.sanchez](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@antonio.sanchez](https://forum.inductiveautomation.com/u/antonio.sanchez)\
**Post date:** [April 12, 2021, 10:23pm UTC](https://forum.inductiveautomation.com/t/change-string-data-to-time/45510/1 "2021-04-12T22:23:57Z")

</div>

hi everyone i would like to know if there is any way to change a string or int variable to time, I currently have something like this

```auto
Contador = system.tag.read(dateFormat(addSeconds(midnight(now()),"[.]MemTimeDiff","HH:mm:ss"))

```

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [April 12, 2021, 10:26pm UTC](https://forum.inductiveautomation.com/t/change-string-data-to-time/45510/2 "2021-04-12T22:26:46Z")

</div>

You’re mixing Jython and the expression language here. Where are you trying to use this? Also, either way, the conversion from string to time would need to be applied to the tag value, rather than trying to read the value of the the string to time conversion as your code above does.

---

<div class="post-metadata">

**Author:** ![antonio.sanchez](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@antonio.sanchez](https://forum.inductiveautomation.com/u/antonio.sanchez)\
**Post date:** [April 12, 2021, 10:30pm UTC](https://forum.inductiveautomation.com/t/change-string-data-to-time/45510/3 "2021-04-12T22:30:09Z")

</div>

I want it to save the data in a database

---

<div class="post-metadata">

**Author:** ![antonio.sanchez](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@antonio.sanchez](https://forum.inductiveautomation.com/u/antonio.sanchez)\
**Post date:** [April 12, 2021, 10:31pm UTC](https://forum.inductiveautomation.com/t/change-string-data-to-time/45510/4 "2021-04-12T22:31:54Z")

</div>

```auto
if currentValue.value == False and currentValue.value != previousValue.value and not initialChange:
                    INS = "INSERT INTO PAROS ( Linea, Maquina, TipoParo, TiempoParo, Fecha) VALUES ( ?, ?, ?, ?, ?)"
                                
                    Contador = system.tag.read(dateFormat(addSeconds(midnight(now()),"[.]MemTimeDiff","HH:mm:ss"))
                    Fecha = system.date.now()
                                
                    system.db.runPrepUpdate(INS,["Item", "Prueba", "Prueba", Contador.value, Fecha],"TOY")

```

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [April 12, 2021, 11:16pm UTC](https://forum.inductiveautomation.com/t/change-string-data-to-time/45510/5 "2021-04-12T23:16:14Z")

</div>

1. These are all [expression language](https://docs.inductiveautomation.com/display/DOC81/Expression+Functions) and will not work here:  
`dateFormat(addSeconds(midnight(now(`  
You will need to use python functions to manipulate the date.
2. I don’t believe you can use a relative tag path here–replace `[.]MemTimeDiff` with the full path to the tag–something like the example in the next point. **EDIT: You actually can use this here, as others have pointed out.**
3. If your tag read was working, it would give you a qualified value which isn’t what you want to apply the other time manipulations to. You want to extract the value out of that by adding `.value` before trying to manipulate it:  
`system.tag.read("[default]MemTimeDiff").value`
4. You need to read the tag value (something like previous line), and then manipulate it with [scripting language](https://docs.inductiveautomation.com/display/DOC81/system.date) (not expression language). Do not try to manipulate it within the `system.tag.read` as in your example. You’ll end up with something like the following (untested).

```python
timeDiff = system.tag.read("[default]MemTimeDiff").value
Contador = system.date.format(system.date.addSeconds(system.date.midnight(system.date.now()), timeDiff), "HH:mm:ss")
```

---

<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:** [April 12, 2021, 11:20pm UTC](https://forum.inductiveautomation.com/t/change-string-data-to-time/45510/6 "2021-04-12T23:20:59Z")

</div>

@witman [this topic](http://forum.inductiveautomation.com/t/save-information-automatically-at-the-end-of-the-count/45400) will help at least clarify the relative tagpath ( **tl;dr** It’s a tag event script)

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [April 13, 2021, 4:16am UTC](https://forum.inductiveautomation.com/t/change-string-data-to-time/45510/7 "2021-04-13T04:16:33Z")

</div>

You actually **can** use relative paths in Jython 🙂 for example in a tag change script

---

<div class="post-metadata">

**Author:** ![antonio.sanchez](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@antonio.sanchez](https://forum.inductiveautomation.com/u/antonio.sanchez)\
**Post date:** [April 13, 2021, 1:32pm UTC](https://forum.inductiveautomation.com/t/change-string-data-to-time/45510/8 "2021-04-13T13:32:58Z")

</div>

I get an error back

> ([default]CONTEO DE FALLA/Falla safety fance open 1, valueChanged) Error executing tag event script: Traceback (most recent call last): File "tagevent:valueChanged", line 6, in valueChanged AttributeError: 'com.inductiveautomation.ignition.common.script.Imm' object has no attribute 'data'

> com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File "", line 6, in valueChanged AttributeError: 'com.inductiveautomation.ignition.common.script.Imm' object has no attribute 'data'

at org.python.core.Py.AttributeError(Py.java:173)

at org.python.core.PyObject.noAttributeError(PyObject.java:930)

at org.python.core.PyObject. **getattr** (PyObject.java:925)

at org.python.pycode.\_pyx97434703.valueChanged$1(:9)

at org.python.pycode.\_pyx97434703.call\_function()

at org.python.core.PyTableCode.call(PyTableCode.java:165)

at org.python.core.PyBaseCode.call(PyBaseCode.java:301)

at org.python.core.PyFunction.function\_\_\_call\_\_(PyFunction.java:376)

at org.python.core.PyFunction. **call** (PyFunction.java:371)

at org.python.core.PyFunction. **call** (PyFunction.java:361)

at org.python.core.PyFunction. **call** (PyFunction.java:356)

at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:649)

at com.inductiveautomation.ignition.common.sqltags.scripts.TagScriptManager$FunctionInvokerImpl.run(TagScriptManager.java:493)

at com.inductiveautomation.ignition.common.sqltags.scripts.AbstractTagScript.invoke(AbstractTagScript.java:33)

at com.inductiveautomation.ignition.common.sqltags.scripts.TagScriptManager$Task.invoke(TagScriptManager.java:442)

at com.inductiveautomation.ignition.common.sqltags.scripts.TagScriptManager$TagScriptDispatcher.run(TagScriptManager.java:405)

at com.inductiveautomation.ignition.common.execution.impl.BasicExecutionEngine$ThrowableCatchingRunnable.run(BasicExecutionEngine.java:518)

at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)

at java.util.concurrent.FutureTask.run(Unknown Source)

at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Unknown Source)

at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

Caused by: org.python.core.PyException: Traceback (most recent call last): File "", line 6, in valueChanged AttributeError: 'com.inductiveautomation.ignition.common.script.Imm' object has no attribute 'data'

... 24 common frames omitted

this is the code

```
if currentValue.value == False and currentValue.value != previousValue.value and not initialChange:
				INS = "INSERT INTO PAROS (Linea, Maquina, TipoParo, TiempoParo, Fecha) VALUES (?, ?, ?, ?, ?)"			
				
				timeDiff = system.tag.read("[.]MemTimeDiff").value
				Contador = system.date.format(system.date.addSeconds(system.date.midnight(system.data.now()), timeDiff), "HH:mm:ss")
				Fecha = system.date.now()	
				
				system.db.runPrepUpdate(INS,["Item","Prueba","Prueba",Contador.value,Fecha],"TOPY_MW")

```

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [April 13, 2021, 1:41pm UTC](https://forum.inductiveautomation.com/t/change-string-data-to-time/45510/9 "2021-04-13T13:41:14Z")

</div>

You have a typo in your code.

You have `data` when you should have `date`

> [@](#):
>
> Caused by: org.python.core.PyException: Traceback (most recent call last): File “”, line 6, in valueChanged AttributeError: ‘com.inductiveautomation.ignition.common.script.Imm’ object has no attribute ‘data’

```auto
if currentValue.value == False and currentValue.value != previousValue.value and not initialChange:
				INS = "INSERT INTO PAROS (Linea, Maquina, TipoParo, TiempoParo, Fecha) VALUES (?, ?, ?, ?, ?)"			
				
				timeDiff = system.tag.read("[.]MemTimeDiff").value
				Contador = system.date.format(system.date.addSeconds(system.date.midnight(system.date.now()), timeDiff), "HH:mm:ss")
				Fecha = system.date.now()	
				
				system.db.runPrepUpdate(INS,["Item","Prueba","Prueba",Contador.value,Fecha],"TOPY_MW")

```

---

<div class="post-metadata">

**Author:** ![antonio.sanchez](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@antonio.sanchez](https://forum.inductiveautomation.com/u/antonio.sanchez)\
**Post date:** [April 13, 2021, 2:30pm UTC](https://forum.inductiveautomation.com/t/change-string-data-to-time/45510/10 "2021-04-13T14:30:43Z")

</div>

> ([default]CONTEO DE FALLA/Falla safety fance open 1,  
> valueChanged) Error executing tag event script:  
> Traceback (most recent call last): File  
> "tagevent:valueChanged", line 6, in valueChanged  
> TypeError: addSeconds(): 2nd arg can't be coerced  
> to int

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [April 13, 2021, 2:39pm UTC](https://forum.inductiveautomation.com/t/change-string-data-to-time/45510/11 "2021-04-13T14:39:19Z")

</div>

> [@antonio.sanchez](#):
>
> TypeError: addSeconds(): 2nd arg can’t be coerced  
> to int

It doesn't like `timeDiff`, is that tag of type string? You can make the `timeDiff` tag of type int or maybe use

```auto
int(system.tag.read("[.]MemTimeDiff").value)

```

---

<div class="post-metadata">

**Author:** ![antonio.sanchez](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@antonio.sanchez](https://forum.inductiveautomation.com/u/antonio.sanchez)\
**Post date:** [April 13, 2021, 7:42pm UTC](https://forum.inductiveautomation.com/t/change-string-data-to-time/45510/12 "2021-04-13T19:42:12Z")

</div>

is already saving time.

only instead of saving 40 min. save me 40 seconds.

```auto
	if currentValue.value == False and currentValue.value != previousValue.value and not initialChange:
					INS = "INSERT INTO PAROS (Linea, Maquina, TipoParo, TiempoParo, Fecha) VALUES (?, ?, ?, ?, ?)"			
					
					timeDiff = int(system.tag.read("[.]../MemTimeDiff").value)
					Contador = system.date.format(system.date.addSeconds(system.date.midnight(system.date.now()), timeDiff), "HH:mm:ss")
					Fecha = system.date.now()	
					
					system.db.runPrepUpdate(INS,["Item","Prueba","Prueba",Contador,Fecha],"TOPY_MW")

```

---

<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:** [April 13, 2021, 8:02pm UTC](https://forum.inductiveautomation.com/t/change-string-data-to-time/45510/13 "2021-04-13T20:02:58Z")

</div>

Is `[.]../MemTimeDiff` minutes? If so, you need to add minutes, not seconds:

```auto
Contador = system.date.format(system.date.addMinutes(system.date.midnight(system.date.now()), timeDiff), "HH:mm:ss")

```
