使用 javascript 将日期字符串转换为 UTC+0530 格式

发布于 2024-10-17 10:03:24 字数 111 浏览 0 评论 0原文

我的日期格式为 14-Feb-2011,但我想将其转换为 Mon Feb 14 10:13:50 UTC+0530 2011 格式。我怎样才能实现这个目标?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

口干舌燥 2024-10-24 10:03:24

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

我尝试了这段代码,它返回了正确的日期(在印度语言环境中)

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

输出:

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>

输出:

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) 

它到处都写着 IST,因为 new Date() 总是将日期显示为本地时区(对我来说是 IST),但上面的日期时间实际上是 原始UTCIST、PST

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.

多情出卖 2024-10-24 10:03:24
var d = new Date("14-Feb-2011");

这将给出输出
2011 年 2 月 14 日星期一 00:00:00 GMT-0500(东部标准时间)

var d = new Date("14-Feb-2011");

this will give an output of
Mon Feb 14 2011 00:00:00 GMT-0500 (Eastern Standard Time)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文