-
Notifications
You must be signed in to change notification settings - Fork 0
/
ics.html
174 lines (163 loc) · 5.24 KB
/
ics.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<!DOCTYPE html>
<html>
<head>
<title>ICS generator</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
@font-face {
font-family: "BogFace";
src: url("../fonts/BogFace.woff");
}
body {
font-family: "BogFace", sans-serif;
background-color: #f0f0f0;
color: #333;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}
header {
margin-bottom: 2rem;
}
main {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
form {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
label {
margin-bottom: 0.5rem;
}
input,
textarea {
margin-bottom: 1rem;
}
.submit button {
margin: 0.5rem;
padding: 0.5rem 1rem;
}
#output {
background-color: #fff;
padding: 1rem;
}
</style>
<script>
function setDate() {
const dateInput = document.getElementById("date");
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, "0");
const day = String(now.getDate()).padStart(2, "0");
const date = `${year}-${month}-${day}`;
dateInput.value = date;
}
document.addEventListener("DOMContentLoaded", () => {
const form = document.getElementById("ics-form");
form.addEventListener("submit", (e) => {
e.preventDefault();
const formData = new FormData(form);
const event = formData.get("event");
const description = formData.get("description");
const date = formData.get("date");
const duration = formData.get("duration");
const location = formData.get("location");
const startDate = new Date(date);
// console.log(Intl.DateTimeFormat().resolvedOptions().timeZone);
const isOnlyDate = date.match(/^\d{4}-\d{2}-\d{2}$/);
let endDate = new Date(date);
if (isOnlyDate) {
endDate.setHours(startDate.getHours() + ((duration % 24) + 24));
} else {
endDate.setHours(startDate.getHours() + duration);
}
console.log(startDate, endDate);
function icsDate(date, dateonly = false) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
const hours = String(date.getHours()).padStart(2, "0");
const minutes = String(date.getMinutes()).padStart(2, "0");
return `${year}${month}${day}${
dateonly ? "" : "T" + hours + minutes + "00Z"
}`;
}
const ics = `BEGIN:VCALENDAR
PRODID:-//Google Inc//Google Calendar 70.9054//EN
VERSION:2.0
BEGIN:VEVENT
SUMMARY:${event}
DTSTART${isOnlyDate ? ";VALUE=DATE" : ""}:${icsDate(
startDate,
isOnlyDate
)}
DTEND${isOnlyDate ? ";VALUE=DATE" : ""}:${icsDate(endDate, isOnlyDate)}
DTSTAMP:${icsDate(new Date())}
${location ? `LOCATION:${location}` : ""}
${description ? `DESCRIPTION:${description}` : ""}
END:VEVENT
END:VCALENDAR
`;
// remove leading spaces
const icsClean = ics.replace(/^[ \t]+/gm, "");
const output = document.getElementById("output");
output.textContent = icsClean;
const blob = new Blob([ics], { type: "text/calendar" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "event.ics";
a.click();
URL.revokeObjectURL(url);
});
});
</script>
</head>
<body>
<header>
<h1>ICS File Generator</h1>
</header>
<main>
<!-- girli gig
2024-06-03T19:30
The Wardrobe, Leeds
reminders: 1day, 2day, 4day, 1week
colour: blue -->
<form id="ics-form">
<label for="event">Event (required) </label>
<input type="text" id="event" name="event" />
<label for="event">Description</label>
<textarea type="text" id="description" name="description"></textarea>
<label for="date">Date (required - YYYY-MM-DD[THH:MM])</label>
<div>
<input type="text" id="date" name="date" />
<button type="button" onclick="setDate()">Now</button>
</div>
<label for="duration">Duration (hr)</label>
<input type="number" id="duration" name="duration" />
<label for="location">Location</label>
<input type="text" id="location" name="location" />
<div class="submit">
<button type="submit">Download ICS</button>
<button type="reset">Reset</button>
</div>
</form>
<section class="output">
<pre id="output"></pre>
</section>
</main>
</body>
</html>