0

Hey I am looking to create a Julian date format like YYDDD from the current date where the DDD would be the number of days since the year began.

Is there any .NET functions to do this easily ?

StevieB
  • 6,263
  • 38
  • 108
  • 193
  • possible duplicate of [Convert DateTime to Julian Date in C# (ToOADate Safe?)](http://stackoverflow.com/questions/5248827/convert-datetime-to-julian-date-in-c-sharp-tooadate-safe) – Ta01 Dec 20 '11 at 19:35

3 Answers3

2

Since, if you're working with Julian dates, you probably will need to do this again and again, I suggest writing an extension function for System.DateTime, which would execute something like the following:

return (DateTime.Year % 100) * 1000 + DateTime.DayOfYear

ETA: If what you want to do is convert a DateTime to the Julian date format (create a formatted string in Julian date format), I still suggest an extension function, but it would look like this:

public static string ToJulianDate(this DateTime date) 
{
    return string.Format("{0:00000}", (date.Year % 100) * 1000 + date.DayOfYear);
}
Ann L.
  • 13,760
  • 5
  • 35
  • 66
  • just wondering how I change to format YYDDD so days past since this year – StevieB Dec 21 '11 at 00:02
  • @StevieB Ah, so you're not asking how to convert a date to a number representing the Julian date, but how to format a System.DateTime as YYDDD (basically, convert it to a string in that format?) – Ann L. Dec 21 '11 at 14:52
0

No functions that I can think of, but maybe this would help:

Public Function Date2Julian(ByVal vDate As Date) As Long

    Date2Julian = CLng(Format(Year(vDate), "0000") _
                  + Format(DateDiff("d", CDate("01/01/" _
                  + Format(Year(vDate), "0000")), vDate) _
                  + 1, "000"))

End Function
Troy
  • 1,659
  • 4
  • 19
  • 33
0

Here are a couple that may help:

   public double GetJulianDate(DateTime pdtmDate)
 {
 DateTime dtmStart = new DateTime(1,1,1);
 TimeSpan objTS = new TimeSpan(pdtmDate.Ticks - dtmStart.Ticks);
 return objTS.TotalDays + 1721637;
 }


Public Function Date2Julian(ByVal vDate As Date) As Long

    Date2Julian = CLng(Format(Year(vDate), "0000") _
                  + Format(DateDiff("d", CDate("01/01/" _
                  + Format(Year(vDate), "0000")), vDate) _
                  + 1, "000"))

End Function
Ashok Padmanabhan
  • 2,110
  • 1
  • 19
  • 36