Javascript - getdate

Posted on 2016-10-04
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
LVL 49

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 49

Assisted Solution

by:Ryan Chong
Ryan Chong earned 250 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 250 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 42

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now