• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 62
  • Last Modified:

using Date function in jQuery

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
maqskywalker
Asked:
maqskywalker
2 Solutions
 
Ryan ChongCommented:
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
 
ste5anSenior DeveloperCommented:
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
 
maqskywalkerAuthor Commented:
Thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now