# Gregorian to Julian conversion

**URL:** <https://techcommunity.softwareag.com/t/gregorian-to-julian-conversion/75907>\
**Category:** Forum\
**Tags:** Adabas-Natural, Natural, Natural-Code-Samples, Natural-on-Windows-Unix\
**Created:** [December 20, 2007, 10:10pm UTC](https://techcommunity.softwareag.com/t/gregorian-to-julian-conversion/75907 "2007-12-20T22:10:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aasantavicca](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@aasantavicca](https://techcommunity.softwareag.com/u/aasantavicca)\
**Post date:** [December 20, 2007, 10:10pm UTC](https://techcommunity.softwareag.com/t/gregorian-to-julian-conversion/75907/1 "2007-12-20T22:10:17Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Jerome\_LeBlanc](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/jerome_leblanc/32/111_2.png) [@Jerome\_LeBlanc](https://techcommunity.softwareag.com/u/Jerome_LeBlanc)\
**Post date:** [December 20, 2007, 10:30pm UTC](https://techcommunity.softwareag.com/t/gregorian-to-julian-conversion/75907/2 "2007-12-20T22:30:21Z")

</div>

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:

```auto
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](http://tech.forums.softwareag.com/viewtopic.php?p=42766) for a lengthy discussion regarding date conversion and efficiency.

---

<div class="post-metadata">

**Author:** ![aasantavicca](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@aasantavicca](https://techcommunity.softwareag.com/u/aasantavicca)\
**Post date:** [December 21, 2007, 12:29am UTC](https://techcommunity.softwareag.com/t/gregorian-to-julian-conversion/75907/3 "2007-12-21T00:29:15Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Steve\_Robinson](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/steve_robinson/32/107_2.png) [@Steve\_Robinson](https://techcommunity.softwareag.com/u/Steve_Robinson)\
**Post date:** [December 21, 2007, 3:30am UTC](https://techcommunity.softwareag.com/t/gregorian-to-julian-conversion/75907/4 "2007-12-21T03:30:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jerome\_LeBlanc](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/jerome_leblanc/32/111_2.png) [@Jerome\_LeBlanc](https://techcommunity.softwareag.com/u/Jerome_LeBlanc)\
**Post date:** [December 21, 2007, 3:37am UTC](https://techcommunity.softwareag.com/t/gregorian-to-julian-conversion/75907/5 "2007-12-21T03:37:49Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![Ralph\_Zbrog](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/ralph_zbrog/32/78_2.png) [@Ralph\_Zbrog](https://techcommunity.softwareag.com/u/Ralph_Zbrog)\
**Post date:** [December 21, 2007, 4:13am UTC](https://techcommunity.softwareag.com/t/gregorian-to-julian-conversion/75907/6 "2007-12-21T04:13:08Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Andrea\_Lits](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Andrea\_Lits](https://techcommunity.softwareag.com/u/Andrea_Lits)\
**Post date:** [July 27, 2011, 2:11am UTC](https://techcommunity.softwareag.com/t/gregorian-to-julian-conversion/75907/7 "2011-07-27T02:11:30Z")

</div>

> [@aasantavicca](#):
>
> Wow, this is [awesome](http://www.starbike.com). 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?

Thank you too guys, I had the same problem!  
[Marc](http://www.racingbike24.com)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/techcommunity/original/3X/c/d/cd19967bb605d28a72d77eb83a1cae22f3df2eb8.svg) [@system](https://techcommunity.softwareag.com/u/system)\
**Post date:** [April 2, 2021, 2:49pm UTC](https://techcommunity.softwareag.com/t/gregorian-to-julian-conversion/75907/8 "2021-04-02T14:49:01Z")

</div>


