Convert Date: Need code

dvelloriy
Kilo Sage

Hello all,

 

I need to convert today’s date to Julian date. 
for ex: 13 sep 2024, in julian date it would be 24257.

then i need to append 0001 as suffix to this julian number.

final output = 242570001.

 

is it possible to run a loop and increment this number by 1 based on a count value provided in one of the catalog variables.

 

i am creating a flow. In the flow input would be the count variable from cat item. Then the script logic will calculate the numbers above in an array and then store them in an output variable on cat item.

 

 

 

please advise

8 REPLIES 8

Mani A
Tera Guru

BOT sample code and make changes as per your use case

once you get JulianDate format 

 

  var count = parseInt(current.variables.count_variable, 10); // Get count from catalog variable

    var incrementedValue = incrementJulianNumber(count);

  gs.print('value'+ incrementedValue);

    function incrementJulianNumber(count) {

        var baseJulian = getJulianDate(); // 24257

        var suffix = '0001';

        var baseNumber = parseInt(baseJulian + suffix, 10);

        return baseNumber + count;

    }

 

This does not work and throwing invalid object error. Chat gpt codes usually dont work. 

Sanjay191
Tera Sage

Please Refer the below code and append your suffix

 

function convertToJulianDate(year, month, day) {
   
    if (month <= 2) {
        year -= 1;
        month += 12;
    }


    var A = Math.floor(year / 100);
    var B = 2 - A + Math.floor(A / 4);

   
    var JD = Math.floor(365.25 * (year + 4716)) +
             Math.floor(30.6001 * (month + 1)) +
             day + B - 1524.5;
   
    return JD;
}

var gdt = new GlideDateTime();
var year = gdt.getYearLocalTime();
var month = gdt.getMonthLocalTime() + 1;
var day = gdt.getDayOfMonthLocalTime();

var julianDate = convertToJulianDate(year, month, day);
gs.info("Julian Date: " + julianDate);

dvelloriy
Kilo Sage

Hi Sanjay, this is not returning correct value.

when i ran this in background scripts it returned

Julian Date : 2460596.5

I am expecting 24257 for today's date.