[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 13314
  • Last Modified:

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.
0
Conrado ZAVALA
Asked:
Conrado ZAVALA
  • 3
  • 2
  • 2
1 Solution
 
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
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now