Skip to content

Instantly share code, notes, and snippets.

@conficient
Created October 27, 2021 18:48
Show Gist options
  • Save conficient/8ed58874ba0dfdfab88161016a4a3a0c to your computer and use it in GitHub Desktop.
Save conficient/8ed58874ba0dfdfab88161016a4a3a0c to your computer and use it in GitHub Desktop.
A simple ICalendar with no requirement for external libraries
@if(IsValid)
{
<a href=@dataUrl download=@Filename >@ChildContent</a>
}
@code
{
[Parameter] public DateTime? Start { get; set; }
[Parameter] public DateTime? End { get; set; }
[Parameter] public string Summary { get; set; }
[Parameter] public string Description { get; set; }
[Parameter] public RenderFragment ChildContent {get;set;}
[Parameter] public string Filename { get; set; } = "Calendar.ics";
string dataUrl => GetDataUrl();
string GetDataUrl()
{
var text = GetCalendarText();
var data = System.Text.Encoding.UTF8.GetBytes(text);
var base64 = Convert.ToBase64String(data);
return $"data:text/calendar;base64,{base64}";
}
bool IsValid => (Start.HasValue && End.HasValue);
string GetCalendarText()
{
var id = Guid.NewGuid().ToString();
return
$@"BEGIN:VCALENDAR
PRODID:-//BlazorICalendar//NONSGML BlazorICalendar//EN
VERSION:2.0
BEGIN:VEVENT
DESCRIPTION:{Description}
DTEND:{GetDate(End)}
DTSTAMP:{GetDate(DateTime.UtcNow)}Z
DTSTART:{GetDate(Start)}
SEQUENCE:0
SUMMARY:{Summary}
UID:{id}
END:VEVENT
END:VCALENDAR";
}
private string GetDate(DateTime value)
{
return value.ToString("yyyyMMdd\THHmmss");
}
}
@conficient
Copy link
Author

A really simple ICalendar generator with no library imports. Set the Start and End dates and the optional Summary and Description fields.
e.g.

<ICalendarSimple Start="@DateTime.Now.AddHours(1)" End="@DateTime.Now.AddHours(2)" Summary="Example" Filename="Meeting.ics" >Add Meeting to my Calendar</ICalendarSimple>

Warning: this is rough-and-ready, I haven't really checked how the descriptions/summary should be encoded, or how dates and timezones are supported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment