Using javascript on Ajax call I get the date format as
Mon Feb 13 2017 00:00:00 GMT+0530(India Standard Time).
I want to change the date format according to clients system date format using javascript.
Can anybody help me with this issue...
Using javascript on Ajax call I get the date format as
Mon Feb 13 2017 00:00:00 GMT+0530(India Standard Time).
I want to change the date format according to clients system date format using javascript.
Can anybody help me with this issue...
Edit:
You can use .toLocaleDateString()
to format date based on the client's machine.
var date=new Date();
alert(date.toLocaleDateString());
You should really be using moment.js.
you can initialize locale using moment.locale();
and then format it accordingly.
Follow the link for documentation
simple... the following code should do the job :
var date = new Date();
var dateString = date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear();
console.log(dateString);
hope this helps :)
Use dateObj.getTimezoneOffset()
method to get the timezone offset and then add/subtract according to the result you receive, as described here on
MDN and already answered stackoverflow question
function convertUTCDateToLocalDate(date) {
var newDate = new Date(date.getTime()+date.getTimezoneOffset()*60*1000);
var offset = date.getTimezoneOffset() / 60;
var hours = date.getHours();
newDate.setHours(hours - offset);
return newDate;
}
Usage:
var date = convertUTCDateToLocalDate(new Date(date_string_you_received));
Display the time based on local time
date.toLocaleString();
as answered by @Adorjan Princz