将事件添加到日历而不遵守时间
Adding event to calendar not respecting times
我正在尝试添加一个日历事件,但是开始时间和结束时间都被忽略了,而是将两者都设置为从现在开始的下半小时。例如,现在它是 3:51pm,所以它将 start/end 次设置为 4:00pm。
这是将事件添加到日历的代码:
public static void AddToCalendar (DateTime startDate, DateTime endDate, string title,
string description = null, string location = null) {
var startDateTicks = DateTime.Now.AddHours (3).ConvertToUnixTimestampMillis ();
var endDateTicks = DateTime.Now.AddDays (2).AddHours(2).ConvertToUnixTimestampMillis ();
var intent = new Intent (Intent.ActionInsert);
intent.SetData (CalendarContract.Events.ContentUri);
//intent.PutExtra (CalendarContract.Events.InterfaceConsts.Dtstart, startDateTicks);
//intent.PutExtra (CalendarContract.Events.InterfaceConsts.Dtend, endDateTicks);
intent.PutExtra (CalendarContract.ExtraEventBeginTime, startDateTicks);
intent.PutExtra (CalendarContract.ExtraEventEndTime, endDateTicks);
intent.PutExtra (CalendarContract.EventsColumns.Title, title);
if (!String.IsNullOrEmpty (description))
intent.PutExtra (CalendarContract.EventsColumns.Description, description);
if (!String.IsNullOrEmpty (location))
intent.PutExtra (CalendarContract.EventsColumns.EventLocation, location);
ApplicationContext.Activity.StartActivity (intent);
}
下面是将日期转换为毫秒的扩展方法:
public static double ConvertToUnixTimestampMillis (this DateTime date) {
var epoch = new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return (long)(date.ToUniversalTime () - epoch).TotalMilliseconds;
}
下面是日历事件。
我尝试设置 Dtstart / Dtend,以及 beginTime 和 endTime。那我错过了什么?
日历预计自纪元以来的毫秒数,因此您应该使用类似这样的方法来获取正确的值:
long GetDateTimeMS (int yr, int month, int day, int hr, int min)
{
Calendar c = Calendar.GetInstance (Java.Util.TimeZone.Default);
c.Set (Calendar.DayOfMonth, day);
c.Set (Calendar.HourOfDay, hr);
c.Set (Calendar.Minute, min);
c.Set (Calendar.Month, month);
c.Set (Calendar.Year, yr);
return c.TimeInMillis;
}
然后在您的代码中这样调用它:
intent.PutExtra (CalendarContract.ExtraEventBeginTime, DateTime.Now.AddHours(3));
intent.PutExtra (CalendarContract.ExtraEventEndTime, DateTime.Now.AddDays(2).AddHours(2));
我正在尝试添加一个日历事件,但是开始时间和结束时间都被忽略了,而是将两者都设置为从现在开始的下半小时。例如,现在它是 3:51pm,所以它将 start/end 次设置为 4:00pm。
这是将事件添加到日历的代码:
public static void AddToCalendar (DateTime startDate, DateTime endDate, string title,
string description = null, string location = null) {
var startDateTicks = DateTime.Now.AddHours (3).ConvertToUnixTimestampMillis ();
var endDateTicks = DateTime.Now.AddDays (2).AddHours(2).ConvertToUnixTimestampMillis ();
var intent = new Intent (Intent.ActionInsert);
intent.SetData (CalendarContract.Events.ContentUri);
//intent.PutExtra (CalendarContract.Events.InterfaceConsts.Dtstart, startDateTicks);
//intent.PutExtra (CalendarContract.Events.InterfaceConsts.Dtend, endDateTicks);
intent.PutExtra (CalendarContract.ExtraEventBeginTime, startDateTicks);
intent.PutExtra (CalendarContract.ExtraEventEndTime, endDateTicks);
intent.PutExtra (CalendarContract.EventsColumns.Title, title);
if (!String.IsNullOrEmpty (description))
intent.PutExtra (CalendarContract.EventsColumns.Description, description);
if (!String.IsNullOrEmpty (location))
intent.PutExtra (CalendarContract.EventsColumns.EventLocation, location);
ApplicationContext.Activity.StartActivity (intent);
}
下面是将日期转换为毫秒的扩展方法:
public static double ConvertToUnixTimestampMillis (this DateTime date) {
var epoch = new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return (long)(date.ToUniversalTime () - epoch).TotalMilliseconds;
}
下面是日历事件。
我尝试设置 Dtstart / Dtend,以及 beginTime 和 endTime。那我错过了什么?
日历预计自纪元以来的毫秒数,因此您应该使用类似这样的方法来获取正确的值:
long GetDateTimeMS (int yr, int month, int day, int hr, int min)
{
Calendar c = Calendar.GetInstance (Java.Util.TimeZone.Default);
c.Set (Calendar.DayOfMonth, day);
c.Set (Calendar.HourOfDay, hr);
c.Set (Calendar.Minute, min);
c.Set (Calendar.Month, month);
c.Set (Calendar.Year, yr);
return c.TimeInMillis;
}
然后在您的代码中这样调用它:
intent.PutExtra (CalendarContract.ExtraEventBeginTime, DateTime.Now.AddHours(3));
intent.PutExtra (CalendarContract.ExtraEventEndTime, DateTime.Now.AddDays(2).AddHours(2));