Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

140
Views
Looking to convert date after doing a split

I am looking to convert a date from MM/DD/YY to YYYYMMDD

The code below gives me an output of 2211 which is incorrect. How can I do a check on Month and Day and add a 0 in front when needed?

var arr = '1/1/22';
arr = NTE.split('/');  //splits the date where it sees /
log.debug("Splitting date : "+ arr);
        
longDate=arr[2]+arr[0]+arr[1];   
log.debug("Long Date : "+ longDate);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You probably need something like this:

let arr = '1/1/22';
arr = arr.split('/');  //splits the date where it sees /
log.debug("Splitting date : "+ arr);

arr = arr.map((element) => element.length > 1 ? element : `0${element}`); // prepend with 0 if needed
const longDate=`20${arr[2]}${arr[0]}${arr[1]}` //prepend year with 20 to convert YY to YYYY
log.debug("Long Date : "+ longDate);
about 4 years ago · Juan Pablo Isaza Report

0

arr = '1/1/22';

arr = NTE.split('/');  //splits the date where it sees /
log.debug("Splitting date : "+ arr);
        

if(arr[0]<10){
    var arr0="0"+arr[0];
    log.debug("arr[0]: "+ arr[0]);
    log.debug("add zero to month: "+ arr0);
    var dayEdited = true;
} else {
    dayEdited = false;
}

if(arr[1]<10){
    var arr1="0"+arr[1];
    log.debug("arr[1]: "+ arr[1]);
    log.debug("add zero to day: "+ arr1);
    var monthEdited = true;
} else {
    monthEdited = false;
}


if(arr[2]<2000){
    var arr2="20"+arr[2];
    log.debug("add 20 to Year: "+ arr2);
    var yearEdited = true;      
} else {
    yearEdited = false;
}

if(yearEdited == true & monthEdited == true & dayEdited == true){
    var longerDate=arr2+arr0+arr1+"000000";    //rearrenges the array by YEAR,    MONTH, DAY
    log.debug("Longer Date : "+ longerDate);
} else if(yearEdited == false & monthEdited == true & dayEdited == true){
    var longerDate=arr[2]+arr0+arr1+"000000";    //rearrenges the array by YEAR, MONTH, DAY
    log.debug("Longer Date : "+ longerDate);
} else if(yearEdited == false & monthEdited == false & dayEdited == true){
    var longerDate=arr[2]+arr[0]+arr1+"000000";    //rearrenges the array by YEAR, MONTH, DAY
    log.debug("Longer Date : "+ longerDate);
} else if(yearEdited == false & monthEdited == true & dayEdited == false){
    var longerDate=arr[2]+arr0+arr[1]+"000000";    //rearrenges the array by YEAR, MONTH, DAY
    log.debug("Longer Date : "+ longerDate);    
} else if(yearEdited == true & monthEdited == false & dayEdited == false){
    var longerDate=arr2+arr[0]+arr[1]+"000000";    //rearrenges the array by YEAR, MONTH, DAY
    log.debug("Longer Date : "+ longerDate);
} else if(yearEdited == false & monthEdited == false & dayEdited == false){
    var longerDate=arr[2]+arr[0]+arr[1]+"000000";    //rearrenges the array by YEAR, MONTH, DAY
    log.debug("Longer Date : "+ longerDate);
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!