7

I have imported a shapefile that contains a serial date number that looks like this: 41256.260799. I would like to create a new field that shows only the hour and second like this: 06:33. I've tried the available VBS and Python date functions, but nothing seems to work.

Would I make my new field of type text?

  • It would help to know how the serial date was created? Is this from excel? – Jamie Jan 2 '13 at 16:35
  • @Gord, How are you getting 06:33 from 41256.260799. I? Do you have another example you could post? – artwork21 Jan 2 '13 at 17:11
  • @artwork21. My mistake, I meant to type hour and minute above... from 41256.260799 I'm getting 06:15:33. I want to create a field that will display just 06:15 as a label. – Gord Jan 2 '13 at 18:07
  • @artwork21 is based on the values that excel and other spreadsheets use. By default, January 1, 1900 is serial number 1. But becasue the OP only wants the hours and minutes they only need the fractional part. – mhoran_psprep Jan 2 '13 at 18:11
  • FIRSTLY, you say, "I've tried the available VBS and Python date functions". Please paste the code that you've tried. SECONDLY, please state what field type the serial field is (eg, float/double/text). THIRDLY, what type does your new field need to be (eg, time or text)? – Son of a Beach Jul 29 at 23:20
8

Eric’s Complete Guide To VT_DATE is a good guide if you are asking what I think you are asking.

From here, you can get the string you want fairly easily in Python using datetime:

import datetime
new_dt = datetime.datetime(1899, 12, 30) + datetime.timedelta(days=41256.260799)
formatted_string = new_dt.time().strftime("%H:%S")

That should be pretty easy to refactor into a function for the field calculator.

1

If you just need it for a label, use it in the label expression.

1

I worked through this same issue today. But we had to tell the Field Calculator to view the serial number as an integer not a string field.

Here the code :

datetime.datetime(1899,12,30) + datetime.timedelta(days = int( !START_DATE!))

0

Here is a python snippet that might do the job using the field calculator function. Note that this assumes that the serial is a float. If the serial is text, then you'd need to add something like serial = float(serial) at the start of the serialToTime() function in the block.

import arcpy, datetime

block = """
def serialToTime(serial):
    serialDateTime = datetime.datetime(1970, 1, 1) + datetime.timedelta(days=serial)
    return serialDateTime.strftime("%H:%M")
"""

arcpy.CalculateField_management(inTable, timeFieldName, "serialToTime(!serialFieldName!)", "PYTHON", block)

NB: This is untested!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.