Question

I'm trying to create a .vcs file in C#. Basically in outlook if you add a calendar appointment it creates a file that in outlook looks like this:

enter image description here

You can actually export this file, right click on it and open it up in your favorite text editor. It looks something like this:

BEGIN:VCALENDAR
PRODID:-//Flo Inc.//FloSoft//EN
BEGIN:VEVENT
DTSTART:6/12/2012 12:00:00 PM
DTEND:6/12/2012 1:00:00 PM
LOCATION:Meeting room 1
DESCRIPTION;ENCODING=QUOTED-PRINTABLE:Learn about assets.
SUMMARY:asset management training.
X-MICROSOFT-CDO-BUSYSTATUS:OOF
PRIORITY:5
END:VEVENT
END:VCALENDAR

So what I am having a problem with is the actual time in DTSTART and DTEND from above. You can see that when I open the outlook file it says 11:00am (as the screen shot shows) but in the text file I have it as 12:00pm.

So I have an application (training application) where I dynamically create one of these vcs files. Using C# I gather the subject, location, description and dates (with times) like so:

 protected void btnOutlook_Click(object sender, EventArgs e)
        {
            string location;
            string description;
            string subject;
            string fromTime;
            string toTime;

            location = txtLocation.Text;
            description = txtDescription.Text;
            subject = lblTitle.Text;
            fromTime = ddlFromTimeHH.SelectedItem.Text + ":" + ddlFromTimeMM.SelectedItem.Text + ddlFromTimeAMPM.SelectedItem.Text;
            toTime = ddlToTimeHH.SelectedItem.Text + ":" + ddlToTimeMM.SelectedItem.Text + ddlToTimeAMPM.SelectedItem.Text;

            string begin = lblDate.Text + " " + fromTime;
            string end = lblDate.Text + " " + toTime;
            string format = "dd/MM/yyyy h:mmtt";

            DateTime trainingDateBegin;
            DateTime trainingDateEnd;

            if (DateTime.TryParseExact(begin, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out trainingDateBegin))
            {
                //good date
            }


            if (DateTime.TryParseExact(end, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out trainingDateEnd))
            {
                //good date
            }

            OpenVCSFile("vcsFile.aspx?TrainingDateBegin=" + trainingDateBegin + "&TrainingDateEnd=" + trainingDateEnd + "&Location=" + location + "&Subject=" + subject + "&Description=" + description, "Utilities", "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=800,height=500,left=10,top=20");
        }

So in the above code fromTime just becomes for instance 7:00 am and toTime becomes something like 8:00 am. I then use DateTime.TryParseExact to merge the date with the time so it becomes for instance 06/01/2012 7:00 am for beginDate and for endDate it becomes for instance 06/01/2012 8:00 am.

So far so good...then I just call a function OpenVCSFile which is just some javascript to open the passed in url like so:

protected void OpenVCSFile(string url, string name, string att)
        {
            Response.Write("<script language='JavaScript'>");
            Response.Write("x=window.open('" + url + "', '" + name + "','" + att + "');");
            Response.Write("x.focus();");
            Response.Write("</script>");
        }

This then calls the vcsFile.aspx page where I can fill in the outlook values...

protected void Page_Load(object sender, EventArgs e)
        {
            DateTime beginDate;
            DateTime endDate;
            string location;
            string description;
            string subject;

            beginDate = Convert.ToDateTime(Request.QueryString["TrainingDateBegin"]);
            endDate = Convert.ToDateTime(Request.QueryString["TrainingDateEnd"]);

            location = Request.QueryString["Location"];
            description = Request.QueryString["Description"];
            subject = Request.QueryString["Subject"];

            MemoryStream mStream = new MemoryStream();
            StreamWriter writer = new StreamWriter(mStream);

            writer.AutoFlush = true;

            //header
            writer.WriteLine("BEGIN:VCALENDAR");
            writer.WriteLine("PRODID:-//Flo Inc.//FloSoft//EN");
            writer.WriteLine("BEGIN:VEVENT");

            //BODY
            writer.WriteLine("DTSTART:" + beginDate);   //why dont the times come out right...
            writer.WriteLine("DTEND:" + endDate);       //same here
            writer.WriteLine("LOCATION:" + location);
            writer.WriteLine("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" + description);
            writer.WriteLine("SUMMARY:" + subject);
            writer.WriteLine("X-MICROSOFT-CDO-BUSYSTATUS:OOF");

            //FOOTER
            writer.WriteLine("PRIORITY:5");
            writer.WriteLine("END:VEVENT");
            writer.WriteLine("END:VCALENDAR");

            //MAKE IT DOWNLOADABLE
            Response.Clear(); //clears the current output content from the buffer
            Response.AppendHeader("Content-Disposition", "attachment; filename=AddToOutlookCalendar.vcs");
            Response.AppendHeader("Content-Length", mStream.Length.ToString());
            Response.ContentType = "application/download";
            Response.BinaryWrite(mStream.ToArray());
            Response.End();
        }

Everything appears to work EXCEPT the most important part, the section where I do this:

writer.WriteLine("DTSTART:" + beginDate);   //why dont the times come out right...
 writer.WriteLine("DTEND:" + endDate);       //same here

The date comes out right as you see in the outlook screen shot, but the time is always wrong... Usually outlook will open it up with 10:00 am till 11:00 am. But it never takes the time that I give it. For instance, in my c# code here is the watch screen:

trainingDateBegin {12/6/2012 12:00:00 PM}
trainingDateEnd {12/6/2012 1:00:00 PM}

So my app is passing in the date of 12/6/2012 with a time of 12:00:00 pm till 12/6/2012 1:00:00 pm. But then when the vcs file is generated here is the result:

enter image description here

(if the image doesnt show up basically outlook is having all the correct information: subject, location, start date end date BUT the time is wrong. It says 11am till 12pm. Its almost like its using my system clock EST)...

Does anyone know what I might be doing wrong. Sorry for the long post:(.

Was it helpful?

Solution

I think it's to do with timezones; you could either add a timezone or (much simpler) use the UTC time in your file.

If you do add a timezone you'll need to use vCal version 2, since Outlook doesn't support timezones in version 1.


EDIT: This is an example of the syntax taken from the Wikipedia article on VCalendar. This event occurs July 14, 1997 17:00 (UTC) through July 15, 1997 03:59:59 (UTC):

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
BEGIN:VEVENT
UID:uid1@example.com
DTSTAMP:19970714T170000Z
ORGANIZER;CN=John Doe:MAILTO:john.doe@example.com
DTSTART:19970714T170000Z
DTEND:19970715T035959Z
SUMMARY:Bastille Day Party
END:VEVENT
END:VCALENDAR
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top