How can I convert Julian Date to Calendar Date

G

Guest

I'm trying to convert a Julian Date (Format "4365") into an actual calendar
date in Visual Basic, can anyone help me out with this.
 
T

Tom Dacon

I don't know what format 4365 is, but if you're talking about real Julian
dates (days and fractional days since noon Universal Time on January 1, 4713
BCE) I have some VB code that will do the two-way conversions between true
Julian and Gregorian calendars. I'd be happy to send it along to you if
that's what you're after.

Tom Dacon
Dacon Software Consulting
 
G

Guest

Tom:
The 4 in 4365 stands for the year, and the 365 is the day number of that
year in this example it would be December 30, of 2004. I was hoping there
would be a function in VB that handled it, I think you code may be useful or
if you know of a VB built in function that would be great too.
 
T

Tom Dacon

That date format sound something like what used to be called 'manufacturing
date' in aerospace. In that format your "4365" would be "04365". VB has no
built-in functionality for date formats like that; you'll have to roll your
own unless someone else has already done so. Here's a quick-and-dirty
example of one way to solve it:

' Receives a date as a string in format "YDDD", where Y is the last digit
' of the year, and DDD is the day number within the year;
' e.g., "4365" is December 31, 2004.
Public Function ToDateTime(ByVal theDate As String) As DateTime
' A DateTime as of the first day of the specified year
Dim dt as new DateTime(CInt("200" + theDate.Substring(0, 1)), 1, 1)
' Add the specified number of days and return the resulting DateTime
object.
return dt.AddDays(CInt(theDate.Substring(1, 3)))
End Function

HTH,
Tom Dacon
Dacon Software Consulting
 
T

Tom Dacon

Oops. There was a bug in the version I first posted, and the date came out
one day too high.
Try this instead:

' Receives a date as a string in format "YDDD", where Y is the last digit
' of the year, and DDD is the day number within the year; returns a
DateTime
' object with specified date. E.g., "4365" is December 31, 2004.
Public Function ToDateTime(ByVal theDate As String) As DateTime
' A DateTime as of the first day of the specified year
Dim dt as new DateTime(CInt("200" + theDate.Substring(0, 1)), 1, 1)
' Add the specified number of days and return the resulting DateTime
object.
return dt.AddDays(CInt(theDate.Substring(1, 3)) - 1)
End Function

This assumes leading zeroes in the day part of the date string. For
instance, January 1, 2004 would be "4001".

Sorry 'bout that.
Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top