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?
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);
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
var date = new Date('2010-10-11T00:00:00+05:30');
alert((date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear());
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/
Related
I have this date 20/4/2016;4:23:00;PM
But i want to put in like this DD/MM/YYYY HH:mm:ss without the AM or PM
For example 20/04/2016 16:23:00
Thanks in advance
Carlos Vieira
In javascript the supported format for in your case is "MM/DD/YYYY hh:mm:ss AM" or "MM/DD/YYYY hh:mm:ss PM".
If you will give input like "04/20/2016 11:00:06 PM" then you will get an output as date format "Fri Mar 04 2016 23:00:06 GMT+0530 (IST)" which you can save it.
You can put "04/20/2016 11:00:06Z PM" mark here and get the result according to the UTC.
For reference https://www.w3schools.com/js/js_date_formats.asp
One approach is to reformat the string and convert the time part to 24hr time, e.g:
function reformatDate(s) {
var b = s.split(';');
var t = b[1].split(':');
var h = +t[0];
h = h%12 + (/pm/i.test(b[2])? 12 : 0);
return b[0] + ' ' + h + ':' + t[1] + ':' + t[2];
}
console.log(reformatDate('20/4/2016;4:23:00;PM')); // 20/4/2016 16:23:00
That avoids the vagaries of parsing the entire string to a date then reformatting it. If that's what you want to do, there are plenty of questions here already on parsing and formatting dates:
Where can I find documentation on formatting a date in JavaScript?
lots of parsing questions
Currently I am making a rest call to SharePoint using JavaScript Rest API. I am getting a Modified date which comes in the following format "2016-08-27T17:40:09Z", from what I have been reading this is a problem many developers have a problem with.
So I decided to go ahead and use the Date.parse(dateString) method to convert the quirky date format and now I am getting Sat Aug 27 2016 13:40:09 GMT-0400 (Eastern Daylight Time).
Now this is something that can be understood, however I am not looking for this, I am looking for the following format Month\Date\Year Hour:Minute. I have been reading the documentation but I am not found anything yet.
var d = new Date('2016-08-27T17:40:09Z'),
dFormatted = [d.getMonth() + 1, d.getDate(), d.getFullYear()].join('\\') + ' ' + [d.getHours(), d.getMinutes()].join(':');
console.log(dFormatted);
If your format is very unique then you need to make your own formatter:
function toMyFormat(time) {
var d = new Date(time);
return d.getMonth() + '\\' + d.getDate() + '\\' + d.getFullYear() + ' ' + d.getHours() + ':' + d.getMinutes();
}
console.log(toMyFormat('2016-08-27T17:40:09Z'));
You may however be lucky enough to find a localeString format that suits you:
var d = new Date('2016-08-27T17:40:09Z');
console.log(d.toLocaleString('en-US'));
console.log(d.toLocaleString('da-DK'));
console.log(d.toLocaleString('de-GE'));
I try to get the correct date format, like this: 24-7-2015.
date = new Date("24-7-2015").toString({ dateFormat: 'd-M-yy' })
but the output of date is then: Wed Dec 7 00:00:00 UTC+0100 2016
Thank you
See this answer
If you want to make it easely, like in your example try to use momentjs
moment('24-7-2015', 'D-M-YYYY').format('DD-MM-YY');
Please try with the below code snippet.
var date = new Date("24-7-2015") // If this is not worked than check your local system date format
document.write(date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear());
Let me know if any concern.
I want to display the date timestamp retrieved from a MySQL database in a HTML table dynamically. I have an array of dates. I am getting date in the following format:
Mar 10, 2014 6:40:45 AM
How can I get the date as it is and represent it in my HTML table using JavaScript?
Assuming that Mar 10, 2014 6:40:45 AM is your input date format, This code will help:
var myDate = new Date('Mar 10, 2014 6:40:45 AM');
var reqDate = ((myDate.getMonth() + 1) + "/" + myDate.getDate() + "/" + myDate.getFullYear());
console.log(reqDate);
output
3/10/2014
Given your clarification that you cannot change the format of the incoming date, you need something like this:
var dateParts = isoFormatDateString.split("-");
var jsDate = new Date(dateParts[0], dateParts[1] - 1, dateParts[2].substr(0,2));
I am trying to figure out what the W3C website means by dateString.
http://www.w3schools.com/jsref/jsref_obj_date.asp
I am trying to do something like:
var _date = new Date("Mon Aug 12 2013 2:00 AM");
or even:
var _date = new Date("Mon, Aug 12 2013, 2:00 AM");
Is there a quick way of turning my string into a format that the date object likes?
Thank you
edit:
I suppose it expects the following:
var d = new Date()
d.toDateString()
"Tue Aug 13 2013"
Is it only that type of string?
The Javascript string-based Date constructor accepts strings in a format accepted by Date.parse().
These are date strings compliant with RFC-2822 or ISO-8601.
Use String in this format:
new Date('2013-08-13')
or
new Date('2013-08-13T10:51:00');
Here, this is how to use dateString as a parameter
var dateString = "08/12/2013";
var d = new Date(dateString);
dateString = d.getFullYear() + "/" + d.getMonth() + "/" + d.getDate();
document.write(dateString);
Remember month are stored at 0th index in javascript.