代码改自:https://blog.csdn.net/zackerzhuang
这位博主,可以配合她的小目标,非常好用。
更新点
1、原本无时间的提醒事项全部堆积在0:00-1:00,这样下滑很容易忘掉,现改为全天,时间轴下拉不遗漏
2、全天性质(无时间有日期)的日程,如果没有做,依旧会推移到第二天全天
3、全天重复性待办,今天打卡结束,会进入第二天全天列
2、优化了图标,更加清淡(个人口味)
3、图示app是calendar和原生日历
优化难点:
1、ios自带的new date 返回的时间,是无法进行等于比较的,,虽然数据类型是Number,单=和!= 无论等不等都返回true。添加if 分支遭到阻碍。
2、新建的无时间的提醒事项,getHours,getSeconds是空,同步一次之后会变成00:00:00
所以,同样是无时间的提醒,背后判定不一样。
优化遗憾:
此代码逻辑下,0点这个时间段的待办会被同步到全天(就是最上面),但点击完成后,仍然可以回归到时间轴。
图示:配合第三方简直完美
原生日历:全天比较窄
代码如下
var dur_month = 3
//设置同步 同步时间为前后3个月
const startDate = new Date()
startDate.setMonth(startDate.getMonth() - dur_month)
console.log(`日历的开始时间 ${startDate.toLocaleDateString()}`)
const endDate = new Date()
endDate.setMonth(endDate.getMonth() + dur_month)
console.log(`日历的结束时间 ${endDate.toLocaleDateString()}`)
const reminders = await Reminder.allDueBetween(startDate, endDate)
console.log(`获取 ${reminders.length} 条提醒事项`)
var calendar = await Calendar.forEvents()
var m_dict = {}
for(cal of calendar)
{
m_dict[cal.title] = cal
//console.log(`日历:${cal.title}`)
}
const events = await CalendarEvent.between(startDate, endDate, calendar)
console.log(`获取 ${events.length} 条日历`)
for (const reminder of reminders) {
//reminder的标识符
const targetNote = `[Reminder] ${reminder.identifier}`
const [targetEvent] = events.filter(e => e.notes != null && (e.notes.indexOf(targetNote) != -1))//过滤重复的reminder
if(!m_dict[reminder.calendar.title])
{
console.warn("找不到日历"+ reminder.calendar.title)
continue
}
if (targetEvent) {
//console.log(`找到已经创建的事项 ${reminder.title}`)
updateEvent(targetEvent, reminder)
} else {
console.warn(`创建事项 ${reminder.title} 到 ${reminder.calendar.title}`)
const newEvent = new CalendarEvent()
newEvent.notes = targetNote + "\n" + reminder.notes//要加入备注
//newEvent.notes =reminder.notes
updateEvent(newEvent, reminder)
}
}
Script.complete()
function updateEvent(event, reminder) {
event.title = `${reminder.title}`
cal_name = reminder.calendar.title
cal = m_dict[cal_name]
event.calendar = cal
//console.warn(event.calendar.title)
//已完成事项
if(reminder.isCompleted)
{
event.title = `✔️${reminder.title}`
event.isAllDay = false
event.startDate = reminder.completionDate
var ending = new Date(reminder.completionDate)
ending.setHours(ending.getHours()+1) //默认时长一小时
event.endDate = ending
var period = (reminder.dueDate-reminder.completionDate)/1000/3600/24
period = period.toFixed(1)
if(period < 0)
{
period = -period
event.location = " 延期" + period + "天完成"
}
else if (period == 0)
{
event.location = " 准时完成"
}
else
{
event.location = " 提前" + period + "天完成"
}
}
//未完成事项
else{
const nowtime = new Date()
var period = (reminder.dueDate-nowtime)/1000/3600/24
period = period.toFixed(1)
//console.log(reminder.title+(period))
if(period < 0)
{
//待办顺延
event.location = " 延期" + (-period) + "天"
//如果不是在同一天,设置为全天事项,也就是昨天及之前之前没做完的进全天事项
if(reminder.dueDate.getDate() != nowtime.getDate())
{
event.title = `✖️${reminder.title}`
event.startDate = nowtime
event.endDate = nowtime
event.isAllDay = true
console.log(`【${reminder.title}】成功进入全天【${reminder.dueDate}】待办同步`)
}
//今天 但过了同步时间还没做完的
else
{
// 没有时间的提醒事项 进入当天的全天
var t = new Date(reminder.dueDate)
var tt =t.getHours()
var t3=t.getMinutes()
var t4 = t.getSeconds()
if(Number(tt)<24 && tt>0 ){
event.title = `◻️${reminder.title}`
event.isAllDay = false
event.startDate = reminder.dueDate
var ending = new Date(reminder.dueDate)
ending.setHours(ending.getHours()+1)
event.endDate = ending
// console.log(`【${reminder.title}】将新建的具体时间【${reminder.dueDate}】待办同步【${reminder.dueDate.getHours()}】`)
}
//有时间的提醒事项进入当天时间轴
else
{
event.title = `◻️${reminder.title}`
event.startDate = nowtime
event.endDate = nowtime
event.isAllDay = true
console.log(`【${reminder.title}】将新建的全天待办同步`)
}
}
console.log(`【${reminder.title}】待办顺延${-period}天` )
}
else
{
var flagtime2 = new Date(reminder.dueDate)
// flagtime2.setHours(flagtime2.getHours())
var ss=flagtime2.getHours()
if(Number(ss) <23 && ss >0)
{
event.isAllDay = false
event.title = `◻️${reminder.title}`
event.location = "还剩" + period + "天"
event.startDate = reminder.dueDate
var ending = new Date(reminder.dueDate)
ending.setHours(ending.getHours()+1)
event.endDate = ending
console.log(`【${reminder.title}】. ${reminder.dueDate.getHours()}五天` )
}
else
{
event.title = `◻️${reminder.title}`
event.startDate = reminder.dueDate
event.endDate = reminder.dueDate
event.isAllDay = true
event.location = "还剩" + period + "天"
console.log(`【${reminder.title}】待办${flagtime2.getHours()}` )
// var ss=flagtime2.getHours()
// var sssss=typeof ss
// console.log(sssss)
}
}
}
event.save()
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)