Javascript - getdate

Posted on 2016-10-04
Medium Priority
Last Modified: 2016-10-16
How can I get getdate in js in this format, mm/dd/yy?
var date = new Date();
alert(date.getMonth()+1 + "/" + date.getDate() + "/" + date.getFullYear().toString().substr(2,2)); ???
Question by:jagr12
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 53

Expert Comment

by:Ryan Chong
ID: 41829281
    var date = new Date();
    document.write("<br>Before: " + date);
    var date2 = ((date.getMonth() < 9) ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear().toString().substr(2, 2);
    document.write("<br>After: " + date2);

Open in new window

or you can try use this library:

LVL 83

Expert Comment

by:Dave Baldwin
ID: 41829284
There isn't any built-in function to return a date in that format.  You have to use the individual 'get' functions and build it yourself.
<script type="text/javascript">
var mydate=new Date();
var yearz=mydate.getYear();
var month=mydate.getMonth();
var daym=mydate.getDate();
if (daym<10) daym="0"+daym;
if (monthz<10) monthz="0"+monthz;
if (yearz > 99) yearz = yearz - 100;
// -->

Open in new window

LVL 53

Assisted Solution

by:Ryan Chong
Ryan Chong earned 1000 total points
ID: 41829286
repost for my example:
    var date = new Date();
    document.write("<br>Before: " + date);
    var date2 = ((date.getMonth() < 9) ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1) + "/" + ((date.getDate() < 10) ? "0" + date.getDate() : date.getDate()) + "/" + date.getFullYear().toString().substr(2, 2);
    document.write("<br>After: " + date2);

Open in new window


Accepted Solution

Rafiq J earned 1000 total points
ID: 41829349
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!

var yyyy = today.getFullYear();
var today = dd+'/'+mm+'/'+yyyy;
document.getElementById("DATE").value = today;
LVL 44

Expert Comment

by:zephyr_hex (Megan)
ID: 41830031
Just want to add here that getDate() is time zone sensitive.  So, you need to be careful in using it.

For example:

var dateString = "2016-10-02";
var jsDate = new Date(dateString);
var day = jsDate.getDate();
console.log(day);  //returns 1, which is wrong for my time zone
var dayUtc = jsDate.getUTCDate();
console.log(dayUtc);  //return 2, which is correct

Open in new window

Here is a JS Fiddle.

Featured Post

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question