简体   繁体   English

如何检测一天中首次启动的应用

[英]How to detect app launched first time in a day

I have app, whenever it launched first time in day then I should execute some logic. 我有应用程序,只要它在一天中的第一时间启动,就应该执行一些逻辑。 I have checked in google but I didnt get any good working logic, If anybody knows this, pls suggest me. 我已经在Google中签到了,但是我没有得到任何好的工作逻辑,如果有人知道这一点,请建议我。

till now I have tried with this code 到目前为止,我已经尝试过使用此代码

       var firstTime = new Date().getTime();
       if(window.localStorage.getItem('firstTime') == null){
            window.localStorage.setItem('firstTime', firstTime);
        }else{
            var secondTime = new Date().getTime();
            var storedTime = window.localStorage.getItem('firstTime');
            if(secondTime > storedTime){
                alert("Second time");
            }else{
                alert("First time");
            }
        }

Note that in: 注意在:

var firstTime = new Date().getTime();
if(window.localStorage.getItem('firstTime') == null){
    window.localStorage.setItem('firstTime', firstTime);

you are storing a number that represents the current moment to the millisecond. 您存储的数字代表当前时刻(以毫秒为单位)。 If you want the start of the day, set the hours to zero first: 如果要开始新的一天,请先将小时数设置为零:

var firstTime = new Date().setHours(0,0,0,0);

Now do the logic, storing the time value is fine: 现在执行逻辑,存储时间值就可以了:

if (window.localStorage.getItem('firstTime') == null){
    window.localStorage.setItem('firstTime', firstTime);

In the else part, you need to compare to another time value for the start of the day, just like above: 在其他部分,您需要与一天中的另一个时间值进行比较,就像上面这样:

} else {

  var storedTime = window.localStorage.getItem('firstTime');

  // Get a new date, zero it as above and see if its the same time
  // If not, it must be a different day
  var secondTime = new Date().setHours(0,0,0,0)

  if (secondTime != +firstTime) {
    alert("Second time is a different day");

  } else {
    alert("First time is the same day");
  }
}

Instead of storing new Date().getTime(); 而不是存储new Date().getTime(); , store new Date().getDate(); ,存储new Date().getDate(); and do the comparison. 并进行比较。 If current date is greater than the stored date, the app is being opened for the first time on that day. 如果当前日期大于存储的日期,则该应用程序在该天首次打开。 So you store this date. 因此,您存储此日期。 And when the next time app is opened, current date won't be greater (it'll be equal) and hence you'll understand that it's not the first time. 而且,当下次打开应用程序时,当前日期不会更长(它将是相等的),因此您会了解到这不是第一次。

Note: getDate() will only give you the day part of the date. 注: getDate()只会给你的日期的部分。 For example, for today's date ie 10/03/2016, it'll will give you "10". 例如,对于今天的日期(即10/03/2016),它将为您提供“ 10”。 So please include months as well to make your logic foolproof. 因此,请同时加上几个月,以使您的逻辑万无一失。

This will work like a charm, Thanks to @camelcasecoder 这将像一种魅力一样工作,感谢@camelcasecoder

       var firstTime = new Date().setHours(0,0,0,0);
        alert("firstTime "+firstTime);
        if (window.localStorage.getItem('firstLaunch') == null){
            window.localStorage.setItem('firstLaunch', firstTime);
            alert("At first time");
        } else {    
            var storedTime = window.localStorage.getItem('firstLaunch');
              // Get a new date, zero it as above and see if its the same time
              // If not, it must be a different day
            var secondTime = new Date().setHours(0,0,0,0)
            alert("secondTime "+secondTime);
            if (secondTime > storedTime) { 
                alert("First time in the day");
                window.localStorage.setItem('firstLaunch', secondTime); 
            } else { 
                alert("Second time in the same day"); 
            }
         } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 首次启动应用程序时未加载JavaScript - JavaScript doesn't load when app launched for the first time 如何在 Web 应用 Javascript 上检测用户首次登录 Firebase - How to Detect User First Time Logging in Firebase on Web App Javascript react native:有办法检测是否是第一次启动应用程序? - react native : There is way to detect if it is the first time launch of the app or not? 我能否检测我的 PWA 是作为应用程序启动还是作为网站访问? - Can I detect if my PWA is launched as an app or visited as a website? 如何检测HTML5时 <video> 是第一次玩? - How to detect when a HTML5 <video> is played for the first time? 如何在用户首次登录时以及第一次加载特定页面时检测到? - How do I detect the first time the user logs in and the first time a specific page is loaded? 如何在反应原生应用程序中检测应用程序首次启动? - How to detect application first launch in react native app? 如何实现本地存储以检测安装后应用的首次运行? - how to implement localstorage to detect app's first run after installation? 如何在moment js中检测夏令时并转换为BST(当地时间) - How to detect day light saving and convert to BST (local time) in moment js Moment.js - 如何检测夏令时并添加一天 - Moment.js - How To Detect Daylight Savings Time And Add One Day
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM