adding hours in date filed to get the final date python scripts

4826
9
Jump to solution
04-24-2013 09:12 PM
nadeemfareed
New Contributor II
hello everyone.
i am using arcgis 10 and now a days working on network analyst. i got struck in a problem. network analyst is good to calculate the final date for a route on the basis of timewindow start. but my application got a formula that calculates some additional hours for a defined route. i have created a attribute table where i import the start date and then a blank column for final date which i want to populate with a python scripts that adding start date plus no hours gives the results.
e.g
3/4/2013 10:23 AM + 36:00 hr =     final date with time

any vb python script would be a good one  for
THANKS
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JamesCrandall
MVP Frequent Contributor
thanks for answer. but in this script just one date or time is given to be automated. my problem is bit complicated. i have two columns in my attribute table (Start_time, Total_time). another blank column with alis Finish_Time. I want to add these two to get finish_time column automated. remember the total time is in hours format not in date format. 
e.g.. 
Start_time+Total_time = finish_time


Please be VERY specific here. What are the exact field types for all 3 fields? (I don't quite understand what your "Total_time" field type is).


I just tested the code below and it updates field "finish_time" just fine by adding "Total_time" field to the "Start_time" field, with the output as a new datetime. However, it is important to know what the field types are:

Start_time: Date
Total_time: Short Integer
finish_time: Date

import arcpy import datetime import timedelta  fc = r"H:\Documents\ArcGIS\Default.gdb\TimeDelta" cursor = arcpy.UpdateCursor(fc) for row in cursor:   startdate = row.Start_time   totaltime = row.Total_time   upddate = datetime.timedelta(hours = totaltime)   later = startdate + upddate      row.setValue("finish_time", later)   cursor.updateRow(row)   

View solution in original post

0 Kudos
9 Replies
nadeemfareed
New Contributor II
hello everyone.
i am using arcgis 10 and now a days working on network analyst. i got struck in a problem. network analyst is good to calculate the final date for a route on the basis of timewindow start. but my application got a formula that calculates some additional hours for a defined route. i have created a attribute table where i import the start date and then a blank column for final date which i want to populate with a python scripts that adding start date plus no hours gives the results.
e.g
3/4/2013 10:23 AM + 36:00 hr =     final date with time

any vb python script would be a good one  for
THANKS

i request readers if they have any general kind of idea, please shere, its highly valueable for me. any solution can be implemented
if available. reply please
0 Kudos
MelindaMorang
Esri Regular Contributor
This sounds like a python question, so I have moved it to the python forum.  I hope you find your answer!
0 Kudos
JamesCrandall
MVP Frequent Contributor
Just as an example of how you might use the timedelta object, so you will have to decide how to implement it.  Below d1 is a string object representation of the date (I wasn't sure what field type was in your attribute table), so it shows this conversion but you may not necessarily need that.


import arcpy
import datetime
import timedelta

d1 = '3/4/2013 10:23'
d1frmt = datetime.datetime.strptime(d1, "%m/%d/%Y %H:%M")

addhours = 36
dt = datetime.timedelta(hours = addhours) 
later = d1frmt + dt

arcpy.AddMessage(str(later))



output date "later" is printed as: 2013-03-05 22:23:00
0 Kudos
MikeHunter
Occasional Contributor
James Crandall has given a very good answer on how to manipulate dates in Python.  I would add a couple of things.  First, don't try to import timedelta.  That will raise an exception.  But you don't need it, since it's part of datetime, so just leave that out and James's code will work perfectly.  Second, if you want to re-create a string with the same, original format from your new datetime object, simply add this line to the bottom of James's code:

new_datestr = later.strftime("%m/%d/%Y %H:%M")


good luck,
Mike
0 Kudos
ShahbazBaig
New Contributor
thanks for answer. but in this script just one date or time is given to be automated. my problem is bit complicated. i have two columns in my attribute table (Start_time, Total_time). another blank column with alis Finish_Time. I want to add these two to get finish_time column automated. remember the total time is in hours format not in date format.
e.g..
Start_time+Total_time = finish_time
0 Kudos
JamesCrandall
MVP Frequent Contributor
thanks for answer. but in this script just one date or time is given to be automated. my problem is bit complicated. i have two columns in my attribute table (Start_time, Total_time). another blank column with alis Finish_Time. I want to add these two to get finish_time column automated. remember the total time is in hours format not in date format. 
e.g.. 
Start_time+Total_time = finish_time


Please be VERY specific here. What are the exact field types for all 3 fields? (I don't quite understand what your "Total_time" field type is).


I just tested the code below and it updates field "finish_time" just fine by adding "Total_time" field to the "Start_time" field, with the output as a new datetime. However, it is important to know what the field types are:

Start_time: Date
Total_time: Short Integer
finish_time: Date

import arcpy import datetime import timedelta  fc = r"H:\Documents\ArcGIS\Default.gdb\TimeDelta" cursor = arcpy.UpdateCursor(fc) for row in cursor:   startdate = row.Start_time   totaltime = row.Total_time   upddate = datetime.timedelta(hours = totaltime)   later = startdate + upddate      row.setValue("finish_time", later)   cursor.updateRow(row)   
0 Kudos
nadeemfareed
New Contributor II
dear members
here i have been asked. what is the total time.
here total time is numer of hours
0 Kudos
JamesCrandall
MVP Frequent Contributor
dear members
here i have been asked. what is the total time.
here total time is numer of hours


That is not specific enough answer.  What FIELD TYPE is it?

I assumed in my last post it is INTEGER, which would be an appropriate choice.  See above for your solution.
0 Kudos
nadeemfareed
New Contributor II
import arcpy
import datetime

fc = r"H:\Documents\ArcGIS\Default.gdb\TimeDelta"
cursor = arcpy.UpdateCursor(fc)
for row in cursor:
  startdate = row.Start_time
  totaltime = row.Total_time
  upddate = datetime.timedelta(hours = totaltime)
  later = startdate + upddate
 
  row.setValue("finish_time", later)
  cursor.updateRow(row)

its excellent script. just import.timedelta is not required.
well done heros
0 Kudos