MainframeMaster

Date Conversions

Date conversion in DFSORT means changing a date field from one format to another—for example, from MMDDYYYY to YYYYMMDD, or from YYDDD (Julian) to YYYYMMDD (Gregorian). You can do this in INREC or OUTREC using format codes (Y2T, Y2W, Y4T, Y4W) and conversion operators (TOGREG, TOJUL). Doing the conversion in INREC is important when you need to sort by the converted date or filter on it with INCLUDE/OMIT—because INREC runs before the sort, the sort sees the new date. This page explains the format codes (year first vs year last, 2-digit vs 4-digit year), how to use TOGREG and TOJUL, building a sortable YYYYMMDD field in INREC, and century windowing for 2-digit years. The same syntax applies in OUTREC when you only need the converted date in the final output.

INREC Processing
Progress0 of 0 lessons

Why Convert Dates in INREC?

Dates on the mainframe appear in many forms: YYYYMMDD (8 bytes), YYYYDDD (Julian, 7 bytes), MMDDYYYY, DDMMYYYY, or YYMMDD with 2-digit year. To sort or filter by date, you usually want a single, consistent format—and often one where character order equals date order, such as YYYYMMDD. If the input has MMDDYYYY at positions 50–57, the character order of that string does not match chronological order (e.g. 12311999 vs 01012000). So you convert that field to YYYYMMDD and put it at a known position (e.g. at the start of the INREC output). Then SORT FIELDS=(1,8,CH,A) sorts by date correctly, and INCLUDE COND=(1,8,CH,GE,C'20230101') filters correctly. Because INREC runs before the sort, the converted date is in the record that the sort and INCLUDE/OMIT see. If you only converted in OUTREC, the sort would still see the original format.

Source Date Format Codes

DFSORT uses four main format codes to describe the source date field. Each code indicates whether the year is 2-digit or 4-digit and whether the year comes first or last.

Date format codes
CodeMeaningExample
Y2T2-digit year first: yymmdd or yyddd251015 = 15 Oct 2025 (if window 1940–2039)
Y2W2-digit year last: mmddyy or dddyy101525 = 15 Oct 2025
Y4T4-digit year first: ccyymmdd or ccyyddd20251015
Y4W4-digit year last: mmddccyy or dddccyy10152025

Y2 = 2-digit year (yy); Y4 = 4-digit year (ccyy). T = year first (e.g. yymmdd, ccyymmdd, yyddd, ccyyddd). W = year last (e.g. mmddyy, mmddccyy, dddyy, dddccyy). You must specify the format that matches your input data so the conversion interprets the bytes correctly.

Conversion Operators: TOGREG and TOJUL

After you specify the source field and its format, you use a conversion operator to say what format you want for the output.

  • TOGREG=YnX — Convert to Gregorian date. YnX is the target format: e.g. TOGREG=Y4T gives ccyymmdd (YYYYMMDD), no separators. TOGREG=Y4T(s) or similar may add separators (e.g. hyphen) depending on the product.
  • TOJUL=YnX — Convert to Julian date (year and day-of-year). e.g. TOJUL=Y4T gives ccyyddd (YYYYDDD).

So you might have: (81,8,Y4W,TOGREG=Y4T) — take 8 bytes at position 81, interpret as Y4W (mmddccyy), convert to Gregorian Y4T (ccyymmdd), and place the result in the output at the position specified (often with an output position prefix like 1: or 21:). Exact syntax can vary; see your DFSORT manual for BUILD vs FIELDS and placement.

INREC Example: Convert MMDDYYYY to YYYYMMDD for Sort

Input has: bytes 1–80 data, bytes 81–88 date in MMDDYYYY (e.g. 12252024). You want to sort by date. Convert in INREC and put the YYYYMMDD field at the start of the reformatted record:

text
1
2
INREC BUILD=(1:81,8,Y4W,TOGREG=Y4T,9:1,80) SORT FIELDS=(1,8,CH,A)

Here, 1:81,8,Y4W,TOGREG=Y4T means: take the 8-byte field at input position 81, interpret it as Y4W (mmddccyy), convert to Y4T (ccyymmdd), and place the result starting at output position 1. Then 9:1,80 copies input 1–80 to output positions 9–88. So the reformatted record has the 8-byte YYYYMMDD at 1–8 and the original 80 bytes at 9–88. SORT FIELDS=(1,8,CH,A) then sorts by that date. (Note: output-position syntax like 1: and 9: may vary by DFSORT version; refer to your product manual.)

Julian to Gregorian

If the source is Julian (e.g. YYDDD or YYYYDDD), use the matching source format (Y2T for yyddd, Y4T for ccyyddd) and convert to Gregorian with TOGREG=Y4T (or Y2T for 2-digit year output). Example: input has 7-byte YYYYDDD at position 50. Convert to 8-byte YYYYMMDD:

text
1
INREC BUILD=(1:50,7,Y4T,TOGREG=Y4T,9:1,49,58:51,30)

Conceptually: source 50,7 as Y4T (ccyyddd), TOGREG=Y4T places 8-byte ccyymmdd at output 1–8; then copy input 1–49 to output 9–57, and input 51–80 to output 58–87. Again, exact placement syntax depends on your DFSORT version; the idea is to build the converted date where the sort can use it.

Century Windowing for 2-Digit Years

When the source date uses a 2-digit year (Y2T or Y2W), DFSORT applies a century window to decide whether yy means 19xx or 20xx. The default window (e.g. 1940–2039) means 40 → 1940, 99 → 1999, 00 → 2000, 39 → 2039. So 25 is interpreted as 2025. If your data has years outside that window (e.g. 1935 or 2045), the converted date can get the wrong century. Whenever possible, use 4-digit year source (Y4T, Y4W) so there is no ambiguity. If you must use 2-digit years, check your DFSORT documentation for options to set or change the century window.

Same Syntax in OUTREC

The same date conversion operands (source format and TOGREG/TOJUL) are supported in OUTREC. Use OUTREC when you do not need to sort or filter by the converted date—you only want it in the final output. For example, you might sort by an existing key and then in OUTREC convert a date field to a display format (e.g. with separators) for a report. INREC is for when the converted date must participate in the sort or in INCLUDE/OMIT.

Building a Sortable Date Without Conversion

If the input has separate fields (e.g. month at 20–21, day at 22–23, year at 24–27), you can sometimes build YYYYMMDD manually with INREC by copying and reordering: e.g. year, month, day in that order. That is just field extraction and reordering (and possibly padding with zeros). When the input is already in a single field but in the wrong format (e.g. MMDDYYYY), use the date conversion (Y4W, TOGREG=Y4T) as above. When the input is in pieces, building with (position,length) may be simpler than conversion.

Explain It Like I'm Five

Imagine the date is written in different ways on different cards: one card says "12-25-2024" (month-day-year), another says "20241225" (year-month-day). To put the cards in order by date, we need them all in the same form—like year first, so when we sort the numbers, the oldest date comes first. Date conversion is when the computer changes the date from one form to another. We do it before sorting (in INREC) so that when the sort looks at the card, it already sees the date in the right form. So the computer rewrites the date into the format we want, then we sort by it.

Exercises

  1. Input has an 8-byte date at position 40 in MMDDYYYY format. You want to sort by date. Do you convert in INREC or OUTREC? Why?
  2. What does Y4W mean for the source field? Give an example of an 8-byte value in that format.
  3. What is the risk of using Y2T (2-digit year) for dates that might be in the 1900s and the 2000s?
  4. Write a short INREC (conceptual) that converts a 7-byte YYYYDDD field at input position 60 to YYYYMMDD and places it at output position 1, then copies the rest of the record. (Use your manual for exact syntax.)

Quiz

Test Your Knowledge

1. Why convert a date in INREC instead of OUTREC when you need to sort by it?

  • OUTREC cannot convert dates
  • INREC runs before the sort, so the sort sees the converted date and can use it in SORT FIELDS
  • OUTREC is only for reports
  • Date conversion only works in INREC

2. What does Y4T mean in DFSORT date conversion?

  • 4-digit year, year last (e.g. MMDDYYYY)
  • 4-digit year, year first (e.g. YYYYMMDD or YYYYDDD)
  • Julian only
  • 2-digit year

3. What is TOGREG=Y4T in a BUILD specification?

  • Sort by Gregorian date
  • Convert the source date field to Gregorian format with 4-digit year first (e.g. YYYYMMDD)
  • Filter by date
  • Insert current date

4. What is the century window (Y2T) problem?

  • Y2T cannot be used
  • 2-digit years (e.g. 99 = 1999 or 2099?) depend on a default window; dates outside that window may get the wrong century
  • Only Y4T has a window
  • There is no problem

5. Can you use the same date conversion syntax in OUTREC?

  • No
  • Yes—the same format codes (Y2T, Y4T, etc.) and TOGREG/TOJUL work in OUTREC BUILD or FIELDS
  • Only TOGREG
  • Only for current date