Solved

Javascript - getdate

Posted on 2016-10-04
5
49 Views
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)); ???
0
Comment
Question by:jagr12
5 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
<script>
    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);
  </script>

Open in new window

or you can try use this library:

Moment.js
http://momentjs.com/
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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;
monthz=month+1;
if (monthz<10) monthz="0"+monthz;
if (yearz > 99) yearz = yearz - 100;
document.write(+monthz+"/"+daym+"/"+yearz);
// -->
</script>

Open in new window


http://www.w3schools.com/jsref/jsref_obj_date.asp
0
 
LVL 49

Assisted Solution

by:Ryan Chong
Ryan Chong earned 250 total points
Comment Utility
repost for my example:
<script>
    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);
  </script>

Open in new window

0
 
LVL 1

Accepted Solution

by:
Rafiq J earned 250 total points
Comment Utility
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!

var yyyy = today.getFullYear();
if(dd<10){
    dd='0'+dd
}
if(mm<10){
    mm='0'+mm
}
var today = dd+'/'+mm+'/'+yyyy;
document.getElementById("DATE").value = today;
0
 
LVL 42

Expert Comment

by:zephyr_hex
Comment Utility
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);
console.log(jsDate);
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.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 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

11 Experts available now in Live!

Get 1:1 Help Now