# Convert String to date

**URL:** <https://forum.inductiveautomation.com/t/convert-string-to-date/53302>\
**Category:** Ignition\
**Tags:** scripting, perspective\
**Created:** [November 19, 2021, 8:54pm UTC](https://forum.inductiveautomation.com/t/convert-string-to-date/53302 "2021-11-19T20:54:39Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![etlikmilos](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@etlikmilos](https://forum.inductiveautomation.com/u/etlikmilos)\
**Post date:** [November 19, 2021, 8:54pm UTC](https://forum.inductiveautomation.com/t/convert-string-to-date/53302/1 "2021-11-19T20:54:39Z")

</div>

Hello  
So can you please help me? I have the date as a string in this formát :  
Fri Nov 19 2021 18:02:03 GMT+0100 (Středoevropský standardní čas)  
And I need date as the date.  
I try to parse for the date but I have a problem with the format ☹  
Thank you

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/4/a4e38ac5062b6781d1300efb7cebfb5477c15766.png)

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/c/4c78f127844a9a8ff53f9f65e2e93c4ab98eb635.png)

---

<div class="post-metadata">

**Author:** ![etlikmilos](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@etlikmilos](https://forum.inductiveautomation.com/u/etlikmilos)\
**Post date:** [November 19, 2021, 10:06pm UTC](https://forum.inductiveautomation.com/t/convert-string-to-date/53302/2 "2021-11-19T22:06:50Z")

</div>

I try this :  
system.date.parse(timeEnd, “EEEE MMM dd yyyy hh:mm:ss Z zzzz”)  
but it dosent work ☹

---

<div class="post-metadata">

**Author:** ![josborn](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@josborn](https://forum.inductiveautomation.com/u/josborn)\
**Post date:** [November 19, 2021, 10:19pm UTC](https://forum.inductiveautomation.com/t/convert-string-to-date/53302/3 "2021-11-19T22:19:09Z")

</div>

Try using **strptime()**  
[https://www.tutorialspoint.com/python/time\_strptime.htm](https://www.tutorialspoint.com/python/time_strptime.htm)

---

<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:** [November 19, 2021, 10:25pm UTC](https://forum.inductiveautomation.com/t/convert-string-to-date/53302/4 "2021-11-19T22:25:36Z")

</div>

Try this: `EEE MMM dd yyyy hh:mm:ss zZ (zzzz)`

---

<div class="post-metadata">

**Author:** ![etlikmilos](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@etlikmilos](https://forum.inductiveautomation.com/u/etlikmilos)\
**Post date:** [November 19, 2021, 11:03pm UTC](https://forum.inductiveautomation.com/t/convert-string-to-date/53302/5 "2021-11-19T23:03:13Z")

</div>

Not working ☹  
I tried :  
system.date.parse(timeEnd, “E MMM dd yyyy HH:mm:ss zZ zzzz”)  
system.date.parse(timeEnd, “E MMM dd yyyy HH:mm:ss zZ (zzzz)”)  
system.date.parse(timeEnd, “EEE MMM dd yyyy HH:mm:ss zZ zzzz”)  
But it doesn’t work ☹

---

<div class="post-metadata">

**Author:** ![etlikmilos](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@etlikmilos](https://forum.inductiveautomation.com/u/etlikmilos)\
**Post date:** [November 19, 2021, 11:08pm UTC](https://forum.inductiveautomation.com/t/convert-string-to-date/53302/6 "2021-11-19T23:08:24Z")

</div>

When I tried this system.date.parse(“Fri Nov 05 2021 17:51:17”, “E MMM dd yyyy HH:mm:ss”) everything work good.  
The problem will be in the zZ zzzz .  
So any idea, please?

---

<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:** [November 19, 2021, 11:09pm UTC](https://forum.inductiveautomation.com/t/convert-string-to-date/53302/7 "2021-11-19T23:09:16Z")

</div>

Sorry, I’m out of ideas. It does appear to be related to the `Středoevropský standardní čas` portion. Is your gateway and the server it’s on all set up for a locale/region where that would make sense?

---

<div class="post-metadata">

**Author:** ![etlikmilos](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@etlikmilos](https://forum.inductiveautomation.com/u/etlikmilos)\
**Post date:** [November 19, 2021, 11:11pm UTC](https://forum.inductiveautomation.com/t/convert-string-to-date/53302/8 "2021-11-19T23:11:43Z")

</div>

I mean that yes I am from Czech Republic. And this App we use in or company with this setting.

---

<div class="post-metadata">

**Author:** ![etlikmilos](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@etlikmilos](https://forum.inductiveautomation.com/u/etlikmilos)\
**Post date:** [November 19, 2021, 11:17pm UTC](https://forum.inductiveautomation.com/t/convert-string-to-date/53302/9 "2021-11-19T23:17:47Z")

</div>

And when i try this :  
system.date.parse(“Fri Nov 05 2021 17:51:17 +0100 (Středoevropský standardní čas)”, “E MMM dd yyyy HH:mm:ss z”)  
It works well, the problem seems to be in GTM

---

<div class="post-metadata">

**Author:** ![etlikmilos](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@etlikmilos](https://forum.inductiveautomation.com/u/etlikmilos)\
**Post date:** [November 19, 2021, 11:45pm UTC](https://forum.inductiveautomation.com/t/convert-string-to-date/53302/10 "2021-11-19T23:45:01Z")

</div>

So i use this and seems to be it work 😃 But i know that is not the best solution ☹  
system.date.parse(timeStart.replace(“GMT”,""), “E MMM dd yyyy HH:mm:ss Z”);

---

<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:** [November 19, 2021, 11:46pm UTC](https://forum.inductiveautomation.com/t/convert-string-to-date/53302/11 "2021-11-19T23:46:55Z")

</div>

I think that’ll have to do for now. I cannot for the life of me figure out how to convince Java to parse that String into a Date. It seems convinced it’s an invalid combination of things for some reason.

---

<div class="post-metadata">

**Author:** ![etlikmilos](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@etlikmilos](https://forum.inductiveautomation.com/u/etlikmilos)\
**Post date:** [November 19, 2021, 11:48pm UTC](https://forum.inductiveautomation.com/t/convert-string-to-date/53302/12 "2021-11-19T23:48:18Z")

</div>

Thank you for helping and your time 😃

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 20, 2021, 11:38pm UTC](https://forum.inductiveautomation.com/t/convert-string-to-date/53302/13 "2021-11-20T23:38:13Z")

</div>

system.date.parse uses SimpleDateFormat under the hood. I went with java.time libraries and a regex to filter out stuff we don’t need. Hope this helps!

```python
def toDate(stringIn):
	""" Specific function to convert string timestamp to local java.util.Date
	    Example: 
	            stringIn: 'Fri Nov 19 2021 18:02:03 GMT+0100 (Středoevropský standardní čas)'
	             returns: Fri Nov 19 12:02:03 EST 2021
	"""
	from java.time.format import DateTimeFormatter
	from java.time import ZonedDateTime
	from java.util import Date, Locale
	import re

		
	format = DateTimeFormatter.ofPattern('EEE MMM dd yyyy HH:mm:ss zxx').withLocale(Locale('en_US'))
	regex = r'(.*) \('
	
	#Get timestamp from the string
	tStampString = re.search(regex, stringIn).groups()[0]
	
	# Parse timestamp string to Instant
	zdt = ZonedDateTime.parse(tStampString, format).toInstant()
	
	#Convert to java.util.Date
	return Date.from(zdt)

dateString = 'Fri Nov 19 2021 18:02:03 GMT+0100 (Středoevropský standardní čas)'

print toDate(dateString)

```

---

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [November 21, 2021, 7:45am UTC](https://forum.inductiveautomation.com/t/convert-string-to-date/53302/14 "2021-11-21T07:45:22Z")

</div>

I tried your example in Script console, but this is what I get:

 ![Screenshot_30](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/c/9c7ede22ff91f64f12ab8ebba127f520279cbfd0.png)

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 21, 2021, 12:37pm UTC](https://forum.inductiveautomation.com/t/convert-string-to-date/53302/15 "2021-11-21T12:37:19Z")

</div>

Hmm. Wonder if it’s a locale thing. I’ll look further when I get a chance.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 21, 2021, 1:21pm UTC](https://forum.inductiveautomation.com/t/convert-string-to-date/53302/16 "2021-11-21T13:21:12Z")

</div>

I was able to replicate. It is a locale thing.  
Updated the above script to use en\_US as the locale of the formatter, then tried a few different locales. See if that does it.

---

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [November 21, 2021, 3:18pm UTC](https://forum.inductiveautomation.com/t/convert-string-to-date/53302/17 "2021-11-21T15:18:20Z")

</div>

> [@JordanCClark](#):
>
> See if that does it

Yup... 👍
