admin管理员组文章数量:1287137
I am getting this "2019-05-05T10:30:00Z"
type of time from an API. But I need to convert it like this "10:30 AM (3:30 PM Your Time)"
. I am using reactJs for my client side. How can I convert the time as user current timezone?
I am getting this "2019-05-05T10:30:00Z"
type of time from an API. But I need to convert it like this "10:30 AM (3:30 PM Your Time)"
. I am using reactJs for my client side. How can I convert the time as user current timezone?
- 2 Possible duplicate of Display date/time in user's locale format and time offset – AlexMA Commented May 23, 2019 at 18:31
3 Answers
Reset to default 6
const dateToTime = date => date.toLocaleString('en-US', {
hour: 'numeric',
minute: 'numeric'
});
const dateString = "2019-05-05T10:30:00Z";
const userOffset = new Date().getTimezoneOffset()*60*1000;
const localDate = new Date(dateString);
const utcDate = new Date(localDate.getTime() + userOffset);
console.log(`${dateToTime(utcDate)} (${dateToTime(localDate)} Your Time)`);
take a look at momentjs. it has localization and you can do also custom things.
https://momentjs.
Your API send server DateTime in response, you can just use below function that can convert to your local time zone.
function convertUTCDateToLocalDate(date) {
var dateLocal = new Date(date);
var newDate = new Date(dateLocal.getTime() - dateLocal.getTimezoneOffset()*60*1000);
return newDate;
}
本文标签: reactjsHow to convert a time to user local timezone in javascriptStack Overflow
版权声明:本文标题:reactjs - How to convert a time to user local timezone in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741280549a2369980.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论