-2
myDate2 = "6.2014"    
var date2= new Date(myDate2);

Here myDate2 does not contain days. It contains only year and month, I want to alert(date2), displays date error.

How to remove and output year and month.

In short I want to process date having format (mm.yyyy) instead of (dd.mm.yyyy).

Chris Martin
  • 28,558
  • 6
  • 66
  • 126
yank
  • 125
  • 8
  • 1
    What exactly do you want? You want to make `date2` have days? – moffeltje Apr 20 '16 at 06:56
  • Why do you want to have days if you did not define it at the beginning itself ? – Nikhilesh K V Apr 20 '16 at 06:57
  • @moffeltje I want only year and month. – yank Apr 20 '16 at 07:03
  • @NikhileshKV i have input field from where only year and month comes in (mm.yyyy) format – yank Apr 20 '16 at 07:04
  • You only want to have year and month and you state that you only have year and month. So what are you missing or what would the output you expect look like? – newBee Apr 20 '16 at 07:05
  • What is `alter(date2)`? – moffeltje Apr 20 '16 at 07:06
  • You will get an Invalid Date error if you don't follow the correct format. What you can do is manually add any day to your date since you're not using it anyway. – Nikhilesh K V Apr 20 '16 at 07:06
  • yes I have `have year and month` and I also mention `myDate2 = "6.2014"` – yank Apr 20 '16 at 07:07
  • @NikhileshKV if I add it manually then I there will be problem for me because it need to add at least 1 day, and later output will be different, so I need only year and month and process it – yank Apr 20 '16 at 07:09
  • guys dnt down vote, what's wrong with this question?? – yank Apr 20 '16 at 07:11
  • can you please add a jsfiddle for better understanding @yank. I did not down vote but the reason might be that your question is not very clear. – Nikhilesh K V Apr 20 '16 at 07:13
  • 1
    I downvoted because I have no idea what you want to achieve and what the actual problem is. – moffeltje Apr 20 '16 at 07:16
  • it is my php project and I need to add small js. I wrote the question making so short and simple as I can. jsfiddle is not good idea bcoz I am middle of project and many thing are related to other...... So i need to mention alot of things. But i can simplify the question more, – yank Apr 20 '16 at 07:19
  • 1
    You need to specify exactly what you want to do and the issues you're having trying to do it. E.g. do you simply want to change "3.6.2015" to "6.2015"? Or do you want to create a Date instance for 1 June given a date string for any time in June? Creating dates by parsing with the Date constructor is **strongly** discouraged. – RobG Apr 20 '16 at 07:21
  • @RobG plz see the question once again – yank Apr 20 '16 at 07:40

1 Answers1

0

If you simply want to parse "6.2014" to a Date, then split the string on the period "." and pass the parts to the Date constructor in the right order (year, month, day). Subtract 1 from the month as they are zero based (0 = January, 1 = February, etc.).

MDN is a good resource.

function parseMY(s) {
  var b = s.split(/\D/);
  return new Date(b[1], b[0]-1);
}

document.write(parseMY('6.2014'));

If you just want to reformat the string, then split it into its parts and reformat as a string:

document.write('1/' + '6.2014'.split('.').join('/'))

There are many libraries, large and small, that can help with parsing but if you only need to deal with a single format, a two line function should suffice (or a couple more lines if validation is required).

RobG
  • 124,520
  • 28
  • 153
  • 188