最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - I get bug when I access Local time - react native - Stack Overflow

programmeradmin0浏览0评论

I am using react native to develop an app that fetches usage time from third party apps in device. I am fetching usage time from midnight 12am to current time. But the usage time I got is a bug. The following is my code section where I set the start time to midnight 12 am and end date to current time

 const startDate = new Date();
 startDate.setUTCHours(18,15,0,0); // Set to midnight in UTC
 startDate.setUTCDate(startDate.getUTCDate() - 1);//This is used to set date to previous day as I am currently in Nepal and NST is UTC+5:45
 const endDate= new Date().

I am using @brighthustle/react-native-usage-stats-manager package. The usage time I received is far from similar to digital wellbeing. Is there some issue fetching the local time of device?

I am using react native to develop an app that fetches usage time from third party apps in device. I am fetching usage time from midnight 12am to current time. But the usage time I got is a bug. The following is my code section where I set the start time to midnight 12 am and end date to current time

 const startDate = new Date();
 startDate.setUTCHours(18,15,0,0); // Set to midnight in UTC
 startDate.setUTCDate(startDate.getUTCDate() - 1);//This is used to set date to previous day as I am currently in Nepal and NST is UTC+5:45
 const endDate= new Date().

I am using @brighthustle/react-native-usage-stats-manager package. The usage time I received is far from similar to digital wellbeing. Is there some issue fetching the local time of device?

Share Improve this question edited Feb 3 at 12:43 Susav Karki asked Feb 3 at 12:42 Susav KarkiSusav Karki 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 1

Well, as far as I can see it looks like the issue might be related to how you're setting the time and handling time zone offsets. Like you're working with Nepal Standard Time (NST, UTC+5:45), but using UTC methods (setUTCHours, setUTCDate), the conversion could be causing discrepancies in the times you're calculating.

Try this:

const startDate = new Date(); 
startDate.setUTCHours(18,15,0,0); // Set to midnight in UTC
startDate.setUTCDate(startDate.getUTCDate() - 1); // This is used to set date to previous day as I am in Nepal and NST is UTC+5:45 

const endDate = new Date();
发布评论

评论列表(0)

  1. 暂无评论