0

I am trying to convert a string value into a JavaScript Date format but I don't seem to find a simple way to do it without having to use additional library such as datejs

var dateString = "20131120"; var date = new Date(dateString); console.log(date); // I need to add an additional day to the original string var newDate = date.getFullYear() + '-' + (date.getMonth()+1) + '-' + (date.getDate()+1); console.log(newDate); 

Any hints are much appreciated.

6
  • 2
    Well, that doesn't look like a common recognicable date format. Split it into Y/M/D parts and pass them distinctly Commented Sep 26, 2013 at 12:32
  • @Bergi Thank you for your info. I don't have control over the output and cannot change the format of it! Commented Sep 26, 2013 at 12:34
  • If it's an option, I'd recommend using a library like moment.js or date.js rather than working directly with Date. Commented Sep 26, 2013 at 12:34
  • you want to convert just date?? Commented Sep 26, 2013 at 12:35
  • @digitup, "output" probably means something else than you think. :) And just modify the input. Commented Sep 26, 2013 at 12:38

4 Answers 4

5

Can you please try this code

 var dateString = "20131120"; var year = dateString.substring(0,4); var month = dateString.substring(4,6); var day = dateString.substring(6,8); var date = new Date(year, month-1, day); 

Here just used the simple substring method

First (0,4) will extract the year then next two is for month and next two is for day of month NOTE: Here we doing `month-1` because it will count from 0 e.g (january==0). 

hope it helps

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

1 Comment

+1 for not using additional libraries, n basic substring func for extracting date.
3

Simple reg expression:

new Date("20131120".replace(/(\d{4})(\d{2})(\d{2})/,"$2/$3/$1")) 

It is very basic regular expression.

Match 4 numbers, Match 2 numbers, Match 2 numbers in groups.

The replace works by using the groups $1 is the first match, $2 is the second match, $3 is the third match. Learn about regular expressions.

4 Comments

This seems to be working well. Testing. I am curious to know what is going on in this regex? can you please explain a bit?
added explanation above like you asked.
I really appreciate that, now I think it will not be fair not to choose this as an answer. Are you aware of any browser incompatibility for this solution ?
Let me tell you; I have been trying to get my head around the RegEx since years and I still have a long mile to go :-) Learning new stuff everyday. Thanks a lot for sharing your knowledge
2

Before ES5, parsing of date strings was entirely implementation dependent. Now a version of ISO8601 has been specified, but it isn't supported by all browsers in use. So the best way is to manually parse it.

So in this case I'd do:

var dateString = "20131120"; var m = dateString.match(/\d\d/g); var date = new Date(m[0] + m[1], --m[2], m[3]); 

which will work in any browser in use that supports javascript.

3 Comments

That is working perfectly. Have never seen such a solution before.
It's the same as Bhavik's, but uses match instead of substring (so I gave him a +1).
Thank you very much +1 for the browsers support
-2

The best you can do is use the ISO format: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS

For example: new Date('2011-04-11')

or

new Date('2011-04-11T11:51:00')

For more Info: MDN | Date

For old Internet Explorer compatibility (IE versions less than 9 do not support ISO format in Date constructor), you should split datetime string representation to it's parts and then you can use constructor using datetime parts, e.g.: new Date('2011', '04' - 1, '11', '11', '51', '00')

Note that the number of the month must be 1 less.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.