1

I want to parse date in the format ddMMyyhhmm (eg 2804121530 representing 28th April 2012, 3:30 PM) to javascript Date() object.

Is there any oneliner solution to it? I'm looking for something of the kind:

var date = Date.parse('2804121530', 'ddMMyyhhmm');

or

var date = new Date('2804121530', 'ddMMyyhhmm'); 

Thanks for help!

2

3 Answers 3

2

A useful library here is DateJs. Just add a reference:

<script src="http://datejs.googlecode.com/files/date.js"
        type="text/javascript"></script>

and use Date.parseExact:

var dateStr = '2804121530';
var date = Date.parseExact(dateStr, 'ddMMyyHHmm');
Sign up to request clarification or add additional context in comments.

Comments

2

For a fast solution you can brake that string into pieces and create date from those pieces

function myDateFormat(myDate){
    var day = myDate[0]+''+myDate[1];
    var month = parseInt(myDate[2]+''+myDate[3], 10) - 1;
    var year = '20'+myDate[4]+''+myDate[5];
    var hour = myDate[6]+''+myDate[7];
    var minute = myDate[8]+''+myDate[9];
    return new Date(year,month,day,hour,minute,0,0);
}

var myDate = myDateFormat('2804121530');

or a simper solution:

function myDateFormat(myDate){
    return new Date(('20'+myDate.slice(4,6)),(parseInt(myDate.slice(2,4), 10)-1),myDate.slice(0,2),myDate.slice(6,8),myDate.slice(8,10),0,0);
}
var myDate = myDateFormat('2804121530');

2 Comments

You need to + 1 the month (it is zero based), so you have to parse at least that (sorry - -1 :/). Also, you could use string.slice to remove some ugliness.
added -1 :) because Date starts from 0
0
(new Date(1381344723000)).toUTCString() 

Correct me if 'm worng...

2 Comments

James has the string '2804121530', so I don't think this helps much.
Right, and I guess the param is seconds/milliseconds since epoch. In my case it's a date string in the format ddMMyyhhmm

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.