Javascript Date
Hi
function ApiFormatter (Fecha)
{
// Split the input date string by '/'
const [month, day, year] = Fecha.split('/');
// Create a new Date object with the parsed values
const date = new Date(year, month - 1, day); // Note: month is 0-based in JavaScript Date object
// Format the Date object to the desired string format
const formattedDate = date.toISOString().split('T')[0] + "T00:00:00";
return formattedDate;
}
I have this method to give a specific format to the dates but it gives me an error in - const [month, day, year] = Fecha.split('/'); - how can I modify it so that it stops giving an error?
-
Hello Christian,
Sorry for the delayed response.
Change your code to this, there was an issue with the syntax.
function apiFormatter(fecha) {
debugger;
const splitDate = fecha.split('/');
const month = splitDate[0];
const day = splitDate[1];
const year = splitDate[2];
// Create a new Date object with the parsed values
const date = new Date(year, month - 1, day); // Note: month is 0-based in JavaScript Date object
// Format the Date object to the desired string format
const formattedDate = date.toISOString().split('T')[0] + "T00:00:00";
return formattedDate
}Thanks,
Jason Wells
Please sign in to leave a comment.
Comments
2 comments