28
Views
3
Comments
Solved
Could not convert string to DateTime
Question
Application Type
Reactive

Hello,

I am calling an API thought that I am getting date format like this : 20240618153915.341Z.  when I am trying to parse in attribute of datetime its giving error "Could not convert string to DateTime".

2022-03-10 08-26-10
Cristian Angel Puma Villalva
Solution

HI

Could you do it with JavaScript and format the date 
hen use the 'Convert Text to Date' function." 

// Assume inputString is the string received from the API

https://www.outsystems.com/forums/discussion/89804/i-need-to-convert-the-format-of-date-from-text-to-date-and-time/ 
https://www.outsystems.com/forums/discussion/95422/text-to-datetime-conversion/ 


inputString = "20240618153915.341Z";


// Extract components

year = inputString.substring(0, 4);

month = inputString.substring(4, 6);

day = inputString.substring(6, 8);

hour = inputString.substring(8, 10);

minute = inputString.substring(10, 12);

second = inputString.substring(12, 14);

millisecond = inputString.substring(15, 18); // Extracting milliseconds separately


// Reformat to ISO 8601

reformattedString = year + "-" + month + "-" + day + "T" + hour + ":" + minute + ":" + second + "." + millisecond + "Z";


2022-03-10 08-26-10
Cristian Angel Puma Villalva
Solution

HI

Could you do it with JavaScript and format the date 
hen use the 'Convert Text to Date' function." 

// Assume inputString is the string received from the API

https://www.outsystems.com/forums/discussion/89804/i-need-to-convert-the-format-of-date-from-text-to-date-and-time/ 
https://www.outsystems.com/forums/discussion/95422/text-to-datetime-conversion/ 


inputString = "20240618153915.341Z";


// Extract components

year = inputString.substring(0, 4);

month = inputString.substring(4, 6);

day = inputString.substring(6, 8);

hour = inputString.substring(8, 10);

minute = inputString.substring(10, 12);

second = inputString.substring(12, 14);

millisecond = inputString.substring(15, 18); // Extracting milliseconds separately


// Reformat to ISO 8601

reformattedString = year + "-" + month + "-" + day + "T" + hour + ":" + minute + ":" + second + "." + millisecond + "Z";


2025-10-10 00-19-44
Bruno Rendeiro

Hello @Rashpal Singh 


Here is a javascript function that you can use,


function formatTimestamp(timestamp) {

    // Parse the timestamp to a Date object

    let date = new Date(timestamp);


    // Extract date components

    let year = date.getUTCFullYear();

    let month = String(date.getUTCMonth() + 1).padStart(2, '0'); // Months are zero-indexed

    let day = String(date.getUTCDate()).padStart(2, '0');

    

    // Extract time components

    let hours = String(date.getUTCHours()).padStart(2, '0');

    let minutes = String(date.getUTCMinutes()).padStart(2, '0');

    let seconds = String(date.getUTCSeconds()).padStart(2, '0');

    let milliseconds = String(date.getUTCMilliseconds()).padStart(3, '0');


    // Construct the formatted datetime string

    let formattedDateTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${milliseconds}`;


    return formattedDateTime;

}


// Example usage

let timestamp = "20240618153915.341Z";

console.log(formatTimestamp(timestamp)); // Outputs: "2024-06-18 15:39:15.341"


Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.