Calculate Field convert date to text

889
7
Jump to solution
12-03-2019 11:15 AM
ModernElectric
Occasional Contributor III

Greetings:

I have a python script written that has a small section that converts a date (11/5/2019) in date format into a text format (Nov-2019). The code is within a Calculate Field tool command and it works just fine. However, the end results of Nov-2019 is not exactly what I want. The end result that I am looking for is NOV-2019. Is there a way in the code that I can tell the tool to capitalize the month so it shows up as NOV-2019 instead of Nov-2019? OR is there a different command tool I need to add into my python script to change the month from lowercase to uppercase? Having a hard time finding the answer.

Here is my code I have for converting the date-time to text:

arcpy.CalculateField_management(ELECTRIC_METER_READING_RAWDATA_VIEW, "READING_DATE_TEXT_CONVERT", "(datetime.datetime.strptime( !READING_DATE_TEXT! ,'%m/%d/%Y %H:%M:%S %p')).strftime('%b-%Y')", "PYTHON", "")

if I use the %B it comes out with the full month instead of the abbreviated month.

Thank You for your help

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Hi ModernGIS 

Sorry, my bad, it seems that I am mixing languages... The ".upper()" should be at the end of the expression. Something like this:

arcpy.CalculateField_management(ELECTRIC_METER_READING_RAWDATA_VIEW,"READING_DATE_TEXT_CONVERT","((datetime.datetime.strptime( !READING_DATE_TEXT! ,'%m/%d/%Y %H:%M:%S %p')).strftime('%b-%Y')).upper()","PYTHON","")

Could you try again?

View solution in original post

7 Replies
XanderBakker
Esri Esteemed Contributor

Hi ModernGIS ,

Can you try this?

arcpy.CalculateField_management(ELECTRIC_METER_READING_RAWDATA_VIEW,"READING_DATE_TEXT_CONVERT","upper((datetime.datetime.strptime( !READING_DATE_TEXT! ,'%m/%d/%Y %H:%M:%S %p')).strftime('%b-%Y'))","PYTHON","")
0 Kudos
ModernElectric
Occasional Contributor III

Thank you for the response. However, the code didn't seem to work. I copied it to my python script to replace the existing and ran the script. Got a response that said:

   NameError: name 'upper' is not defined

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi ModernGIS 

Sorry, my bad, it seems that I am mixing languages... The ".upper()" should be at the end of the expression. Something like this:

arcpy.CalculateField_management(ELECTRIC_METER_READING_RAWDATA_VIEW,"READING_DATE_TEXT_CONVERT","((datetime.datetime.strptime( !READING_DATE_TEXT! ,'%m/%d/%Y %H:%M:%S %p')).strftime('%b-%Y')).upper()","PYTHON","")

Could you try again?

ModernElectric
Occasional Contributor III

Thank you for the revision. That did the trick. So I can understand and possibly implement it into other scripts, how does putting the .upper() into the code block work? Are there other things that I can add in it's place for other variables and/or circumstances?

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Modern Electric ,

There are a large number of "helpers" that you can use in the single line expression:

You can also put your logic in the Code Block and import modules and do some really complex stuff. However, when you feel the need to use code in the code block, you might what to evaluate if it is not easier to create a stand alone script to do the processing and have more control over the process. 

Here is some further reading: Calculate Field Python examples—Data Management toolbox | ArcGIS Desktop 

ModernElectric
Occasional Contributor III

Thank You for your help. I will do some more research to see how it may help some of my other scripts.

ModernElectric
Occasional Contributor III

I am doing a follow-up on this solution you assisted with awhile ago, but this time for a different project. I am understanding the basics but still having some issues.

Lets say I have a DATE field that is populated from a different database. However, within the field the dates/times are produced differently. Some may be 2/25/2010 7:41:00 AM and others *In the Same Field* are 7/9/2008 *Missing the time*

When I run the script on the whole column and try to convert, it crashes due to different date formats. 

My goal is to get all of them into the Day of the Week, month day and year. I have figured out how to do that part but with (2) sometimes (3) different date formats, its not working. Is there a way to take ALL of the dates, regardless if they are different format and convert them ALL to the same format?

So, 2/25/2010 7:41:00 AM and 7/9/2008 with ONE command would change to Weekday, Month Day, Year?

I am stuck and my research isn't working.

Thank You

0 Kudos