3

Is there a function in Visual Basic 6 that can be used to convert a julian date string (99001-1st of Jan 1999) to a Date object?

I have tried using CDate() but the results are not as expected.

THanks.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Koekiebox
  • 5,793
  • 14
  • 53
  • 88
  • `99001` ?? How is the day and the month encoded into this - there's only one `1`? Did you mean '990101`? Which is the day and which the month? Do you want to be able to encode dates after 1999? I.e. are you concerned about [year 2000 problems](http://en.wikipedia.org/wiki/Year_2000_problem)? You may need to pick a year value below which you assume years are 19** and above which you assume years are above 20**. If you have any control over the format I would advise 4 digit years! – MarkJ Jan 18 '11 at 11:30
  • Year is 99 and day of year is 001. I guess the year can also be 2099. – Koekiebox Jan 18 '11 at 11:38

1 Answers1

4

I think the following would work, just put your julian date inside the jd variable.

Dim dt as Date 
Dim jd as Long
dt = DateSerial(1900 + Int(jd / 1000), 1, jd Mod 1000)
Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
  • It worked. The only thing is the 1900 should be changed to 2000. THanks. – Koekiebox Jan 18 '11 at 11:38
  • @MarkJ: I'll admit that it was a little while since I last looked at this code :) – Hans Olsson Jan 18 '11 at 11:44
  • The only thing is I need to check if the year is more than lets say 80 I make it 2000 and 1900 if its more I make it 1999. – Koekiebox Jan 18 '11 at 11:46
  • I'd replace Int(jd / 1000) with jd \ 1000 just to state it more clearly and avoid floating point intermediate values. – Bob77 Jan 19 '11 at 18:03
  • For posterity: DateSerial(year, month, day), this code sets month to 1 and lets the day wrap up. Year can be two digits, and MS will try to do the right thing. – Ron Jensen Feb 23 '17 at 18:42