Select to view content in your preferred language

Concatenate Date and Time in a date field

5738
6
Jump to solution
08-27-2018 08:12 AM
AhmadSALEH1
Frequent Contributor

Hello,

I Have 3 dates fields as the image below:

What I am trying to do is to populate 'Created' and 'Updated' fields with values from  'Designdate' field  and  add time to this field.

The time value will be static and doesn't change ( 12:01:00 AM) the result should be something like this "1/1/2015 12:01:00 AM".

Till now I couldn't  figure out how to do this using python ,  what I have so far is the below script but this will populate the fields with  dates only without the time. Any Ideas?. 

import datetime
import arcpy


targetfc=r"D:\d2\New File Geodatabase.gdb\gf"
updateDateFields=['created','updated','DesignDate']
time='12:01:00 AM'

with arcpy.da.UpdateCursor(targetfc, updateDateFields) as userUpdateCursor2:
          for row in userUpdateCursor2:
             row[0]=row[2]
             row[1]=row[2]
             userUpdateCursor2.updateRow(row)
          



del userUpdateCursor2

Thank you,

0 Kudos
1 Solution

Accepted Solutions
AhmadSALEH1
Frequent Contributor

Thank you Dan, 

I used different   approach, and it worked with me, here is my script:

import datetime
import arcpy

targetfc=r"D:\d2\New File Geodatabase.gdb\gf2"
updateDateFields=['created','updated','LastUpdatedDate']
time='12:01:00 AM'

with arcpy.da.UpdateCursor(targetfc, updateDateFields) as userUpdateCursor2:
          for row in userUpdateCursor2:
             rowTime = "{0}".format(row[2]) # this prints somthing like 2015-1-1 00:00:00
             rowTime2=rowTime[:10] # this prints 2015-1-1
             rowTime3 = "{0} {1}".format(rowTime2, time) # this prints 2015-1-1 12:01:00 AM

            
             x = datetime.datetime.strptime(str(rowTime3), '%Y-%m-%d %I:%M:%S %p')

             print (x)
             row[0]=x
             row[1]=x
             userUpdateCursor2.updateRow(row)

del userUpdateCursor2

 

Thank you,

View solution in original post

0 Kudos
6 Replies
DanPatterson_Retired
MVP Emeritus

You can concatenate string/text data.

If one of the fields is a date, it needs to be converted to text (ie str(  …. ) ) before concatenation.

With the warning, that if you convert an object to text, it may not appear as expected and may need clarification before concatenating

a = datetime.datetime.now()

str(a)
'2018-08-27 16:43:26.454007'

str(a.date())
'2018-08-27'

str(a.date()) + " 00:00:00"
'2018-08-27 00:00:00'
AhmadSALEH1
Frequent Contributor

Dan,

I could do something like this:

import datetime
import arcpy


targetfc=r"D:\d2\New File Geodatabase.gdb\gf"
updateDateFields=['created','updated','DesignDate']
time='12:01:00 AM'

with arcpy.da.UpdateCursor(targetfc, updateDateFields) as userUpdateCursor2:
          for row in userUpdateCursor2:
             row[0]=str(row[2])+ ' ' + time
             row[1]=str(row[2])+ ' ' + time
             userUpdateCursor2.updateRow(row)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

But the issue here is that I am changing the dates to strings and will not be able to insert the values in Created /updated fields since they have date as field type!

Thank you,

0 Kudos
DanPatterson_Retired
MVP Emeritus

Then you can't concatenate you want to combine


a = datetime.datetime.combine(datetime.date(2018, 1, 1), datetime.time(00, 00)) 

a
datetime.datetime(2018, 1, 1, 0, 0)

print(a)

2018-01-01 00:00:00‍‍‍‍
AhmadSALEH1
Frequent Contributor

Thank you Dan, 

I used different   approach, and it worked with me, here is my script:

import datetime
import arcpy

targetfc=r"D:\d2\New File Geodatabase.gdb\gf2"
updateDateFields=['created','updated','LastUpdatedDate']
time='12:01:00 AM'

with arcpy.da.UpdateCursor(targetfc, updateDateFields) as userUpdateCursor2:
          for row in userUpdateCursor2:
             rowTime = "{0}".format(row[2]) # this prints somthing like 2015-1-1 00:00:00
             rowTime2=rowTime[:10] # this prints 2015-1-1
             rowTime3 = "{0} {1}".format(rowTime2, time) # this prints 2015-1-1 12:01:00 AM

            
             x = datetime.datetime.strptime(str(rowTime3), '%Y-%m-%d %I:%M:%S %p')

             print (x)
             row[0]=x
             row[1]=x
             userUpdateCursor2.updateRow(row)

del userUpdateCursor2

 

Thank you,

0 Kudos
DanPatterson_Retired
MVP Emeritus

combine didn't work?

AhmadSALEH1
Frequent Contributor

Hi Dan,

I tried your answer,  combine will work also, here is my script: 

import datetime
import arcpy

targetfc=r"D:\d2\New File Geodatabase.gdb\gf"
updateDateFields=['created','updated','DesignDate']


with arcpy.da.UpdateCursor(targetfc, updateDateFields) as userUpdateCursor2:
          for row in userUpdateCursor2:
             
             rowTime=str(row[2]) 
            
             y= (rowTime[:4]) #Y
             m= (rowTime[5:7]) #M
             d= (rowTime[8:10]) # D
             x= datetime.datetime.combine(datetime.date(int(y),int(m),int(d)), datetime.time(12, 00))
             
             print (x)
             row[0]=x
             row[1]=x
             userUpdateCursor2.updateRow(row)

del userUpdateCursor2
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos