views:

154

answers:

1

I am trying to convert a julian date (yy/ddd) to a regualr mm/dd/yy data in excel vba. I have an idea of using a select case statement that calls a function for each month. But that is alot of code and I am lazy. I am wondering if there is a better way?

+1  A: 

According to this website:

=DATE(IF(0+(LEFT(A1,2))<30,2000,1900)+LEFT(A1,2),1,RIGHT(A1,3))

It it works as as long as you have a five-digit number in A1. For example, it turns 95032 into 2/1/1995. You'll need to adjust the formula if your data actually has a slash in it (95/032).

The same site has a vba section, as well. It says that the following will work:

Function JDateToDate(JDate As String) As Long
Dim TheYear As Integer
Dim TheDay As Integer
Dim TheDate As Long

TheYear = CInt(Left(JDate, 2))
If TheYear < 30 Then
    TheYear = TheYear + 2000
Else
    TheYear = TheYear + 1900
End If

TheDay = CInt(Right(JDate, 3))
TheDate = DateSerial(TheYear, 1, TheDay)
JDateToDate = TheDate

End Function

I didn't test the vba code, but since it is essentially a code version of the above formula, it should do the job.

Edit: As tbischel points out, this is not actually a Julian Date. It is an Ordinal Date.

Bill
This is not a Julian date, nor a modified Julian date... the range is incorrect in your test case. JD should be > 2400000 for anything recent. A modified julian date subtracts 2400000 from the result, but this number should be around 50000 for anything in the 1990s. http://www.csgnetwork.com/julianmodifdateconv.html
tbischel
I'm not sure why you're commenting on my answer instead of the original question. The OP used "Julian Date" instead of "Ordinal date", true, but I don't say anything about it being a Julian date.
Bill