0

I have a date at a string - dtStr

var dtStr = "Thu May 28 02:13:16 BDT 2015";  

I want to get a date like - MM DD YYYY HH mm format from the dtStr. For getting this I am trying to convert the dtStr to a Date and then try to use date format like this -

var dtStr = "Thu May 28 02:13:16 BDT 2015";
today = new Date(dtStr);
alert( today.toLocalDateFormat("MM DD YYYY HH mm") );  

But it didn't work for me. Can any one help me for - converting dtStr to a date with format MM DD YYYY HH mm ?

Thanks in advance.

3
  • 2
    Close your alert method call: alert(today.toLocalDateFormat("MM DD YYYY HH mm")); Also, what is the alert displaying? You may want to have a look at Moment.js. It makes it much easier to parse. Commented May 27, 2015 at 21:20
  • @chRyNaN, it was a typo. In my real code it's parenthesis is well balanced. And I can not reach the alert, because there is some error at new Date(dtStr) Commented May 27, 2015 at 21:24
  • I believe the method is toLocalDateString not toLocalDateFormat. Or use, toLocalFormat. Documentation. Commented May 27, 2015 at 21:24

3 Answers 3

2

Unfortunately, the string you're trying to parse won't be accepted by Date.parse(), the method that will parse the string when you're creating it. If the string will always be in that format, you could do some string manipulation and rearrange it to the RFC2822/IETF format, which Date() can handle.

// this creates a proper Date object
new Date("Thu, May 28 2015 02:13:16 +0600");

Alternatively, you could create a new Date object with one of the other constructors, by splitting/parsing the string yourself, and inserting them in the correct places in the constructor.

At this point, you'll have a Date object, but you still need to get the values from it - the only built in method that can do something like what you're trying to do is toLocaleFormat(), which isn't standard track (it's not supported in my version of Chrome, for example). Thus, you would need to get the values independently, and concatenate them together.

At this point, it's probably easier to just do straight up parsing of the string, and skip the Date object altogether, or use a library like datejs, which provides support for formatting output strings.

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

Comments

1

You may try to use the following method -

function formatReview(date){

        /*****************************************************************
        * The method parameter 'date' is in the following format - 
        *          "Thu May 28 02:13:16 BDT 2015"
        * Javascript 'new Date()' has not suitable constructor to 
        * support this format. The parameter 'date' need to 
        * convert to fee the Date() constructor. 
        *******************************************************************/

        var monthSymbols = "JanFebMarAprMayJunJulAugSepOctNovDec";

        var elements = date.split(" ");
        var day = elements[0];
        var monthName = elements[1];
        var monthIndex = monthSymbols.indexOf(monthName)/3 +1;
        var date = elements[2];
        var year = elements[5];

        var timestamp = elements[3];
        var timestampElements = timestamp.split(":");
        var hour = timestampElements[0];
        var minutes = timestampElements[1];

        var dateString = monthIndex +"-"+ date +"-"+ year +" "+ hour +":"+ minutes;
        return dateString;
    }

Comments

1

Try this

var todayDate=new Date("Thu May 29 2014 13:50:00");
  var format ="AM";
  var hour=todayDate.getHours();
  var min=todayDate.getMinutes();
  if(hour>11){format="PM";}
  if (hour   > 12) { hour = hour - 12; }
  if (hour   == 0) { hour = 12; }  
  if (min < 10){min = "0" + min;}
  document.write(todayDate.getMonth()+1 + " / " + todayDate.getDate() + " / " +  todayDate.getFullYear()+" "+hour+":"+min+" ");

fiddle: http://jsfiddle.net/e0ejguju/

Comments

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.