Gregorian to Julian conversion

Are there any relatively easy ways to convert a given Gregorian date into a Julian date? I know there are system variables, but I need to convert a user-entered variable.

Thanks!

It feels like I just had this conversation, but that was moving a YYYYMMDD field to a DDMMYYYY field. To convert any date format to any other date format, the most reliable way is to use MOVE EDITED to convert the date to D format and then convert the D format date to the desired format. This is not efficient, but it is reliable.

Here is a bit of code:

DEFINE DATA LOCAL                                            
  1 #DATE-MDCY (A8)   INIT <'12312007'>                      
  1 #DATE-D    (D)                                           
  1 #DATE-J    (A7)                                          
END-DEFINE                                                   
*                                                            
IF #DATE-MDCY NE MASK (MMDDYYYY)                             
OR #DATE-MDCY NE MASK (....1582:2699)                        
   WRITE '#MDCY field is not in MMDDYYYY format.' #DATE-MDCY 
   STOP                                                      
END-IF                                                       
*                                                            
MOVE EDITED #DATE-MDCY TO #DATE-D (EM=MMDDYYYY)              
MOVE EDITED #DATE-D (EM=YYYYJJJ) TO #DATE-J                  
*                                                            
WRITE '=' #DATE-MDCY / '=' #DATE-D / '=' #DATE-J             
*                                                            
END

The MASK check is to prevent Nat 1143 errors on the first move edited statement. You can omit the second mask to check the year range if the compiler option MASKCME is turned on.

See this topic for a lengthy discussion regarding date conversion and efficiency.

Wow, this is awesome. I’m the only one in our group here who’s a programmer and I never know where to go to find help like this!!!

THANK YOU!!!

Next question; suppose my initial date is in the format: MM/DD/YY

How would this change the code you gave me? Am I correct that I would need to find a way to convert this first into the D format, and then go from there? If so, any tricks to getting that YY to easily turn into a YYYY? (I have the feeling not)

Also, once the date is in the D format, it sounds like I can get it turned into pretty much any format I like. Is that right?

Not a problem:

DEFINE DATA LOCAL
1 #DATE-MDCY (A8) INIT <‘123107’>
1 #DATE-D (D)
1 #DATE-J (A7)
END-DEFINE
*
IF #DATE-MDCY NE MASK (MMDDYY)
**OR #DATE-MDCY NE MASK (…1582:2699)
WRITE ‘#MDCY field is not in MMDDYYYY format.’ #DATE-MDCY
STOP
END-IF
*
MOVE EDITED #DATE-MDCY TO #DATE-D (EM=MMDDYY)
MOVE EDITED #DATE-D (EM=YYYYJJJ) TO #DATE-J
*
WRITE ‘=’ #DATE-MDCY / ‘=’ #DATE-D / ‘=’ #DATE-J
*
END

Yes, you are correct; once you have D format (which is just the number of days since day 1 year 0) you can do anything you want with it.

The YY will be a problem if you want to deal with 19 and 20 intermixed.

steve

Natural handles 2 digit years using a sliding window to determine the century. I think this can vary by installation, but here 28 evaluates to 1928 and 27 to 2027.

Also, if your input dates have slashes, just include them in the mask check and in the EM= clause.

IF #DATE-MDY = MASK (MM'/'DD'/'YY)
...
MOVE EDITED #DATE-MDY TO #D (EM=MM'/'DD'/'YY)

Don’t be afraid of writing test programs to see what Natural does.

Our shop has the sliding window turned off, so the century is based on current date and we get 20yy regardless of the value of yy.

The profile parameter is YSLW. For Jerome, YSLW=79, so he gets years 1928-1999 and 2000-2027 (2007 - 79 = 1928).

Thank you too guys, I had the same problem!
Marc