Solved

using Date function in jQuery

Posted on 2016-09-29
3
38 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 50

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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…

776 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