javascript 将 GMT 时间转换为当地时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19491222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert GMT time to local time
提问by Dibish
Am getting a GMT time from my server in this format
我以这种格式从我的服务器获取 GMT 时间
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
我的要求是使用Javascript将此时间转换为当地时间,例如:/如果用户来自印度,首先我需要将时区+5.30并将其添加到我的服务器时间并将时间字符串转换为以下格式
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
请帮我解决这个问题,提前致谢
回答by jacouh
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.aspfor all further date time manipulations, from the date object myDate.
请查看http://www.w3schools.com/jsref/jsref_obj_date.asp以了解所有进一步的日期时间操作,来自日期对象 myDate。