Solved

using Date function in jQuery

Posted on 2016-09-29
3
48 Views
Last Modified: 2016-10-01
I'm using jQuery

If i use this function

var d = new Date();

That saves today's date in the variable called d in this format:   Thu Sep 29 2016 21:59:45 GMT-0800 (Pacific Standard Time)

How do I revise the Format so it saves today's date in this format?:    09/29/2016
0
Comment
Question by:maqskywalker
3 Comments
 
LVL 51

Accepted Solution

by:
Ryan Chong earned 250 total points
ID: 41822946
try:
<script>
      var d = new Date();
      document.write("<br>Before: " + d);
      var d2 = ((d.getMonth() < 9) ? ("0" + (d.getMonth() + 1)) : d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getFullYear();
      document.write("<br>After: " + d2);
  </script>

Open in new window

0
 
LVL 33

Assisted Solution

by:ste5an
ste5an earned 250 total points
ID: 41823186
That saves today's date in the variable called d in this format[..]

Nope, that's wrong. It is saved in a internal numeric format. But there is no other way to display it, by using a format. The default format is specified by using the browsers locale:

var dt = new Date('2013-01-30');
console.log(dt.toUTCString());
console.log(dt.toString());

Open in new window


Imho the best way to deal with it, is to use moment.js:

console.log(moment().format('MM/DD/YYYY'));
// or
moment.locale('en');
console.log(moment().format('L'));

Open in new window

0
 
LVL 1

Author Closing Comment

by:maqskywalker
ID: 41825045
Thanks
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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)

726 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