2

I'm working on a reminders application using Phonegap [Javascript + Html5] in which the user enters the weekly task he wants to be reminded of and I'm supposed to alert a notification every week on that day.

Now the point is: I use Phonegap Local Notification Plugin to alert the notifications, this plugin takes a date to alert on time.

How can I give the needed date everyweek .. i.e: How Can I increment the counter when the day ends ?

Usually if the date if preknown: I use this function:

 if (typeof plugins !== "undefined")
                       {
                        var RId = 0;
                        var rDate =new Date();
                        var RemDate = reminder_deadline.split("T")[0];
                        var RemTimeB = reminder_deadline.split("T")[1];
                        var RemTime = RemTimeB.split("Z")[0];
                        var RYear = RemDate.split("-")[0];
                        var RMonth = RemDate.split("-")[1];
                        var RMonth = RMonth-1;
                        var RDay = RemDate.split("-")[2];
                        var RHour = RemTime.split(":")[0];
                        var RMinute = RemTime.split(":")[1];
                        var RSecond = RemTime.split(":")[2];
                        alert(RYear+".."+RMonth+".."+RDay+".."+RHour+".."+RMinute+".."+RSecond);
                        rDate.setFullYear(RYear);
                        rDate.setMonth(RMonth);
                        rDate.setDate(RDay);
                        rDate.setHours(RHour);
                        rDate.setMinutes(RMinute);
                        rDate.setSeconds(RSecond);
                        plugins.localNotification
                        .add({ 
                            date: rDate,
                            message: reminder_name, 
                            id: RId
                             });
                             }
                             RId++;
             }

But now the user will just enter Monday ... & I'm supposed to notify him every Monday ... So is that possible to be made ?

4

1 回答 1

1
var today = new Date();
var nextWeek = new Date();
nextWeek.setDate(nextWeek.getDate() + 7);

这是上周一给你的一个片段。如果您需要任何其他日期,只需var daysOffMonday相应地编辑该行(0=星期日,1=星期一,...)

然后使用上面的代码段,您可以获取下一个星期一,以及旁边的那个,等等。

​var today = new Date();

​var dayOfWeek = today.getDay();
var daysOffMonday = 1 - dayOfWeek;
var lastMonday = new Date();
lastMonday.setDate(lastMonday.getDate() + daysOffMonday);

​alert(lastMonday);​
于 2012-12-12T09:24:51.453 回答