0

how can I convert a date string

'13.04.2015'

into JavaScript Date object in a performant way?

quma
  • 5,233
  • 26
  • 80
  • 146
  • take a look at this post, using moment.js http://stackoverflow.com/questions/22184747/parse-string-to-date-with-moment-js – Aleksandar Gajic Mar 18 '16 at 13:13
  • why you would care about perfomrance? do you want to parse more than 1 date? all solutions so far take less than 1ms: https://jsfiddle.net/q6vb1m64/1/ – Roland Starke Mar 18 '16 at 13:34

3 Answers3

2

Try this parser:

var dateString = '13.04.2015';
var myDate = new Date(dateString.split('.').reverse());

Check the demo.

Dmitri Pavlutin
  • 18,122
  • 8
  • 37
  • 41
0

Try this code:

var str = '13.04.2015',
    dateParts = str.split('.'),
    year = dateParts[2],
    month = dateParts[1],
    day = dateParts[0],
    yourDate = new Date([year, month, day]);

JSFiddle example

Also if need to work a lot with dates, I would recomend you MomentJs Library

Code with it will look like:

moment('13.04.2015', 'DD.MM.YYYY')

JSFiddle example moment

ilyabasiuk
  • 4,270
  • 4
  • 22
  • 35
0
new Date( "13.04.2015".replace( /(\d{2}).(\d{2}).(\d{4})/, "$2/$1/$3") );

JSFiddle

Marcus
  • 8,230
  • 11
  • 61
  • 88