I want to format a date, which has a bad format.
For example:
Mar 8, 2019 5:13pm GMT+0100
It would work, if it would've the following format:
Mar 8, 2019 17:13:00 GMT+0100
Is there any way to format the date quickly, so it can be parsed by the default javascript Date function?
I tried to find the substr by pm/am. But I don't know how to get the rest of it(5:13pm) and also how to format it "automatically". I thought about adding 12, but I am not sure how to grab the substring.
Thanks in advance.
Edit:
As for now I wsa using momentJs, but this is not working either.
function returnFormatedDate(dt)
{
return moment(dt).format('DD.MM.YY HH:mm');
};
Returns "invalid date"
Edit2: Example code but I prefer Chris G.'s one:
var str = "Mar 8, 2019 5:13pm GMT+0100";
var parts = str.split(" ");
console.log(parts);
if(3 in parts)
{
badPart = parts[3];
if(badPart.includes("pm"))
{
var newBadPart = badPart.replace('pm', '');
var moddedDate = newBadPart.split(":");
moddedDate[0] = 12 + Number(moddedDate[0]);
badPart = moddedDate.join(":");
}
else if(badPart.includes("am"))
{
badPart.replace("am", "");
}
var newParts = [parts[0], parts[1], parts[2], badPart, parts[4]];
var dte = new Date(newParts.join(" "));
console.log(dte);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
var parts = dt.split(" ");The bad time is now inparts[3].