date difference in cobol in javascript

To calculate date difference in COBOL you can use the intrinsic function FUNCTION INTEGER-OF-DATE. The function returns a four-digit integer representing a date in the format of YYYYMMDD. You can subtract the two dates and convert the result back to days using the FUNCTION INTEGER-OF-INTEGER.

Here is an example COBOL program to calculate date difference:

index.tsx
IDENTIFICATION DIVISION.
PROGRAM-ID. DATE-DIFFERENCE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 DATE-1.
   05 YY-1   PIC XX.
   05 MM-1   PIC XX.
   05 DD-1   PIC XX.
01 DATE-2.
   05 YY-2   PIC XX.
   05 MM-2   PIC XX.
   05 DD-2   PIC XX.
01 DATE-1-INTEGER PIC 9(8).
01 DATE-2-INTEGER PIC 9(8).
01 DIFFERENCE PIC 9(4).
PROCEDURE DIVISION.
   MOVE '20210305' TO DATE-1.     // YYYYMMDD
   MOVE '20210215' TO DATE-2.     // YYYYMMDD
   COMPUTE DATE-1-INTEGER = FUNCTION INTEGER-OF-DATE(DATE-1)
   COMPUTE DATE-2-INTEGER = FUNCTION INTEGER-OF-DATE(DATE-2)
   COMPUTE DIFFERENCE = FUNCTION INTEGER-OF-INTEGER(DATE-1-INTEGER - DATE-2-INTEGER)
   DISPLAY 'Date Difference: ' DIFFERENCE ' days'
   STOP RUN.
704 chars
24 lines

To calculate date difference in JavaScript, you can use a popular library, moment.js. Here is an example JavaScript program to calculate date difference using moment.js:

index.tsx
const date1 = moment('2021-03-05')
const date2 = moment('2021-02-15')
const difference = date1.diff(date2, 'days')
console.log(`Date Difference: ${difference} days`)
166 chars
5 lines

This program will output "Date Difference: 18 days".

gistlibby LogSnag