We help IT Professionals succeed at work.

date function formatting in jquery

maqskywalker
maqskywalker asked
on
86 Views
Last Modified: 2018-09-12
Inside my jquery button click event i have this date variable

var d = new Date();

But when I display the value inside variable d the format looks like this:

Fri Sep 07 2018 15:35:50 GMT-0700 (Pacific Daylight Time)


How do i format it to this format:  MM/DD/YYYY
Comment
Watch Question

leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
var d = new Date();
var d2 = function(n) { return n>9?n:"0"+n; };
var strDate = d2(1+d.getMonth()) + "/" + d2(d.getDate()) + "/" + d.getFullYear();

Open in new window


if you need more power on formatting date, you may use a lib like this one : https://momentjs.com/
Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
thanks