Extract two digit month part in field calculator

01-16-2022 01:22 AM
Labels (3)
New Contributor

i use datepart() to extract year and  month from current date and populate a field with "YYYYMM". but month parameter in datepart function doesnt allow setting it to always have two digits.

how can i extract month as MM as default? (for example March would be '03' instade of '3')



0 Kudos
2 Replies
MVP Esteemed Contributor

Does the following expression (Python Parser) work for you on a TEXT field?




If it doesn't, please provide a few more details

- What type of feature class (Shapefile, File GDB Feature Class, Enterprise FC-Oracle, PostgreSQL, SQL Server, etc.)?

- What expression have you got so far? Screenshot would help.

- Version of ArcGIS Pro?


Think Location
0 Kudos
MVP Esteemed Contributor

Calculate Field Python examples—ArcGIS Pro | Documentation has examples

!field1!.strftime("%m/%d/%Y, %H:%M:%S")

shows all the parts and the result

'02/22/2021, 10:15:00'

so as Jayanta says, you just need to parse the bits in the order you want


Where field1 is your field containing the data and your output field is a text field.

Note datetime and time are always available in the field calculator



... sort of retired...
0 Kudos