Select to view content in your preferred language

Calculate Date Field.

7010
15
Jump to solution
10-04-2016 11:52 AM
jaykapalczynski
Honored 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)

0 Kudos
15 Replies
jaykapalczynski
Honored Contributor

If I could get that one last step to only time I would be done...I cant thank you enough for this help...newbee is very grateful.

0 Kudos
DarrenWiens2
MVP Honored Contributor

Yeah, I hate dates, too. I've gotten by only using the final section of the datetime help page, where it explains strptime (converts string to datetime), strftime (converts datetime to string), and the date formatting codes.

import datetime 
my_string = '7:30AM March 15, 2000' # some date
dt = datetime.datetime.strptime(my_string, '%H:%M%p %B %d, %Y') # create datetime object from string
my_new_string = dt.strftime('%H-----%M') # create a string from some parts of the datetime object
print my_new_string‍‍‍‍‍

Result:

07-----30

Some things I think about (whether they're wrong or right):

- the datetime object is sort of formatless, but has all formats available through strftime (I suspect the format is defined in the help)

- datetime objects are very smart when it comes to date operations (like "what is the time 2074 minutes from 9:00pm on Friday, June 29?")

- those date formatting codes are important, and that's how you pull out the time components of the datetime object via strftime()

0 Kudos
jaykapalczynski
Honored Contributor

Dude cant thank you enough for all your help…it is so gratefully appreciated.

Thanks

Jay

Jay Kapalczynski

GIS Coordinator - Virginia Department of Game & Inland Fisheries

(new address) 7870 Villa Park Drive Suite 400, Henrico VA 23228

Phone: 804.367.6796 | Fax: 804.367.2628

ü Please consider the environment before printing this email.

0 Kudos
jaykapalczynski
Honored Contributor

I did it like this but dont think this is the best way....

getDateTime = getdate(Hour, Minute)
getDateTime2 = str(getDateTime)
getDateTime3 = getDateTime2[-8:]

# send getDateTime3 to the insert....came out alright...but lots of processing....

0 Kudos
jaykapalczynski
Honored 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")

0 Kudos
DanPatterson_Retired
MVP Emeritus

Notes and references   Messing with dates and time

Notes:
: - http://www.iso.org/iso/support/faqs/faqs_widely_used_standards/
: widely_used_standards_other/iso8601
: - http://strftime.org
: - Richard Fairhurst's blog
: https://community.esri.com/blogs/richard_fairhurst/2016/07/01/
: ive-saved-time-in-a-bottle-how-do-i-get-it-back-out-doing-more-
: with-date-fields-using-the-field-calculator-and-python

: - https://pymotw.com/3/datetime/
: The default string representation of a datetime object uses the ISO-8601
: format (YYYY-MM-DDTHH:MM:SS.mmmmmm). Alternate formats can be generated
: using strftime().


windows 3.x 2.7 from Richards
yy or y = %y (Year number without century with or without a leading zero)
yyyy = %Y (Year number with century)
MM or M = %m (Month number with or without a leading zero)
MMM = %b (Month as locale’s abbreviated name)
MMMM = %B (Month as locale’s full name)
dd or d = %d (Day of the month number with or without a leading zero)
ddd = %a (Weekday as locale’s abbreviated name)
dddd = %A (Weekday as locale’s full name)

hh or h = %I (Hour (12-hour clock) with or without a leading zero)
HH or H = %H (Hour (24-hour clock) with or without a leading zero)
mm or m = %M (Minute number with or without a leading zero)
ss or s = %S (Second number with or without a leading zero)
tt = %p (Locale’s equivalent of either AM or PM)
/, -, : = /, -, : (literal date or time separator characters)

Test output... fiddle with the options above to get what you want

def _demo():
    """   
    :Required:
    :--------
    :  import datetime
    :Returns
    :-------
    :
    """
    today = datetime.datetime.today()
    print('ISO     :', today)
    print('format(): {:%a %b %d %H:%M:%S %Y}'.format(today))
    return today
    

if __name__=="__main__":
    """   """
    #print("Script... {}".format(script))
    a  = _demo()

sample output.... change line 12 to get what you want

ISO     : 2016-10-04 17:06:34.598792
format(): Tue Oct 04 17:06:34 2016