-1

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...

shv22
  • 680
  • 5
  • 28
Soni
  • 42
  • 6
  • Please share what have you tried – Saurabh Sharma Feb 20 '17 at 05:46
  • What is date format you want to convert – shv22 Feb 20 '17 at 05:46
  • Check out javascript's *native* Date object: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date it has methods for parsing and printing dates – Rico Kahler Feb 20 '17 at 05:48
  • I want the date format of the clients machine...the culture info – Soni Feb 20 '17 at 05:49
  • I am not getting the date as per my system format ..My system format date is 20-Feb-17 – Soni Feb 20 '17 at 05:52
  • var value = orders[i].Date; var dt = new Date(parseInt(value.substring(6, value.length - 2))); var dtString = dt.getDate() + "/" + (dt.getMonth() + 1) + "/" dt.getFullYear(); @SaurabhSharma This is what I am using for now. – Soni Feb 20 '17 at 05:54
  • you can try [toLocaleDateString()](https://www.w3schools.com/jsref/jsref_toLocaleDateString.asp) to convert the date to your/clients system date format – m87 Feb 20 '17 at 05:55
  • @siam I had used this..It din work for me.. – Soni Feb 20 '17 at 05:59

3 Answers3

1

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

Saurabh Sharma
  • 2,422
  • 4
  • 20
  • 41
0

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 :)

m87
  • 4,445
  • 3
  • 16
  • 31
  • @Soni you want to convert clients system date format to your system date format? – m87 Feb 20 '17 at 06:30
  • No its the other way..I have the date I want to change it to clients system date format.. – Soni Feb 20 '17 at 06:34
  • See Client can change his system date format..So I dont want to hardcode the month and year format..I want to just change the date to clients date time format . – Soni Feb 20 '17 at 07:04
  • @Soni its not hard-coded.. its getting the date dynamically – m87 Feb 20 '17 at 07:05
  • Thanks a lot @siam for helping me with this issue but still this code din work for me.. – Soni Feb 20 '17 at 07:13
  • @Soni oh.. :/ in which format you want the date string `20/2/2017` or `20-Feb-2017`? – m87 Feb 20 '17 at 09:36
0

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

Community
  • 1
  • 1
warl0ck
  • 3,356
  • 4
  • 27
  • 57
  • I tried using this function and I got the format as gives me the format as 2/13/2017 whereas it has to be 13-Feb-17 format – Soni Feb 20 '17 at 06:30
  • all you have to do it use [momentjs](http://momentjs.com/) to the view you want, in your case `moment(date).format('Do MMMM YY');` check this [fiddle](http://jsfiddle.net/jsjain007/h38h0Ljf/2/) hope this solves your date format problem as well – warl0ck Feb 20 '17 at 07:38