Tapatalk

Julian Date to Gregorian date conversion script

Julian Date to Gregorian date conversion script

4
NewbieNewbie
4

    Jan 02, 2009#1

    Hello everyone and happy new year !!!
    Would be possible to convert a list of julian dates to normal calendar dates by use of script ?
    It can be on the same file or different file..

    File in (yy/jjj)
    09001
    09002
    09003
    09004
    09005
    ...

    File Out (mm/dd/yy)
    01/01/09
    01/02/09
    01/03/09
    01/04/09
    01/05/09
    ...

    6,685587
    Grand MasterGrand Master
    6,685587

      Jan 02, 2009#2

      Sure, should be no problem. Julian Day Converter is a webpage with JavaScript code to convert a Julian date to Gregorian calendar date. Use View - Source in your browser to see the source code of the JavaScript function which converts the Julian date to Gregorian date. You can use this function slightly adapted with some additional UltraEdit script commands to make the conversion for a list of dates.

      By the way: Is your example correct?

      Note: I have no experience with Julian date.
      Best regards from an UC/UE/UES for Windows user from Austria

      4
      NewbieNewbie
      4

        Jan 03, 2009#3

        Hello Mofi, thanks for the reply... I've checked the website refered and no lucky checking the code... also I'm not one of the best person in the world to adapt a javascript code... :roll:

        Could someone point me to javascript code that I can use to convert those records ?
        When 01/01 is the first julian date and 12/31 is last one on the current year.

        6,685587
        Grand MasterGrand Master
        6,685587

          Jan 03, 2009#4

          I thought that already. But I doubt that your Julian date strings are really Julian date values because when I use the converter on the referenced page I get for Gregorian 01/01/2009 the Julian date value 2454833.

          So I think you only want to change the display format of Gregorian dates which can be done without a script or macro by just using a regular expression replace with tagged expression as demonstrated in the IDM power tip Tagged expressions.

          Or do you mean with yy/jjj that yy is the short value for the year as 09 for 2009 and jjj is the day in the year as 260 for 17th September?
          Best regards from an UC/UE/UES for Windows user from Austria

          4
          NewbieNewbie
          4

            Jan 03, 2009#5

            You are correct, 2009/09/17 is day 260 of year 2009.
            I need to convert a list of dates like these :

            input:
            09005
            09260
            08170

            output :
            01/05/2009
            09/17/2009
            06/18/2008

            6,685587
            Grand MasterGrand Master
            6,685587

              Jan 03, 2009#6

              Okay, here we are. I'm also not a JavaScript expert, but this script worked on your example.

              Code: Select all

              if (UltraEdit.document.length > 0) {  // Is any file opened?
              
                 var sDate;     // String variable for the date.
                 var sDay;      // String variable for the day.
                 var iDay;      // Integer variable for the day.
                 var sMonth;    // String variable for the month.
                 var iMonth;    // Integer variable for the month.
                 var sYear;     // String variable for the year.
                 var iYear;     // Integer variable for the year.
                 var bValid;    // false ... invalid date string
                 var iLeapYear; // 0 ... no leap year, 1 ... leap year
                 DaysOfMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
              
                 // Set working environment for the script.
                 UltraEdit.insertMode();
                 if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
                 else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
                 UltraEdit.activeDocument.hexOff();
                 UltraEdit.ueReOn();
              
                 /* Verify the line termination of the last line of the file
                    and insert one if the last line has no line termination. */
                 UltraEdit.activeDocument.bottom();
                 if (UltraEdit.activeDocument.isColNumGt(1)) {
                    UltraEdit.activeDocument.insertLine();
                    if (UltraEdit.activeDocument.isColNumGt(1)) {
                       UltraEdit.activeDocument.deleteToStartOfLine();
                    }
                 }
                 UltraEdit.activeDocument.top();
              
                 // Run the following loop until end of file is reached.
                 while (!UltraEdit.activeDocument.isEof()) {
              
                    // Get next date into string variable.
                    UltraEdit.activeDocument.selectWord();
                    sDate = UltraEdit.activeDocument.selection;
                    bValid = false;
                    if (sDate.length == 5) {  // Date string must have a length of 5 characters.
              
                       // Get year, test for valid number and add century.
                       // The first 2 characters are for the decimal year.
                       sYear = sDate.substr(0,2);
                       iYear = parseInt(sYear,10);
                       if (!isNaN(iYear)) {
              
                          // The year number is valid, add century.
                          iYear += (iYear >= 70) ? 1900 : 2000;
                          sYear = String(iYear);
              
                          // Is the year a leap year with 366 days?
                          if (((iYear % 400) == 0) || (((iYear % 100) != 0) && ((iYear % 4) == 0))) {
                             iLeapYear = 1;
                             DaysOfMonth[1] = 29;
                          } else {
                             iLeapYear = 0;
                             DaysOfMonth[1] = 28;
                          }
              
                          // Get day and test for valid number.
                          sDay = sDate.substr(2,3);
                          iDay = parseInt(sDay,10);
                          if (!isNaN(iDay)) {
              
                          // The day number is valid, test on value.
                             if ((iDay != 0) && (iDay <= (365+iLeapYear))) {
              
                                // String in the file is valid date string.
                                bValid = true;
                                // Get month for this day.
                                for (iMonth = 0; iDay > DaysOfMonth[iMonth]; iMonth++) {
                                   iDay -= DaysOfMonth[iMonth];
                                }
                                iMonth++;
                                // Convert day and month numbers into strings.
                                if( iMonth < 10 ) sMonth = "0" + String(iMonth);
                                else sMonth = String(iMonth);
                                if( iDay < 10 ) sDay = "0" + String(iDay);
                                else sDay = String(iDay);
                             }
                             else UltraEdit.outputWindow.write(sDate+" contains invalid day value!");
                          }
                          else UltraEdit.outputWindow.write(sDate+" contains invalid day string!");
                       }
                       else UltraEdit.outputWindow.write(sDate+" contains invalid year string!");
                    }
                    else if (sDate.length != 0) UltraEdit.outputWindow.write(sDate+" is an invalid date string!");
                    // Replace the existing date string if it was valid.
                    if (bValid) UltraEdit.activeDocument.write(sMonth+"/"+sDay+"/"+sYear);
                    else UltraEdit.activeDocument.endSelect();
                    // Set cursor to start of the next line.
                    UltraEdit.activeDocument.key("HOME");
                    UltraEdit.activeDocument.key("DOWN ARROW");
                 }  // End of while !eof loop.
                 UltraEdit.activeDocument.top();
              }

              4
              NewbieNewbie
              4

                Jan 04, 2009#7

                It worked perfectly! Thank you very much Mofi!