document.addEventListener("DOMContentLoaded", function () {
const utcDateElements = document.querySelectorAll('.date-utc');
console.log("utcDateElements",utcDateElements);
utcDateElements.forEach(function (element) {
const utcDateString = element.getAttribute('data-utc-date');
console.log("utcDateString",utcDateString);
const dateParts = utcDateString.split('-');
console.log("dateParts",dateParts);
const utcDate = new Date(`${dateParts[1]}-${dateParts[0]}-${dateParts[2]}`);
console.log("utcDate",utcDate);
element.textContent = utcDate.toLocaleString();
});
});
that code workes fine on local but on staging give me invaild date text
I have to sent datetime from server that is in format
05-01-2024 12:03:54
Loading

Anandu G NathPosted Jan 6, 2024, 10:22 AM
Check this code
Amit MohantyPosted Jan 5, 2024, 12:31 PM
Try this:
See this js<>fiddle: https://jsfiddle.net/jdcykgzw/
Simran VermaPosted Jan 5, 2024, 11:19 AM
what if when sever sent the date in 1/25/2024 12:03:54 PM, this format
document.addEventListener("DOMContentLoaded", function () {
const utcDateElements = document.querySelectorAll('.date-utc');
console.log("utcDateElements", utcDateElements);
utcDateElements.forEach(function (element) {
const utcDateString = element.getAttribute('data-utc-date');
console.log("utcDateString", utcDateString);
const dateTimeParts = utcDateString.split(' ');
const dateParts = dateTimeParts[0].split('-');
const timeParts = dateTimeParts[1].split(':');
const utcDate = new Date(
parseInt(dateParts[2]),
parseInt(dateParts[1]) - 1,
parseInt(dateParts[0]),
parseInt(timeParts[0]),
parseInt(timeParts[1]),
parseInt(timeParts[2])
);
console.log("utcDate", utcDate);
element.textContent = utcDate.toLocaleString();
});
});
this method give me invaild
i want to method that is independet for all
Naimish MakwanaPosted Jan 5, 2024, 10:30 AM
The issue you’re facing might be due to the date format you’re using. JavaScript’s
Dateobject can be inconsistent when parsing date strings, especially when the date string format is not standard12.In your case, the date string format
05-01-2024 12:03:54seems to beDD-MM-YYYY HH:MM:SS. However, JavaScript’sDateobject expects the date string to be in a format that it can parse correctly. The safest format to use is the ISO 8601 format:YYYY-MM-DDTHH:MM:SSZ13.Here’s how you can modify your code to handle this:
In this code, I’ve modified the date string to be in the ISO 8601 format before passing it to the
Dateconstructor. This should help ensure that the date string is parsed correctly across different environments13.Please note that this code assumes that the date and time are in UTC. If they’re not, you’ll need to adjust the code accordingly13.
If you’re still facing issues, it would be helpful to check if the date string is valid before trying to convert it. You can use
isNaN(Date.parse(yourDateString))to check if the date string is valid2. If this returnstrue, then the date string is invalid2.Thanks
Jayraj ChhayaPosted Jan 5, 2024, 9:06 AM
The "invalid date" error typically occurs when the JavaScript
Dateconstructor is unable to parse the provided date string. In your case, the datetime format received from the server is "05-01-2024 12:03:54".To resolve the issue, you need to modify the code to handle this datetime format correctly. One way to achieve this is by splitting the datetime string into separate date and time parts, and then constructing the
Dateobject using the appropriate parts.Here's an updated version of your code that handles the datetime format "05-01-2024 12:03:54":
In this updated code, we first split the datetime string into separate date and time parts using the space character as the delimiter. Then, we further split the date and time parts into their respective components (day, month, year, hour, minute, second). Finally, we construct the
Dateobject using the parsed components.By making these modifications, the code should now be able to handle the datetime format "05-01-2024 12:03:54" correctly and display the local date and time on the staging environment without any "invalid date" errors.
Sachin SinghPosted Jan 5, 2024, 8:54 AM
Test this