Is it possible to extract years from date format ?

3308
5
02-20-2023 08:33 AM
SoleneMas
Emerging Contributor

Good morning,

Allow me to ask you because I start with python and I have trouble knowing whether what I want to do is possible or not.
I would like to put in place a warning system for trees to be cut down.


I have a table with projected slaughter dates (maj_pevisionnel) and a year column that I use for my alerts symbology.
I would have liked to know if it was possible in the shift-forecast column to extract the year’s value from the date format and put it in the year column?
Or else by a condition to compare with this format the year of maj_previsional with the current year and assign it the desired symbology?

0 Kudos
5 Replies
DavidPike
MVP Frequent Contributor

Looks like arcmap you're using so I think the date structure is a bit funky.  Might be best just using the VB code in field calculator as dates in ArcGIS give me a headache at the nest of times.

DatePart("yyyy",[your date field])

 

0 Kudos
SoleneMas
Emerging Contributor

Good morning,

Allow me to ask you because I start with python and I have trouble knowing whether what I want to do is possible or not.
I would like to put in place a warning system for trees to be cut down.


I have a table with projected slaughter dates (maj_pevisionnel) and a year column that I use for my alerts symbology.
I would have liked to know if it was possible in the shift-forecast column to extract the year’s value from the date format and put it in the year column?
Or else by a condition to compare with this format the year of maj_previsional with the current year and assign it the desired symbology?

0 Kudos
BlakeTerhune
MVP Regular Contributor

If the field is a date type, then Python will read the value as a datetime object (at least it does with data access cursors). You can use the .year attribute to get only the year portion of a datetime object.

my_date_field.year

 

0 Kudos
DavidSolari
Frequent Contributor

The Field Calculator also loads datetime fields in as native datetime objects so it'll work there as well. If they need the year as a string in a different format then the strftime method and the format code of their choice should do what they need.

DuncanHornby
MVP Notable Contributor

@SoleneMas It's important you state what software you are using and what the format of your data is. Is it a shapefile or a file geodatabase, a date field or a text field? The answers currently being offered up are valid but they are making assumptions about data format which is critical in answer this question. Edit your question and add that detail.

0 Kudos