Date format

Hello, is there a quick way to convert the date format “Mon Mar 03 4:14:39 CET 2014” to “2014-03-03 4:14:39”?

Thanks in advance.

Is this in a script? If so, Python will do it for you.

Check out time.strptime to parse your string into a time struct, then time.strftime to make it back into the string you want. (Don’t forget to import time.)

Yes, it is. I’ve been doing some tests but with this script

import time res = time.strptime("Wed Mar 26 10:13:54 CET 2014", "%a %b %d %H:%M:%S %Z %Y")
the following error is displayed:

ValueError: time data did not match format:  data=Wed Mar 26 10:13:54 CET 2014  fmt=%a %b %d %H:%M:%S %Z %Y

Any hints on what I’m doing wrong?

Which version of Ignition are you using? Try using the little “z” instead for the timezone.

shv, I copied and pasted your code into a script, and it worked fine for me. (Gave me a value of (2014, 3, 26, 2, 13, 54, 2, 85, 1) )

The month and weekday names and abbreviations depend on your locale. What locale is your computer set up for? Your JVM?

Kathy:

[quote]Timezone Europe/Madrid [GMT+1:00]
Locale es_ES[/quote]
adamaustin: I tried using “% z” but has not solved the problem.

[quote]Ignition Version: 7.6.4 (b2013112117)
Java Version: Sun Microsystems Inc. 1.6.0_27[/quote]

You can also use java.text.SimpleDateFormat…

[code]from java.text import SimpleDateFormat

inputFormat=SimpleDateFormat(“EEE MMM dd HH:mm:ss Z yyyy”) # Must match the format of the input string
outputFormat=SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”) # The format you want to convert to

dateIn=“Wed Mar 26 10:13:54 CET 2014” # Input Time/Date

processedDateIn=SimpleDateFormat.parse(inputFormat,dateIn) # Convert Input String to Java Date
dateOut=outputFormat.format(processedDateIn) # Convert Java Date to a New String

print dateOut
[/code]

1 Like

I happen to have a vm set up in French, so I tried the script there. As expected, res = time.strptime("Wed Mar 26 10:13:54 CET 2014", "%a %b %d %H:%M:%S %Z %Y") fails, but res = time.strptime("mer. mars 26 10:13:54 CET 2014", "%a %b %d %H:%M:%S %Z %Y") works.

First, I would try altering your script to import time res = time.strptime("mié mar 26 10:13:54 CET 2014", "%a %b %d %H:%M:%S %Z %Y") print res and test it out.

If your data is arriving in English, you have a couple of choices. Assuming you are on a Windows machine, Python is looking at the settings under Control Panel > Region and Language > Formats > Format for what the weekday and month abbreviations are. If you change that to English, Python will be happy with the English abbreviations, but your whole system will use English names and formats for any dates/times anywhere in the system – probably not what you want.

The alternative would be to set up a function that translates the abbreviations for you, using str.replace.

Just a note: I tried Jordan’s solution in my French vm, and it fails when using English abbreviations, but works with French. I think that no matter what, the abbreviation is going to need to match what the OS thinks it should be for the locale.

Okay, Using Kathy’s French date, I got it to work using a locale:

[code]from java.text import SimpleDateFormat
from java.util import Locale

inputFormat=SimpleDateFormat(“EEE MMM dd HH:mm:ss Z yyyy”, Locale.FRENCH) # Must match the format of the input string
outputFormat=SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”) # The format you want to convert to

dateIn=“mer. mars 26 10:13:54 CET 2014” # Input Time/Date

processedDateIn=SimpleDateFormat.parse(inputFormat,dateIn) # Convert to Java Date
dateOut=outputFormat.format(processedDateIn) # Convert Java Date to String

print dateOut
[/code]

So, try adding an English locale to the format string:

[code]from java.text import SimpleDateFormat
from java.util import Locale

inputFormat=SimpleDateFormat(“EEE MMM dd HH:mm:ss Z yyyy”, Locale.ENGLISH) # Must match the format of the input string
outputFormat=SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”) # The format you want to convert to

dateIn=“Wed. Mar 26 10:13:54 CET 2014” # Input Time/Date

processedDateIn=SimpleDateFormat.parse(inputFormat,dateIn) # Convert to Java Date
dateOut=outputFormat.format(processedDateIn) # Convert Java Date to String

print dateOut
[/code]

Ah, elegant! Much better than replacing strings.

I tried using Python’s locale to do the same thing. Let’s just say Python’s locale implementation isn’t ready for production work. :smiley:

[edit]
As a side note, Java’s Locale class doesn’t have a constant for Spanish. (Silly, but nothing we can do.)

For a Spanish locale, replace Locale.FRENCH with new Locale(“es”, “ES”) (not sure if the capitalization is necessary.)

It worked! Thank you very much guys. You have been very helpful.

Finally this solution is good for me.

[code]from java.text import SimpleDateFormat
from java.util import Locale

inputFormat=SimpleDateFormat(“EEE MMM dd HH:mm:ss Z yyyy”, Locale.ENGLISH) # Must match the format of the input string
outputFormat=SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”) # The format you want to convert to

dateIn=“Wed. Mar 26 10:13:54 CET 2014” # Input Time/Date

processedDateIn=SimpleDateFormat.parse(inputFormat,dateIn) # Convert to Java Date
dateOut=outputFormat.format(processedDateIn) # Convert Java Date to String

print dateOut[/code]
On the other hand I have no need to replace Locale.ENGLISH with new Locale (“es”, “ES”) because all dates are in English.

Regards

Kathy,
Is there a way to generate a unique number out of it in Ignition before 7.7
Like, generating it as 20140326 (yyyymmdd) out of the given date( Wed Mar 26 10:13:54 CET 2014)

Did you try using the techniques I suggested in March 2014 above?

Yes. It return the value 10, 3, 26, 0, 0, 0, 2, 85, -1, but cant make this as a number as it separated with ","

You can loop through the returned object and build a string from the values, or sum them, or whatever other method you would like to use.

import time 
res = time.strptime("Wed Mar 26 10:13:54 CET 2014", "%a %b %d %H:%M:%S %Z %Y")

sRes = ''
for i in range(len(res)):
	sRes += str(res[i])
	
print sRes
import time 
res = time.strptime("2014/03/26 10:13:54", "%Y/%m/%d %H:%M:%S")
# the right answer:
print time.strftime("%Y%m%d%H%M%S", res)
# the lazy answer:
print ''.join(map(str, (r for r in res if r >= 0)))
1 Like

You didn’t mention what version of Ignition you are running, but do you have system.date.* available to you?

dateString = "Wed Mar 26 10:13:54 CET 2014"
format = "E MMM d HH:mm:ss z yyyy"

parsedDate = system.date.parse(dateString, format)

dateOut = system.date.format(parsedDate, 'yyyyMMdd')
3 Likes

Irose.
Thanks and it works well. I am still playing with it to limit this length just enough to get YYYYMMDD. I don’t need time and time zone.
I know Ignition 7.9, this was very easy to do and I have done with system.date syntax as Jordan said. But this is in 7.5, that is the trouble going through.

Jordan,
Sorry that I didnt mention the version. This one is 7.5 and as you said, this was easy to achieve in 7.9 with system.date format.
Thanks again for your time and input. I really appreciate you all for the way being part of a query and response. It is an amazing forum and I hardly see these types of interactions with other group.
Have a wonderful day.

3 Likes