# Calculate Date Field.

6005
15
10-04-2016 11:52 AM
Frequent Contributor

I am trying to calculate a date/time field in Python with not much luck...

In ArcGIS I am doing this with a calculate and it calculates fine...its going into a Date Field.

(Right("00" & [Hour],2) + ":" + Right("00" & [Minute],2))

result:  4:02:00 PM

I want to do this in Python and am trying this after reading in a CSV File.  No error but no result in the feature class created.

If I dont get the syntax correct it tells me "Right" is not defined

Hour = str(lineSplit[3])
Minute = str(lineSplit[4])

CalcDate1 = "Right('00' & Hour,2) & ':' & Right('00' & Minute,2)"
print CalcDate1

It gives me this in the results window in the shell:

Right('00' & Hour,2) & ':' & Right('00' & Minute,2)

1 Solution

Accepted Solutions
Frequent Contributor

Mitch Holley, Darren Wiens THANK YOU FOR YOUR HELP AND SUPPORT....Things are running great...although not the most efficient way but I got it...with YOUR help of course

ANY QUESTIONS:  please feel free to reply and I will get back with you..

THANKS Again Mitch and Darren....SOOOO very much appreciated.

1. Read CSV to file to get all records

2. I calculated the Time field (CalcTime) from an Hour and Minute Field..

12:02:00 AM

3. Once I had that I calculated the Full CalcDateTime field from CalcTime above and the Date field from the CSV

6/29/2016 12:02:00 AM

4. Inserted ROWS into the Feature Class using SHAPE@XY Token to create the geometry

Hour Minute from CSV File

0          2

getTime3 Variable - CalcTime Field Result in New Feature Class
12:02:00 AM

getDateTime Variable - CalcDateTime field result in the New Feature Class concatenated with the Date field from the CSV File

6/29/2016 12:02:00 AM

FULL CODE BELOW

import arcpy
import datetime

def getdate(Hour, Minute):
return datetime.datetime.strptime(str(Hour).zfill(2) + ' ' + str(Minute).zfill(2), '%H %M')

fc = r'C:\Users\\Collar\BearLocationImports.gdb\BearCollar'

cursor = arcpy.da.InsertCursor (fc,["CollarSeri", "Year", "Julianday", "Hour", "Minute", "Activity", "Temperatur", "Latitude", "Longitude", "HDOP", "NumSats", "FixTime", "Date", "_2D_3D", "BearID", "CalcDate", "CalcDate3", "SHAPE@XY"])

for ln in open (r"C:\Users\tjv36463\Desktop\Bear Collar\rangedate_D032495_20160822124736_NH.txt", 'r').readlines():

#Each line in the csv is a string, so turn it into a list so you can reference each column
lineSplit = ln.split(",")

#Use index positions to get the right column from the csv
#Make sure the data is the correct type (i.e. X and Y need to be numbers, not strings)
CollarSeri = long(lineSplit[0])
Year = str(lineSplit[1])
Julianday = str(lineSplit[2])
Hour = int(lineSplit[3])
Minute = int(lineSplit[4])
Activity = int(lineSplit[5])
Temperature = int(lineSplit[6])
Latitude = float(lineSplit[7])
Longitude = float(lineSplit[8])
HDOP = str(lineSplit[9])
NumSats = int(lineSplit[10])
FixTime = int(lineSplit[11])
_2D_3D = int(lineSplit[12])

BearID = 0

Date = str(lineSplit[13]) # working with Date Time http://stackoverflow.com/questions/1521906/how-to-specify-date-and-time-in-python

getTime = getdate(Hour, Minute)
getTime2 = str(getTime)
getTime3 = getTime2[-8:]

getDateTime = (Date + getTime3)
print getDateTime

shapeVal = (Longitude, Latitude)

#Insert the values from csv into feature class
cursor.insertRow ([CollarSeri, Year, Julianday, Hour, Minute, Activity, Temperature, Latitude, Longitude, HDOP, NumSats, FixTime, Date, _2D_3D, BearID, getTime3, getDateTime, shapeVal])

count += 1

del cursor
print ("done")

15 Replies
MVP Regular Contributor

So, are you trying to read through the CSV and get the date from different fields?  Can you show the fields?

Frequent Contributor

This is how I am going through the CSV and assigning the variables for each record....I then write to the Feature Class.

for ln in open (r"C:\Users\tjv36463\Desktop\Bear Collar\rangedate_D032495_20160822124736_NH.txt", 'r').readlines():
lineSplit = ln.split(",")

Hour = int(lineSplit[3])
Minute = int(lineSplit[4])

CalcDate1 = Right('00' & Hour,2) & ':' & Right('00' & Minute,2)

In ArcGIS using calculate on a date field with this: Right('00' & [Hour],2) & ':' & Right('00' & [Minute],2) I get this: 4:00:00 PM it the Values were 16 for hour and 2 for Minute

Just need to translate this to Python

Edit:  Looks like ArcGIS is converting the Military time to normal time....think I need to add this into Python or is there a date handler of some sort to do that conversion?

Frequent Contributor

Yes as I parse the CSV I set the Hour and Minute to Variables...

Hour Minute

16       2

This results in 4:02:00 PM when I run it in ArcGIS with the formula above.

MVP Honored Contributor
think I need to add this into Python or is there a date handler of some sort to do that conversion?

Yes, the normal way to deal with date/time in Python is via datetime.

e.g.

Expression:

``getdate( !Hour! , !Minute! )‍‍``

Codeblock:

``````import datetime
def getdate(hour, minute):
return datetime.datetime.strptime(str(hour).zfill(2) + ' ' + str(minute).zfill(2), '%H %M')‍‍‍‍‍‍``````

Result:

Frequent Contributor

Think I got it....one second...

1. I still get military time and missing the AM PM

2. Only looking for the time not the date on the front...

Not really....did this and got this..but getting closer...

If I use this I get error:  getDateTime = getdate(!Hour!, !Minute!)

RESULT:

1900-01-01 20:01:00
1900-01-01 00:01:00
1900-01-01 04:01:00
1900-01-01 12:02:00
1900-01-01 16:01:00
1900-01-01 20:01:00

CODE:

import arcpy
import datetime

def getdate(hour, minute):
return datetime.datetime.strptime(str(Hour).zfill(2) + ' ' + str(Minute).zfill(2), '%H %M')

fc = r'C:\Users\tjv36463\Desktop\Bear Collar\BearLocationImports.gdb\BearCollar'

cursor = arcpy.da.InsertCursor (fc,["CollarSeri", "Year", "Julianday", "Hour", "Minute", "Activity", "Temperatur", "Latitude", "Longitude", "HDOP", "NumSats", "FixTime", "Date", "_2D_3D", "BearID", "SHAPE@XY"])

for ln in open (r"C:\Users\tjv36463\Desktop\Bear Collar\rangedate_D032495_20160822124736_NH.txt", 'r').readlines():

lineSplit = ln.split(",")

Hour = int(lineSplit[3])
Minute = int(lineSplit[4])

getDateTime = getdate(Hour, Minute)
print getDateTime

MVP Honored Contributor

Did you import datetime? Otherwise, what's the full error message?

MVP Honored Contributor

I think the difference you're seeing between my example and yours, is that I'm writing the datetime object to a geodatabase date field through the field calculator and you're printing the datetime object directly.

From Fundamentals of date fields—Help | ArcGIS for Desktop: "A geodatabase formats the date as datetime yyyy-mm-dd hh:mm:ss AM or PM." So, no matter in which date format Python or my system settings would like to display the datetime object, once it's written to the geodatabase date field, it will be yyyy-mm-dd hh:mm:ss AM or PM.

Since you're printing the raw datetime object, it's either the Python datetime object itself or your system time settings that control what format it displays in the print output.

Regarding time only format, you will have to format the datetime object into a string using strftime(), and write it to a string type field (won't work in date type field).

Frequent Contributor

When I use my example above why do I still get military time and no AM PM...I can deal with the date at the front...just want the time to at least come out right.

Im gonna write this to a GDB field and see if it differs....

THANKS for you help....very much appreciated.

Frequent Contributor

That did it....when I wrote to my GDB field it was in normal time and had the AM PM on it....with my lack of knowledge here I think I might just strip the characters(Date) from the front of the variable and try that....I hate dates....

Unless you have an example of the .strftime