简体   繁体   中英

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.

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(); , store 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. For example, for today's date ie 10/03/2016, it'll will give you "10". So please include months as well to make your logic foolproof.

This will work like a charm, Thanks to @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"); 
            }
         } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM