Convert text format to date format

717
8
Jump to solution
10-15-2021 02:44 AM
Labels (1)
bhariadi
New Contributor III

Hello,

Please help me how to convert DD.MM.YYYY stored as Text format to YYYY/MM/DD as Date format using ArcGIS Pro Calculate Field?

 

Capture138.JPG

 

1 Solution

Accepted Solutions
TimOrmsby
Esri Regular Contributor

Hi, the Convert Time Field geoprocessing tool is another option.

View solution in original post

8 Replies
JohannesLindner
MVP Regular Contributor

EDIT: Updated the code. I forgot that the Date function uses a zero based month. Thanks, @KenBuja !

Date Functions | ArcGIS Arcade

To convert your strings to dates: calculate date field, switch to Arcade

 

if(IsEmpty($feature.DateText)) {
  return null
}
var date_split = Split($feature.DateText, ".")
return Date(date_split[2], date_split[1] - 1, date_split[0])

 

 

To change how the date is displayed in the table:

https://pro.arcgis.com/en/pro-app/latest/help/data/tables/format-numeric-and-date-fields.htm 


Have a great day!
Johannes
KenBuja
MVP Esteemed Contributor

Please note that this will give you an incorrect date, since the Month value in the Date function is 0-based. Use this instead.

if(IsEmpty($feature.DateText)) {
  return null
}
var date_split = Split($feature.datetext, ".")
return Date( date_split[2], date_split[1] - 1, date_split[0])

 

0 Kudos
bhariadi
New Contributor III

Hi @KenBuja ,

Thank you for your response, I also try your script:

if(IsEmpty($feature.DateText)) {
  return null
}
var date_split = Split($feature.datetext, ".")
return Date( date_split[2], date_split[1] - 1, date_split[0])

 

and give me this result:

Capture139.JPG

0 Kudos
KenBuja
MVP Esteemed Contributor

You have to set the time zone for your layer since it defaults to GMT. Your time zone is seven hours off.

0 Kudos
bhariadi
New Contributor III

Hi @JohannesLindner ,

Thank you for your response. I have tried the arcade script & get this result.

Capture140.JPG

 

0 Kudos
curtvprice
MVP Esteemed Contributor

Python:

# expression
f(!DTFIELD!)

# code block - assuming "yyyy.mm.dd"
def f(dt):
    try:
        d = dt.split(".")
        return datetime.datetime(int(d[2]), int(d[0], int(d[1])
    except:
        return None

ArcGIS Pro Help: Calculate Field Python Examples - Dates

0 Kudos
TimOrmsby
Esri Regular Contributor

Hi, the Convert Time Field geoprocessing tool is another option.

bhariadi
New Contributor III

Hi @TimOrmsby 

Thank you for the solution. With this geoprocessing tool I don't need to run a script.

This is the result:

Capture141.JPG

0 Kudos