0

I'm trying to create a vCalendar appointment to send to our clients. The date and time is of a clinic appointment. The server that generates the vCal is on Eastern time but we have clients that span the whole country. The time and date is time zone independent because it's set by the clinic they are seeing. I need to send the vCal using the specific date and time and not the date and time adjusted for the time zone. Here is the code i'm using now but no matter what i do it adjusts for time zone on the client end which results in an incorrect time for their appointment.

Private Function CreateCalendarItem(ByVal Mail As System.Net.Mail.MailMessage, ByVal PlainText As String, ByVal ApptDateTime As String) As System.Net.Mail.AlternateView

    Dim ApptDate As DateTime = CDate(ApptDateTime)

    Dim calendarEventText As StringBuilder = New StringBuilder
    calendarEventText.AppendLine("BEGIN:VCALENDAR")
    calendarEventText.AppendLine("METHOD:REQUEST")
    calendarEventText.AppendLine("PRODID:-//MyCompanyName")
    calendarEventText.AppendLine("VERSION:2.0")
    calendarEventText.AppendLine("BEGIN:VEVENT")
    calendarEventText.AppendLine("DTSTART:" & ApptDate.ToUniversalTime.ToString("yyyyMMddTHHmmss"))
    calendarEventText.AppendLine("DTSTAMP:" & ApptDate.ToUniversalTime.ToString("yyyyMMddTHHmmss"))
    calendarEventText.AppendLine("DTEND:" & ApptDate.ToUniversalTime.ToString("yyyyMMddTHHmmss"))
    calendarEventText.AppendLine("UID:" & Guid.NewGuid().ToString)
    calendarEventText.AppendLine("DESCRIPTION:" & PlainText)
    calendarEventText.AppendLine("X-ALT-DESC;FMTTYPE=text/html:" & PlainText)
    calendarEventText.AppendLine("SUMMARY:" & Mail.Subject)
    calendarEventText.AppendLine("ORGANIZER:MAILTO:" & Mail.From.Address)
    If Mail.To.Count > 0 Then
        calendarEventText.AppendLine("ATTENDEE;CN=\" & Mail.To(0).DisplayName & "\;RSVP=FALSE:mailto:" & Mail.To(0).Address)
    Else
        calendarEventText.AppendLine("ATTENDEE;CN=\\;RSVP=FALSE:mailto" & "")
    End If
    calendarEventText.AppendLine("BEGIN:VALARM")
    calendarEventText.AppendLine("TRIGGER:-PT15M")
    calendarEventText.AppendLine("ACTION:DISPLAY")
    calendarEventText.AppendLine("DESCRIPTION:Reminder")
    calendarEventText.AppendLine("END:VALARM")
    calendarEventText.AppendLine("END:VEVENT")
    calendarEventText.AppendLine("END:VCALENDAR")
    Dim ct As System.Net.Mime.ContentType = New System.Net.Mime.ContentType("text/calendar")
    ct.Parameters.Add("method", "REQUEST")
    Dim avCal As System.Net.Mail.AlternateView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(calendarEventText.ToString(), ct)

    Return avCal

End Function

Any help would GREATLY be appreciated. Thank you for your input.

  • Have a look [at this thread](http://stackoverflow.com/questions/1201378/how-does-datetime-touniversaltime-work) on how time works in .NET depending on which version you're running. Apparently [TimeZoneInfo](https://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx) is the way forward. – ourmandave Sep 01 '15 at 11:46
  • Thank you very much for your response. I'm trying to implement TimeZoneInfo but there is no change in that outcome. I still see my reminders adjusted for Time Zones. Dim ApptDate As String = TimeZoneInfo.ConvertTimeToUtc(CType(ApptDateTime, DateTime)).ToString("yyyyMMddTHHmmss") – Jason Hall Sep 01 '15 at 17:41
  • So how are you storing the ApptDateTime? Is that UTC or local time or something else? – ourmandave Sep 01 '15 at 17:46
  • That must be the problem. I'm storing the Appointment date in our database as local time. So there is no way to convert it after the fact? Thanks again for your insight. – Jason Hall Sep 02 '15 at 03:07
  • You'll have the know the time zone the appointment is in and adjust off the difference from Eastern time before you set the vCalendar date. See [TimeZoneInfo.ConvertTime](https://msdn.microsoft.com/en-us/library/bb382770(v=vs.110).aspx). – ourmandave Sep 02 '15 at 11:49
  • That's what i was afraid of. I haven't had a chance to try anything with this yet but i'll let you know if i have any further questions. Thank you very much again. – Jason Hall Sep 03 '15 at 12:39

1 Answers1

0

Use the following:

calendarEventText.AppendLine("DTSTART:" & ApptDate.ToUniversalTime.ToString("yyyyMMddTHHmmssZ"))
calendarEventText.AppendLine("DTSTAMP:" & ApptDate.ToUniversalTime.ToString("yyyyMMddTHHmmssZ"))
calendarEventText.AppendLine("DTEND:"   & ApptDate.ToUniversalTime.ToString("yyyyMMddTHHmmssZ"))

Notice I added 'Z' to the end of the DATE-TIME string format. The 'Z' is how vCalendar file parsers know that the date-time value is UTC time.

See the RFC 5545 Date-Time spec info.

Community
  • 1
  • 1
Justin Steele
  • 2,230
  • 13
  • 16
  • Thank you very much, this particular code was put on hold so I haven't done anything with it in a long time. I do believe that I was originally using the Z in the format but still having issues with it reflecting the correct times in the vCalendar. I will pick this back up for sure and retest with the Z and let you know if it helps to resolve my issue. – Jason Hall Dec 11 '15 at 21:19