12

Am getting a GMT time from my server in this format

Fri, 18 Oct 2013 11:38:23 GMT

My requirement is to convert this time to local time using Javascript, eg:/ if the user is from India, first i need to take the time zone +5.30 and add that to my servertime and convert the time string to the following format

2013-10-18 16:37:06

I tried with following code but not working

var date = new Date('Fri, 18 Oct 2013 11:38:23 GMT');
date.toString();

Please help me to solve this issue, Thanks in advance

Dibish
  • 9,133
  • 22
  • 64
  • 106

1 Answers1

34

This worked for me:

<script>
var strDateTime = "Fri, 18 Oct 2013 11:38:23 GMT";
var myDate = new Date(strDateTime);
alert(myDate.toLocaleString());
</script>

Please take a look at http://www.w3schools.com/jsref/jsref_obj_date.asp for all further date time manipulations, from the date object myDate.

jacouh
  • 8,473
  • 5
  • 32
  • 43
  • Yes, now its working, thank you so much for your help, Do you have any idea about convert this date fromat 18/10/2013 17:08:23" in to 2013-10-18 17:08:23 – Dibish Oct 21 '13 at 10:16
  • Hope you dont mind asking a doubt, is this conversion based on users system time/browser time ? What will happened when their time zone are wrong in their system. Is there any other way to do this ? – Dibish Oct 21 '13 at 10:40
  • Yes it's browser's time, so Windows Locale Settings or any OS, We should not care about the wrong browser settings - let him a wrong info. Sorry. – jacouh Oct 21 '13 at 11:02
  • Why do you have to put the day and date, is there a way to do it with just the time? – Philll_t Jul 16 '15 at 21:27