0

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>

7
  • Can you put up the code that you tried? Commented Jul 19, 2019 at 10:30
  • Try using momentJS. Commented Jul 19, 2019 at 10:32
  • Edited my inital post. Commented Jul 19, 2019 at 10:35
  • Start with var parts = dt.split(" "); The bad time is now in parts[3]. Commented Jul 19, 2019 at 10:36
  • 1
    Here's example code: jsfiddle.net/khrismuc/2esxhyqb Commented Jul 19, 2019 at 10:52

2 Answers 2

1

This is my solution:

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>

And this is Chris G's, which I prefere more: https://jsfiddle.net/khrismuc/2esxhyqb

Sign up to request clarification or add additional context in comments.

Comments

0

Here is the solution:

var timestamp = "Mar 8, 2019 5:13pm GMT+0100";

var cur_date = timestamp.replace(/T/, ' ').replace(/\..+/, '').substr(0, 19);
console.log(cur_date)

1 Comment

This wont work: jsfiddle.net/qb0xrck9/1 I had to remove pm/am and convert it to valid time, in case its pm. Thank you anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.