Help formatting a date

I have date values which are coming back as something like this:

2015-07-29 00:00:00

var incomingDate=$(this).attr("ows_Updated");
var formatedDate = new Date(incomingDate);
formattedDate= (formatedDate.getMonth()+ 1) + '/' + formatedDate.getDate() + '/' + formatedDate.getFullYear();
console.log("formatedDate = " + formatedDate);

Open in new window

My output is coming out as this: fomattedDate = NaN/NaN/NaN

How can I fix this so that it displays the date as: 07/29/2015

Thank you
greddinAsked:
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.

Julian HansenCommented:
The problem is the original date format. The date function expects dates in ISO 8601 format (https://en.wikipedia.org/wiki/ISO_8601).

This format dictates that when you combine date and time you have to separate the two with a 'T' so in your example the input date would have to be
2015-07-29T00:00:00

Open in new window


If you cannot get the originator of the date to insert the 'T' you can do it in script like this

var incomingDate=$(this).attr("ows_Updated").replace(/\s/,'T');

Open in new window

0
greddinAuthor Commented:
Thanks.  I've modified my code as follows. But I'm still getting the error of "Object doesn't support property or method getMonth".

 Now this is my code:

var incomingDate=$(this).attr("ows_Updated").replace(/\s/,'T');
console.log("incomingDate = " + incomingDate);
var formatteddate = (incomingDate.getMonth()+ 1) + '/' + incomingDate.getDate() + '/' + incomingDate.getFullYear();
console.log("formattedDate = " + formatteddate);

Open in new window


console.log output for incomingDate appears like this now:

incomingDate = 2015-07-29T00:00:00
0
Julian HansenCommented:
Full sample here
<!doctype html>
<html>
<head>
<title>Test</title>
<link href="css/bootstrap.css" rel="stylesheet" />
<link href="css/custom.css" rel="stylesheet" />
<style type="text/css">
</style>
</head>
<body>
<header>
  <div class="container">
    <h1 class="col-lg-9">Changing Datestring to match ISO 8601 Requirement</h1>
  </div>
</header>
<div class="container">
  <h5>Author: Julian Hansen, August 2015</h5>
  <a href="#" ows_Updated="2015-07-29 00:00:00">Click Me</a>
  <div id="result"></div>
  <script src="js/psource.js"></script>
</div>
<script src="http://code.jquery.com/jquery.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script>
$(function() {
  $('a').click(function(e) {
    e.preventDefault();
    var incomingDate=$(this).attr("ows_Updated").replace(/\s/,'T');
    var formatedDate = new Date(incomingDate);
    formattedDate= (formatedDate.getMonth()+ 1) + '/' + formatedDate.getDate() + '/' + formatedDate.getFullYear();
    $('#result').html("formatedDate = " + formatedDate);
  });
});
</script>
</body>
</html>

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
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.

Julian HansenCommented:
Thanks.  I've modified my code as follows. But I'm still getting the error of "Object doesn't support property or method getMonth".

Your code is missing this line
 var formatedDate = new Date(incomingDate);

Open in new window

And then in your building of the output string you need to use formatedDate not incomingDate - the latter is the string version of the Date - the formattedDate is the date object.

See sample in previous post.
0
greddinAuthor Commented:
Thanks Julian!  It's working now.   If I wanted to present the Month as two-digits could I still do it with this code?  

So for example 8 would be 08, etc?
0
greddinAuthor Commented:
Thanks again Julian!  My original question has been answered.
0
Julian HansenCommented:
Yes - you use a trick, prepend a "0" to the month and then take the right 2 most chars like this

("0" + (this.getMonth()+1)).slice(-2)

Open in new window


I have put a sample up that shows this as well as how to add a method to the date object to produce custom formatted output

Sample here
0
Julian HansenCommented:
Thanks again Julian!  My original question has been answered.
You are welcome.
0
greddinAuthor Commented:
Awesome Julian.  Thank you for your help today. It's been great to me.
0
Julian HansenCommented:
:) No problem
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
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.