Select to view content in your preferred language

Python Date Stamping

5384
13
Jump to solution
10-20-2014 08:03 AM
StephenEldridge
Deactivated User


Anyone know how to make python put a date stamp on a filename when creating a feature?

Thanks,

Steve

0 Kudos
1 Solution

Accepted Solutions
StephenEldridge
Deactivated User

Here is how I had to code it:

import time

from datetime import Date

#Sets a variable to hold today's date

currentdate = today()

  1. Process: Feature Class to Feature Class (6)

arcpy.FeatureClassToFeatureClass_conversion(in, out loc, "output_"+str(currentdate)

The result is: output_2014-10-23

The “-“ causes failure as an invalid character

View solution in original post

0 Kudos
13 Replies
BrandonKeinath1
Deactivated User

Hi Steve,

Can you clarify your question a little?  Are you looking to put a date stamp on a new feature class?  I've written a few scripts that utilize dates and times and the method changes a little based on where you want to put the data.  Let me know and I can try to help.


Thanks,


Brandon

0 Kudos
StephenEldridge
Deactivated User


Hi Brandon,

Yes on a new feature class.  I am running a feature to feature script that filters specific data for each feature and I need to date stamp them so that I can view changes in a side by side comparison and the output goes into a File Geodatabase.

Thanks,

Steve

0 Kudos
BrandonKeinath1
Deactivated User

And you would like the date stamp to be on the feature class?  So you could check today's "River" export against yesterday's "River" feature class?  If I'm understanding you right then I would suggest importing datetime.  Depending on how you want to format the date you have some options shown here 8.1. datetime — Basic date and time types — Python 2.7.8 documentation

import datetime

#Set a variable to hold today's date

currentdate = datetime.date.today()

#Execute the feature to feature function.  Casting as a string just to be safe

arcpy.FeatureClassToFeatureClass_conversion(in_features, out_path, out_name + str(currentdate))

StephenEldridge
Deactivated User

Yes, that is basically what I am trying to do, I will give that a try and let you know how it goes.

Thanks for your quick responses,

Steve

0 Kudos
BrandonKeinath1
Deactivated User

You're welcome and good luck.  If it gives you trouble let me know and I can try to help.

Brandon

0 Kudos
StephenEldridge
Deactivated User

Thanks

0 Kudos
StephenEldridge
Deactivated User

Good morning Brandon,

I figured out how to send the string to the feature output but the format is 2014-10-23 and I need it to be 20141023. I have read through documentation and have not been able to figure out how to do this format could you please assist?

Thanks,

Steve

0 Kudos
BrandonKeinath1
Deactivated User

Happy to,

I'm not sure how you ended up coding it but you can modify your date object with .strftime which lets you tell the date objects how to orient themselves.  There is a really good reply on stack overflow datetime - How to print date in a regular format in Python? - Stack Overflow

But in your case I would say code like currentdate.strftime(%Y%b%d) should format it the way you want.  You can reference the formatting letters in either the link or the help for strftime.

Good luck,

Brandon

0 Kudos
StephenEldridge
Deactivated User

Here is how I had to code it:

import time

from datetime import Date

#Sets a variable to hold today's date

currentdate = today()

  1. Process: Feature Class to Feature Class (6)

arcpy.FeatureClassToFeatureClass_conversion(in, out loc, "output_"+str(currentdate)

The result is: output_2014-10-23

The “-“ causes failure as an invalid character

0 Kudos