I have an attribute table with an ALARM_DATE field (mm/dd/yyyy) (data type: Date) and I want to extract only the month from it to a new field.
What is the simplest way to achieve this in field calculator or python 3?
Solved! Go to Solution.
Thanks everyone. I ended up figuring it out with an Arcade solution using the date function Month.
date field is !ALARM_DATE!
new field I want only the month number is !Month_!
All you need to do is use the field calculator for new field ----> !Month_! (integer)
Choose Arcade, enter Month($feature.ALARM_DATE), this returns values of the month 0 - 11 in Month_ field.
Use field calculator again on !Month_! with Python to add 1 to make it 1-12, -----> !Month_! + 1
An example of a time field in a table called Timetest (python parser)
int(!Timetest!.month)
Now via example
n = datetime.datetime.now()
n
datetime.datetime(2018, 12, 15, 20, 11, 32, 784152)
type(n)
<class 'datetime.datetime'>
dir(n)
[... snip, 'astimezone', 'combine', 'ctime', 'date', 'day', 'dst', 'fold',
'fromordinal', 'fromtimestamp', 'hour', 'isocalendar', 'isoformat', 'isoweekday',
'max', 'microsecond', 'min', 'minute', 'month', 'now', 'replace', 'resolution',
'second', 'strftime', 'strptime', 'time', 'timestamp', 'timetuple', 'timetz',
'today', 'toordinal', 'tzinfo', 'tzname', 'utcfromtimestamp', 'utcnow', 'utcoffset',
'utctimetuple', 'weekday', 'year']
n.month # ---- which is now an integer
12
Have you tried using the Left () available in the Field Calculator under the String() tab?
How to extract Middle, Left, or Right characters in Calculate Field tool
Calculate Field Python examples—Data Management toolbox | ArcGIS Desktop
Thanks everyone. I ended up figuring it out with an Arcade solution using the date function Month.
date field is !ALARM_DATE!
new field I want only the month number is !Month_!
All you need to do is use the field calculator for new field ----> !Month_! (integer)
Choose Arcade, enter Month($feature.ALARM_DATE), this returns values of the month 0 - 11 in Month_ field.
Use field calculator again on !Month_! with Python to add 1 to make it 1-12, -----> !Month_! + 1