Convert a string into date with javascript or jquery.

Dear Experts,

I have this string "07/31/2014" and I want to convert that string into a date.
How can I do this with javascript or jquery?

Thanks in Advance.

Best Regards.
Conrado ZAVALAAsked:
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.

leakim971PluritechnicianCommented:
test page : http://jsfiddle.net/oc45w9c3/
var toDate = function(mmDDYYY) {
   var arr = mmDDYYY.split("/");
   return new Date(arr[2],arr[0]-1,arr[1]);
}

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
Ray PaseurCommented:
<script>
var d = new Date("07/31/2014");
alert(d);
</script>

Open in new window

0
RobOwner (Aidellio)Commented:
The following returns a number.  I've found this very flexible for comparing dates, storing in databases and it's easily formatted when you need it.
var d = Date.parse("07/31/2014");
alert(d);  // returns the number of milliseconds since January 1, 1970

Open in new window

It's also important to note that these functions do NOT support the European/Australian date format dd/mm/yyyy and in that case I would recommend using a library like datejs (http://www.datejs.com) or parsing it yourself.  The important thing is you have to be aware of the format and not mix them unknowingly.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Ray PaseurCommented:
OT, but if you have the PHP scripting language date conversions are very easy.  The principles about ISO-8601 standards are applicable to all languages.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0
RobOwner (Aidellio)Commented:
As Ray has indicated you can see this on Mozilla
dateString
A string representing an RFC2822 or ISO 8601 date (other formats may be used, but results may be unexpected).
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
0
Ray PaseurCommented:
And one other note... JavaScript was created in about 2 months time, in a dead-heat race for a finished language.  As a result there are some oddities in JavaScript, to be sure.  The "month" number returned by JavaScript functions is zero-based, with zero being January and 11 being December.  Nobody writes calendar dates that way (except JavaScript).  You do not have to worry about this anomaly if you're using the new Date() notation, but it's worth being aware of it when you retrieve fractional parts of a date value or attempt arithmetic.

Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMonth
0
leakim971PluritechnicianCommented:
var removeMonthFromDate = function(mmDDYYY, monthToRemove) {
   var arr = mmDDYYY.split("/");
   return new Date(arr[2],arr[0]-1-monthToRemove,arr[1]);
}

Open in new window


I can do : removeMonthFromDate("07/31/2014", 60); // 5 years ago

http://jsfiddle.net/bf8med9w/
0
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
jQuery

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.