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
/** @jsxImportSource npm:react */
import { renderToString } from "npm:react-dom@18/server";
import React from "npm:react@18";
const clientScript = `
import { parse } from 'https://esm.sh/chrono-node@2.3.8';
const inputEl = document.getElementById('dateInput');
const outputEl = document.getElementById('output');
const clearBtn = document.getElementById('clearBtn');
function detectDate() {
const inputText = inputEl.value;
const parsedResults = parse(inputText);
if (parsedResults.length > 0) {
const detectedDate = parsedResults[0].start.date();
const detectedDateString = parsedResults[0].text;
const formattedDate = formatDate(detectedDate);
outputEl.innerHTML = \`
<div class="flex space-x-2">
<span class="bg-gray-800 text-white px-2 py-1 rounded inline-flex items-center text-sm">
\${formattedDate}
</span>
<span class="border border-gray-800 text-gray-800 px-2 py-1 rounded inline-flex items-center text-sm">
\${detectedDateString}
</span>
</div>
\`;
inputEl.value = ''; // Clear the input after detection
} else {
outputEl.innerHTML = '';
}
}
function clearAll() {
inputEl.value = '';
outputEl.innerHTML = '';
inputEl.focus();
}
function formatDate(date) {
if (!date) return '';
return date.toLocaleString('en-US', {
month: '2-digit',
day: '2-digit',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
hour12: true,
});
}
inputEl.addEventListener('input', detectDate);
clearBtn.addEventListener('click', clearAll);
`;
const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI Date Input</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }
</style>
</head>
<body class="bg-white">
<div class="max-w-md mx-auto p-4">
<h1 class="text-2xl font-bold mb-4">AI Date Input</h1>
<div class="relative mb-4">
<input
id="dateInput"
type="text"
class="w-full p-2 pr-8 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-gray-500"
placeholder="Enter a date or time..."
/>
<button
id="clearBtn"
class="absolute right-2 top-1/2 transform -translate-y-1/2 text-gray-400 hover:text-gray-600"
style="font-size: 1.2em;"
>
×
</button>
</div>
<div id="output" class="mt-2"></div>
<p class="mt-4 text-sm text-gray-600">
Try typing things like "tomorrow", "next Friday", "3 days from now", or "2024-09-15 14:30".
</p>
<div class="mt-8 text-xs text-gray-500">
<a href="${import.meta.url.replace("esm.town", "val.town")}" target="_blank" rel="noopener noreferrer" class="underline">
View Source
</a>
</div>
</div>
<script type="module">