1

I got a simple Julian Date Calculator with the following code:

DateTime date = DateTime.UtcNow;
int month = date.Month > 2 ? date.Month : date.Month + 12;
int year = month > 2 ? date.Year : date.Year - 1;
int hour = date.Hour;
int minute = date.Minute;
int second = date.Second;
int millisecond = date.Millisecond;
double day = date.Day + hour / 24.0 + minute / 1440.0 + (second + millisecond * 1000) / 86400.0;
int isJulianCalendar = isJulianDate(year, month, date.Day) ? 0 : 2 - year + year / 100 / 4;

When I run the program, it returns a lower value than the previous one (e.g if I run now, it shows a value, but if I run in a couple of minutes, it shows another value).

From the .pdf I copied the expression, it says that the formula use UT time. Is there any relevant difference from the UTC time?

Vlocks
  • 13
  • 3
  • This calculation looks fundamentally flawed to me, doing work that isn't necessary, and not doing work that is necessary. Where did you find it? –  Aug 03 '15 at 05:57
  • Well, I got some parts from this question: http://stackoverflow.com/questions/5248827/convert-datetime-to-julian-date-in-c-sharp-tooadate-safe and some other parts from this .pdf: http://www.nrel.gov/docs/fy08osti/34302.pdf (page 9).... It seems good for me, but I don't have such knowledge in this area – Vlocks Aug 03 '15 at 19:49
  • I'm having trouble seeing how you get from either of those links to the code you put in your question. At any rate, any calculation that involves hours, minutes, seconds and milliseconds will be pretty likely to give different values when you run it at a different time of the day. Sorry, I'm having a lot of trouble making sense of your question. –  Aug 03 '15 at 20:12
  • Sorry, I will try to better explain: In the .pdf, to calculate the day it says `is the day of the month with decimal time` Since I was confused with that, I copied this code from the other question. That's why I got one part from both links. – Vlocks Aug 03 '15 at 20:23
  • But if you include the time, then how could you not get different values when you run it at different times? The fact that you're getting different values seems perfectly normal to me. –  Aug 03 '15 at 20:25
  • And I know that, how it is a time based code, it should constantly change, but not backwards (it shows a lower value each run, instead of a bigger one). – Vlocks Aug 03 '15 at 20:25
  • Oh wait, your numbers seem off... Your code is incomplete, not accounting for `year` and `month` in your calculation of `day`. The problem you describe is nonetheless in the code you put in the question. Will answer that part, but please keep in mind that it won't be complete even if you fix that. –  Aug 03 '15 at 20:29

2 Answers2

1

.NET has a built in JulianCalendar class, which you should probably use instead of writing your own code.

0
double day = date.Day + hour / 24.0 + minute / 1440.0 + (second + millisecond * 1000) / 86400.0;

The (second + millisecond * 1000) part seems intended to calculate fractional seconds, but to get that, you need to divide millisecond by 1000.0, not multiply it.

Note that as I pointed out in the comments, this only addresses the immediate problem you are asking about, it will likely not be sufficient to actually correctly calculate the Julian day. However, since you yourself have posted links to working answers showing a calculation of the Julian day without time, you should be able to get it working from here on.

  • This!! Thank you, that was the problem. I also made a mistake in the `int isJulianCalendar = isJulianDate(year, month, date.Day) ? 0 : 2 - year + year / 100 / 4` ... I should get the int part of it, instead of the double value. Thank you again – Vlocks Aug 03 '15 at 20:42