Request help with a date conversion

I am trying to use a given date, take that date and essentially create a serial code for the date. Everything works like I want it to with one exception.

Here is the code:

dRan = row[27] 
print dRan
date = str(dRan)
print date
inputFormat = SimpleDateFormat("MM/dd/yyyy HH:mm:ss a")
dateOut = SimpleDateFormat.parse(inputFormat,dRan)
print dateOut

The results are
1/7/2021 7:47:22 PM
1/7/2021 7:47:22 PM
Thu Jan 07 07:47:22 CST 2021

this is causing the serial creation to be off from what it should be.
Here is the complete coding for it:

dRan = row[27]
print dRan
date = str(dRan)
print date
inputFormat = SimpleDateFormat("MM/dd/yyyy HH:mm:ss a")
dateOut = SimpleDateFormat.parse(inputFormat,dRan)
print dateOut
year = system.date.format(dateOut, "YY")
DoY = system.date.format(dateOut, "DDD")
HtoS = int(system.date.format(dateOut, "HH"))*3600
MtoS = int(system.date.format(dateOut, "mm"))*60
SoD = int(system.date.format(dateOut, "ss"))
seconds =  str(HtoS + MtoS + SoD).zfill(5)
f_name1 = year + DoY + seconds
print f_name1

The result of f_name1 is:
2100728042
it should be
2100771242

it seems that the format seems to ignore the ‘a’ for AM or PM and the time code is off by 12 hours.

I am not sure what I am doing wrong, any help will be greatly appreaciated.

I got this to work by changing the H’s to lower case. Uppercase HH is used for 24 hour format. This should do the trick.

SimpleDateFormat("MM/dd/yyyy hh:mm:ss a")
2 Likes

That did the trick, thank you very much for your help!!!

You can also use the https://docs.inductiveautomation.com/display/DOC81/system.date.parse function to avoid the manual SimpleDateFormat import.