简体   繁体   中英

System.Date in .ics file

I am attaching a .ics file with a email I send to the user. It works fine except the date and time. Currently I get system date and time when I double click on the attached .ics file. I couldn't add my own date columns value which I get from database to the calendar. Really appreciate some help. Here is the contents for the .ics file:

            string calLocation = eventRecordAfterInsert.location;
            string calSubject = eventRecordAfterInsert.eventName;
            string calDescription = "Event Schedule Description";
            DateTime? calDate = eventRecordAfterInsert.eventDt;
            DateTime? calTime = Convert.ToDateTime(row.Cells[11].Text);
            DateTime calEventDateAndTime = calDate.Value.Date + calTime.Value.TimeOfDay;
String[] contents = { "BEGIN:VCALENDAR",
                                    "VERSION:2.0",
                                    "PRODID:-//flo Inc.//FloSoft//EN",
                                    "METHOD:PUBLISH",
                                    "BEGIN:VEVENT",
                                    //"UID:{0}",
                                    "DTSTAMP:" + calEventDateAndTime,
                                    "DTEND:" + calEventDateAndTime,
                                    "Location:"+ calLocation, 
                                    "Description;Encoding=QUOTED-PRINTABLE:" + calSubject,
                                    "Summary:" + calDescription, 
                                    "Priority:3",
                                    "BEGIN:VALARM",
                                    "TRIGGER:-PT15M",
                                    "ACTION:DISPLAY",
                                    "DESCRIPTION:Reminder",
                                    "END:VALARM",
                                    "END:VEVENT", "END:VCALENDAR" };
                System.IO.File.WriteAllLines(Server.MapPath("EventDetails.ics"), contents);

Date strings within an ICS file should be in the yyyyMMddTHHmmssZ format. So instead of:

"DTSTAMP:" + calEventDateAndTime,

You should do:

"DTSTAMP:" + calEventDateAndTime.ToUniversalTime().ToString("yyyyMMddTHHmmssZ"),

Or since you're repeating the value, move it out into another variable before appending it into your string array.

You are also trying to add a DateTime 's time component to another DateTime with a date component, but I don't think it's going to work the way you want it to. You'll have to rethink that approach, perhaps parsing the time component out of the cell and adding that to the date:

DateTime? calDate = eventRecordAfterInsert.eventDt;
TimeSpan calTime = TimeSpan.Parse(row.Cells[11].Text);
DateTime calEventDateAndTime = calDate.Value.Date.Add(calTime);

Or combining the two DateTime structs into third new one:

DateTime calEventDateAndTime = new DateTime(calDate.Value.Year, 
                                            calDate.Value.Month, 
                                            calDate.Value.Day, 
                                            calTime.Value.Hour, 
                                            calTime.Value.Minute, 
                                            calTime.Value.Second);

Also, you aren't checking if calDate or calTime have values either, so I don't see the point in making them Nullable<DateTime> s.

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