Solved

using Date function in jQuery

Posted on 2016-09-29
3
25 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 49

Accepted Solution

by:
Ryan Chong earned 250 total points
Comment Utility
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 32

Assisted Solution

by:Stefan Hoffmann
Stefan Hoffmann earned 250 total points
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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 …
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

771 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

9 Experts available now in Live!

Get 1:1 Help Now