JavaScript Date Format

Need help with a simple JavaScript that will reformat a date string.  I currently have the variable containing a date string in CCYY-MM-DD format.  I need to change this format to mm/dd/ccyy.

var revision = node.getRevision();              
var MyDate = node.getValue("UPDT_DATE").GetSimpleValue(ScriptHelper.getISODate(revision.getCreated()));

//MyDate is formatted as  CCYY-MM-DD.  I need to change to MM/DD/CCYY.
tmajor99Asked:
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.

[ fanpages ]IT Services ConsultantCommented:
Below I have replicated the extraction of the value of UPDT_DATE by setting the value of MyDate directly (as 27 August 2015) within [CCYY-MM-DD] format.

I have then transposed the digits & output the MyDate variable within the required format [MM/DD/CCYY]:

//var revision = node.getRevision();
//var MyDate = node.getValue("UPDT_DATE").GetSimpleValue(ScriptHelper.getISODate(revision.getCreated()));

  var MyDate = new Date('2015-08-27');

  var MyDate = ((MyDate.getMonth()<9)?'0':'') + (MyDate.getMonth() + 1) + '/' + ((MyDate.getDate()<10)?'0':'') + MyDate.getDate() + '/' + MyDate.getFullYear();

  document.write(MyDate);

Open in new window

0
Julian HansenCommented:
You can subclass the Date object to add your own format method and then use string concatenation and split to 0 pad the date
<script>
Date.prototype.myFormat = function() {
  var dateString =  
    ("0" + (this.getMonth()+1)).slice(-2) +"-"+ // 0 pad month
    ("0" + this.getDate()).slice(-2) + " " +    // 0 pad day
    this.getFullYear();

  return dateString;
}
 
var d = new Date();
alert (d.myFormat());
</script>

Open in new window

0
Prakash SamariyaIT ProfessionalCommented:
I think default format of date object in Javascript is "mm/dd/yyyy" format!

You do not need to do anything extra, just give date string into Date object and use it's inbuilt functions like below:
var dt = new Date('2015-08-31');
//complete date alongwith time
document.write('date is:'+dt);
//Result=> date is:Mon Aug 31 2015 05:30:00 GMT+0530 (India Standard Time)

//Date with localDateString (Local can be use to set the format of date like
// for "en-US" => 'mm/dd/yyyy' and "en-GB" => 'dd/mm/yyyy'
document.write('<br/>date is:'+dt.toLocaleDateString('en-US'));
//Result=> date is:8/31/2015

//Complete date with time  in local format
document.write('<br/>date is:'+dt.toLocaleString('en-US'));
//Result=> date is:8/31/2015, 5:30:00 AM

Click below link to check with Javascript documentation
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
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.