Select to view content in your preferred language

get day and time from date field with time, with field calculator

1962
4
Jump to solution
02-17-2016 08:31 AM
GustavoCordero
Deactivated User

I'm using ArcGIS 10.1 and have a field containing a date and time I need to get the day of the week based on my date field and also when the same field that contains date and time

I need to do this from the field calculator

example

2016-02-17 10_15_41-Microsoft Excel - Libro1.png

Tags (1)
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

The script above was created to run as stand alone script, but if you have your table in you TOC, you can simply take the code below:

with arcpy.da.UpdateCursor('NombreDeLaTabla', ('Hora__GPS', 'Dia', 'Hora')) as curs:
    for row in curs:
        curs.updateRow((row[0], row[0].weekday() + 1, row[0].strftime('%H'), ))

... change the 'NombreDeLaTabla' on the first line into the name of your table, open the Python Window:

... paste the code:

.. and hit Enter.

(Check and change the names of the fields 'Hora__GPS', 'Dia', 'Hora' if your schema is different.)

View solution in original post

0 Kudos
4 Replies
AjitkumarBabar
Frequent Contributor

Kindly go though the link

Code samples—dates section -

Calculate Field examples—Data Management toolbox | ArcGIS for Desktop

DanPatterson_Retired
MVP Emeritus

over simplified, but you need to do slicing... convert dates to string first

>>> t = "02/02/2016 08:54:11 a.m."
>>> dia = t[:2]
>>> dia
'02'
>>> hora = t.split(" ")[1][:2]
>>> hora
'08'
>>>

Which would mean that you would have something like

dia field ....  str(!timefieldname!)[:2]

hora ....    str(!timefieldname!).split(" ")[1][:2]

or something like that

XanderBakker
Esri Esteemed Contributor

To bad you have to use the FieldCalculator, because it can be a real challenge to make it work. I tried the samples that Ajitkumar Babar refers to, but for some reason it throws errors like "The value type is incompatible with the field type". Parsing it to string will work, as Dan Patterson suggest, but you will have to account for the AM/PM setting to get the right hour (hora).

I you could use some Python code, then this is how you could do this:

def main():
    import arcpy
    tbl = r'C:\GeoNet\XLSDate\data.gdb\test_tbl2'

    fld_date = 'Hora__GPS'
    fld_dia = 'Dia'
    fld_hora = 'Hora'

    with arcpy.da.UpdateCursor(tbl, (fld_date, fld_dia, fld_hora)) as curs:
        for row in curs:
            fecha = row[0]
            dia = fecha.weekday() + 1
            hora = fecha.strftime('%H')
            curs.updateRow((fecha, dia, hora, ))

if __name__ == '__main__':
    main()

Result:

XanderBakker
Esri Esteemed Contributor

The script above was created to run as stand alone script, but if you have your table in you TOC, you can simply take the code below:

with arcpy.da.UpdateCursor('NombreDeLaTabla', ('Hora__GPS', 'Dia', 'Hora')) as curs:
    for row in curs:
        curs.updateRow((row[0], row[0].weekday() + 1, row[0].strftime('%H'), ))

... change the 'NombreDeLaTabla' on the first line into the name of your table, open the Python Window:

... paste the code:

.. and hit Enter.

(Check and change the names of the fields 'Hora__GPS', 'Dia', 'Hora' if your schema is different.)

0 Kudos