[英]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.