使用javascript将日期字符串转换为UTC + 0530格式 [英] Converting a date string into UTC+0530 format using javascript

查看:143
本文介绍了使用javascript将日期字符串转换为UTC + 0530格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的日期格式为 2011年2月14日,但我想将其转换为格式 Mon Feb 14 10:13 :50 UTC + 0530 2011 。如何实现这个?

解决方案

使用新的日期(Date.UTC(年,月,日) ,小时,分钟,秒))您可以从特定的UTC时间创建一个日期对象。



我尝试过这个代码,它返回正确日期(在印度语言区域)

  var d = Date.parse(2011年2月14日); 
document.write(new Date(d));

输出:

 code> 2011年2月14日星期一00:00:00 GMT + 0530(印度标准时间)。 



这是一个在不同时区之间转换的例子。 >

 < html> 
< body>

< script type =text / javascript>

//设置您的偏移量为+5.5为IST
var offsetIST = 5.5;


//设置您的偏移量为-8为PST
var offsetPST = -8;

//从给定字符串创建一个新的日期
var d = new Date(Date.parse(14,2011年2月));

//通过减去当前的时区偏移量来转换为UTC datetime
var utcdate = new Date(d.getTime()+(d.getTimezoneOffset()* 60000));

//然后将UTS日期映射到所需的时区偏移量,如IST
var istdate = new Date(utcdate.getTime() - ((-offsetIST * 60) * 60000));

//然后将UTS日期缩放到所需的时区偏移,如PST(加拿大美国)$ 8 $ b var pstdate = new Date(utcdate.getTime() - (( -offsetPST * 60)* 60000));

document.write(d);
document.write(< br />);
document.write(utcdate);
document.write(< br />);
document.write(istdate);
document.write(< br />);
document.write(pstdate);
< / script>

< / body>
< / html>

输出:

 code> 2011年2月14日星期一00:00:00 GMT + 0530(印度标准时间)
2011年2月13日18:30:00 GMT + 0530(印度标准时间)
Mon Feb 14 2011 00:00:00 GMT + 0530(印度标准时间)
Sun Feb 13 2011 10:30:00 GMT + 0530(印度标准时间)
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> , UTC IST PST


I have a date in the format 14-Feb-2011, but I want to convert it into the format Mon Feb 14 10:13:50 UTC+0530 2011. How Can I achieve this?

解决方案

Using new Date(Date.UTC(year, month, day, hour, minute, second)) you can create a Date-object from a specific UTC time.

I tried this code and it returned proper date (In Indian Locale)

var d=Date.parse("14,Feb,2011");
document.write(new Date(d));

Output:

Mon Feb 14 2011 00:00:00 GMT+0530 (India Standard Time) .


Here's an example of converting between different time zones.

<html>
<body>

<script type="text/javascript">

//Set you offset here like +5.5 for IST
var offsetIST = 5.5;


//Set you offset here like -8 for PST
var offsetPST = -8;

//Create a new date from the Given string
var d=new Date(Date.parse("14,Feb,2011"));

//To convert to UTC datetime by subtracting the current Timezone offset
var utcdate =  new Date(d.getTime() + (d.getTimezoneOffset()*60000));

//Then cinver the UTS date to the required time zone offset like back to 5.5 for IST
var istdate =  new Date(utcdate.getTime() - ((-offsetIST*60)*60000));

//Then cinver the UTS date to the required time zone offset like back to -8 for PST (Canada US)
var pstdate=  new Date(utcdate.getTime() - ((-offsetPST*60)*60000));

document.write(d);
document.write("<br/>");
document.write(utcdate);
document.write("<br/>");
document.write(istdate);
document.write("<br/>");
document.write(pstdate);
</script>

</body>
</html>

Output:

Mon Feb 14 2011 00:00:00 GMT+0530 (India Standard Time)
Sun Feb 13 2011 18:30:00 GMT+0530 (India Standard Time)
Mon Feb 14 2011 00:00:00 GMT+0530 (India Standard Time)
Sun Feb 13 2011 10:30:00 GMT+0530 (India Standard Time) 

Its writing IST every where because new Date() always show date as local timezone (which is IST for me) but above datetime are actually Original, UTC, IST, PST respectively.

这篇关于使用javascript将日期字符串转换为UTC + 0530格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆