converting a strange date/time value into a recognisable date-time format

plusunim

New Member
Joined
Aug 17, 2014
Messages
15
Hi all. I have a column (say col A) that has values in the format yyyy-mm-ddThh_mm_ss. I decided to create the function below to convert them to dd/mm/yyyy hh:mm:ss in column B. However I'm getting a #Name? error. I'm not exactly a VBA proficient and I would like your opinion about where it is going wrong.

Code:
Function DateTime(dtval As Range) As Date

    'Converts the filenames with format yyyy-mm-ddThh_mm_ss into dd/mm/yyyy hh:mm:ss
    Dim dayfn, monthfn, yearfn, hourfn, minutefn, secondfn As String
    
    If dtval Is blank Then
        Exit Function
    Else
        yearfn = Left(dtval, 4)
        monthfn = Mid(dtval, 6, 2)
        dayfn = Mid(dtval, 9, 2)
        
        hourfn = Mid(dtval, 12, 2)
        minutefn = Mid(dtval, 15, 2)
        secondfn = Mid(dtval, 16, 2)
        
        DateTime = Format("dayfn/monthfn/yearfn hourfn:minutefn:secondfn", "dd/mm/yyyy hh:mm:ss")
    End If

End Function

Thanks!
 
Last edited:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Assuming the upper case T is physically in the text, your function does not have to be as complex as you are assuming, this one-liner should work fine for you (of course, you will have to format the cell the function is in with the date/time display of your choice)...
Code:
Function DateTime(S As String) As Date
  DateTime = Replace(Replace(S, "T", " "), "_", ":")
End Function
 
Upvote 0
Assuming the upper case T is physically in the text, your function does not have to be as complex as you are assuming, this one-liner should work fine for you (of course, you will have to format the cell the function is in with the date/time display of your choice)...
Code:
Function DateTime(S As String) As Date
  DateTime = Replace(Replace(S, "T", " "), "_", ":")
End Function

Thanks for the reply. Yes, 'T' is in the filename to differentiate the date and time. The filename is generated by a commercial software and there is no way to go around it. I get your idea. I am using the following code but am still getting a #Name? error.

Code:
Function DateTime(S As String) As Date
  DateTime = Format(Replace(Replace(Replace(S, "T", " "), "_", ":"), "-", "/"), "dd/mm/yyyy hh:mm:ss")
End Function

Thanks.
 
Upvote 0
Thanks for the reply. Yes, 'T' is in the filename to differentiate the date and time. The filename is generated by a commercial software and there is no way to go around it. I get your idea. I am using the following code but am still getting a #Name? error.

Code:
Function DateTime(S As String) As Date
  DateTime = Format(Replace(Replace(Replace(S, "T", " "), "_", ":"), "-", "/"), "dd/mm/yyyy hh:mm:ss")
End Function
A UDF can only return a value to the cell it is in, it cannot do anything else (such as changing the format for the cell). Go back to my original UDF, put the formula using it in the cell, change the cells format, then you can copy that down (or across) as needed and the cell format will be placed in each cell it is copied to.
 
Upvote 0
[SOLVED] Re: converting a strange date/time value into a recognisable date-time format

A UDF can only return a value to the cell it is in, it cannot do anything else (such as changing the format for the cell). Go back to my original UDF, put the formula using it in the cell, change the cells format, then you can copy that down (or across) as needed and the cell format will be placed in each cell it is copied to.

Thanks! I was still getting a #Name? error until I changed that to Date_Time...

Works wonders!

Thanks!
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,688
Members
449,117
Latest member
Aaagu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top