# Using Tag Values in Strings & Date Formatting

**URL:** <https://forum.inductiveautomation.com/t/using-tag-values-in-strings-date-formatting/32320>\
**Category:** Ignition\
**Tags:** ignition79, scripting\
**Created:** [February 16, 2020, 9:35pm UTC](https://forum.inductiveautomation.com/t/using-tag-values-in-strings-date-formatting/32320 "2020-02-16T21:35:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![s\_petrarca](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@s\_petrarca](https://forum.inductiveautomation.com/u/s_petrarca)\
**Post date:** [February 16, 2020, 9:35pm UTC](https://forum.inductiveautomation.com/t/using-tag-values-in-strings-date-formatting/32320/1 "2020-02-16T21:35:18Z")

</div>

I’m writing a script to export a Report that will run on the value change of a UDT mem tag and I’m having some issues with trying to dynamically adjust the file name of the Report pdf.  
I have a Job # (MO) and batch # (control number) stored on int mem tags and then a start date and end date stored on date mem tags. I’m trying to format the dates so that they’re “yyyy-MM-dd” but using system.date.format complains that “1st arg can’t be coerced to java.util.Date” and DateFormat doesn’t seem to exist in any capacity even with imports.  
On top of that. I’m having issues substituting the MO and c\_num into the string, getting errors saying “int argument required” when those tag values are ints. I was able to get a report generated with just the base string with no substitutions using the variable containing the string as the filename before but now it’s ignoring that string variable, choosing to create the PDF with the default “M-dd-dd hhPM” name.

```auto
import system
#if currentValue.value != previousValue.value and initialChange == false:
#Control # report range
batch = system.tag.read("[.]batchStart")
system.tag.write("[.]reportStart", batch)
system.tag.write("[.]reportEnd", system.date.now())
system.tag.write("[.]batchStart", system.date.now())

title = "Zone_Mem MO %i Control# %i Report %s - %s.pdf"
mo = system.tag.read("[.]MO")
c_num = system.tag.read("[.]ControlNum")
reportStart = system.tag.read("[.]reportStart")
rStart = system.date.format(reportStart, "MM-dd-yyyy")	
reportEnd = system.tag.read("[.]reportEnd")
rEnd = system.date.format(reportEnd, "MM-dd-yyyy")	
settings={"path":"C:\Capstone", "filename":title % (mo, c_num, rStart, rEnd), "format":"pdf"}
system.report.executeAndDistribute(path="C-num Report", project="Line5", action="save", actionSettings=settings)

```

---

<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:** [February 16, 2020, 9:42pm UTC](https://forum.inductiveautomation.com/t/using-tag-values-in-strings-date-formatting/32320/2 "2020-02-16T21:42:50Z")

</div>

Wherever you read a tag in a script, you get a QualifiedValue object, not the raw value. That explains your “cannot coerce” error, as a QV that contains a date is not itself a date. You need to add a .value to those QVs to get the actual value. Also, consider using .readAll() and .writeAll() when you work with multiple tags.

---

<div class="post-metadata">

**Author:** ![s\_petrarca](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@s\_petrarca](https://forum.inductiveautomation.com/u/s_petrarca)\
**Post date:** [February 17, 2020, 1:17am UTC](https://forum.inductiveautomation.com/t/using-tag-values-in-strings-date-formatting/32320/3 "2020-02-17T01:17:48Z")

</div>

I believe I at lease did the .value with the mo and c\_num to test them in the string and it still complained about expecting an int (it’s case sensitive, correct?). I’ll try that with the date formats and see if it will accept them.

---

<div class="post-metadata">

**Author:** ![s\_petrarca](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@s\_petrarca](https://forum.inductiveautomation.com/u/s_petrarca)\
**Post date:** [February 18, 2020, 1:51am UTC](https://forum.inductiveautomation.com/t/using-tag-values-in-strings-date-formatting/32320/4 "2020-02-18T01:51:33Z")

</div>

So after realizing that when you do a system.tag.read(), it always pulls the QualifiedValue of the tag and doesn’t actually pull the value unless you add a .value after the tag read. Doing that does successfully read the tag value in its proper format and everything seems to execute fine but it’s still ignoring my fileName for the exported Report and saving it as the default “2-17-20 08PM”. I can tell it executes properly because the File Explorer modified date reflects the execution time and just saves over that default fileName.

```auto
	import system
	#if currentValue.value != previousValue.value and initialChange == false:
	#Control # report range
	batch = system.tag.read("[.]batchStart")
	report = ["[.]reportStart", "[.]reportEnd","[.]batchStart"]
	adjust = [batch, system.date.now(), system.date.now()]
	adjust = system.tag.writeAll(report, adjust)
	
	title = "Zone_Mem MO %i Control# %i Report %s - %s.pdf"
	range = ["[.]MO.Value", "[.]ControlNum", "[.]reportStart", "[.]reportEnd"]
	stats = system.tag.readAll(range)
	rStart = system.date.format(stats[2].value, "MM-dd-yyyy")	
	rEnd = system.date.format(stats[3].value, "MM-dd-yyyy")	
	settings={"path":"C:\Capstone", "filename":title % (stats[0].value, stats[1].value, rStart, rEnd), "format":"pdf"}
	system.report.executeAndDistribute(path="C-num Report", project="Line5", action="save", actionSettings=settings)

```

---

<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:** [February 18, 2020, 4:29am UTC](https://forum.inductiveautomation.com/t/using-tag-values-in-strings-date-formatting/32320/5 "2020-02-18T04:29:01Z")

</div>

> [@s\_petrarca](#):
>
> it’s still ignoring my fileName

> [@s\_petrarca](#):
>
> settings={"path":"C:\Capstone", "filename"

That's because it isn't receiving a `fileName` key; capitalize "N" in "filename".

```python
settings={"path":"C:\Capstone", "fileName":title % (stats[0].value, stats[1].value, rStart, rEnd), "format":"pdf"}

```

---

<div class="post-metadata">

**Author:** ![s\_petrarca](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@s\_petrarca](https://forum.inductiveautomation.com/u/s_petrarca)\
**Post date:** [February 18, 2020, 4:43am UTC](https://forum.inductiveautomation.com/t/using-tag-values-in-strings-date-formatting/32320/6 "2020-02-18T04:43:30Z")

</div>

You know, it’s the simple stuff that always throws us for a loop…  
I’ll go and fix that and I feel like I’ll be back when it comes time to edit the executeAndDistribute for email delivery but thank you guys for the help.

---

<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:** [February 18, 2020, 11:24am UTC](https://forum.inductiveautomation.com/t/using-tag-values-in-strings-date-formatting/32320/7 "2020-02-18T11:24:48Z")

</div>

Also, either change your slashes to forward or double them up. A backslash is a python escape character for injecting control codes.

---

<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:** [February 18, 2020, 3:43pm UTC](https://forum.inductiveautomation.com/t/using-tag-values-in-strings-date-formatting/32320/8 "2020-02-18T15:43:10Z")

</div>

Adding to @pturmel’s note, it looks like you got lucky with your path. `\C` works as you expected, but a lot of characters after the backslash would not. See examples below–the last two are safe ways of including backslashes, while #3 works using a forward slash instead.

```python
test = "C:\Capstone"
test2 = "C:\test"
test3 = "C:/test"
test4 = "C:\\test"
test5 = r"C:\test"
print test, test2, test3, test4, test5

```

Output–note `\t` becomes a tab:

```nohighlight
>>> 
C:\Capstone C:	est C:/test C:\test C:\test
>>> 

```
