使用 React Native 获取时区 [英] Get the time zone with React Native

查看:37
本文介绍了使用 React Native 获取时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取电话用户的时区,以便我可以将其添加或减去我从 API 获得的 UTC 时间,然后对它们进行操作(例如,用于通知).

I'd like to get the timezone of the phone user so I can add or substract it to the UTC times I get from my API, and then make operations on them (for notifications, for example).

我只找到了如何使用DatePickerIOS添加偏移量,但没有其他...是否可以从前面获取它?

I only found how to add an offset with DatePickerIOS, but nothing else... Is it possible to get it from the front?

推荐答案

几乎任何你可以在浏览器上用 JavaScript 做的事情(当然,除了例外),你可以在 React Native 中做.我会启动一个新的日期对象并在其上调用 getTimezoneOffset() 以获取时区在本地偏移的分钟数.

Pretty much anything you could do in JavaScript on the browser (with exception, of course), you can do in React Native. I would initiate a new date object and call getTimezoneOffset() on it to get the number of minutes the timezone is offset locally.

var date = new Date();
var offsetInHours = date.getTimezoneOffset() / 60;

这篇关于使用 React Native 获取时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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