Reputation: 1257
I am updated the value of an HTML input field with data from a query:
$("##warranty_end_date#id#").val(warranty_end_date);
But the data is store in my database as a SQL Date/Time and has an output like this: May, 08 2019 00:00:00 -0400
I would like the data to be formatted like this: 05/08/2016
How can I accomplish this?
Upvotes: 1
Views: 1638
Reputation: 1773
You can accomplish this using the following:
var date = new Date('May, 08 2019 00:00:00 -0400');
var output = date.toLocaleFormat('%m/%d/%Y');
alert(output);
Here's a jsfiddle: https://jsfiddle.net/y4zemzqg/
Also, look over using moment.js. http://momentjs.com/ Moment.js makes formatting dates and time incredibly easy. I've used it for quite some time with no issue:
var theDate = moment('May, 08 2019 00:00:00 -0400').format('MM/DD/YYYY');
alert(theDate);
Here is a JS Fiddle using the moment.js solution: https://jsfiddle.net/v923bn5s/
Upvotes: 0
Reputation: 1598
warranty_end_date = "May, 08 2019 00:00:00 -0400";
var d = new Date(warranty_end_date);
var f = ("00" + (d.getDate()).toString()).slice(-2) + "/" + ("00" + (d.getMonth()+1).toString()).slice(-2) + "/" + (1900 + d.getYear()).toString();
$("##warranty_end_date#id#").val(f);
Upvotes: 1
Reputation: 2058
var date = new Date('2010-10-11T00:00:00+05:30');
alert((date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear());
Upvotes: 0
Reputation: 1523
Maybe you could format the date in the SQL query already. Something like:
SELECT convert(varchar, getdate(), 101)
101 = mm/dd/yyyy – 10/02/2008
Upvotes: 1