how to parse a date string --javascript

Hello,
  How do I parse a date string in the format "2015-07-02T19:04:39.424Z" ..

I need to return a string with the format MM/YYYY  (07/2015)

Also, if the string is in the proper format (MM/YYYY) then do not parse it...


Thanks for your help
chand pbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CuteBugCommented:
You can check if the given input is in the proper format (MM/YYYY) by checking the following:
1. length of the input
2. presence of '/' at the 3rd character location
3. validating the first two characters are numbers in the range 1-12 and validating the last four characters are a valid number

For parsing the UTC format string check this link
http://delete.me.uk/2005/03/iso8601.html
0
Snarf0001Commented:
For the mm/yyyy check, you can use a regular expression to test for the validity:
^(0[1-9]|1[0-2])\/\d{4}$/

For the full UTC, you can parse it by simply calling new Date("xxx").  If it's valid it will return a date object, if not, all the date parameters will be NaN.

var input = "07/2015";
//var input = "2015-07-02T19:04:39.424Z";


if(/^(0[1-9]|1[0-2])\/\d{4}$/.test(input)) 
	return input;

var parsedDate = new Date(input);
if(!isNaN(parsedDate.getYear())) {
	//month is zero based, and needs to have a zero appended if less than 10
	var month = parsedDate.getMonth() + 1;
	if(month < 10) month = "0" + month;

	return month + "/" + parsedDate.getFullYear();
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.