|
| 1 | +<!doctype html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 6 | + <title>tsb — toDatetime</title> |
| 7 | + <style> |
| 8 | + body { font-family: system-ui, sans-serif; max-width: 860px; margin: 40px auto; padding: 0 20px; line-height: 1.6; color: #222; } |
| 9 | + h1 { font-size: 1.8rem; } |
| 10 | + h2 { font-size: 1.2rem; margin-top: 2rem; border-bottom: 1px solid #ddd; padding-bottom: 4px; } |
| 11 | + pre { background: #f6f8fa; border: 1px solid #e1e4e8; border-radius: 6px; padding: 16px; overflow: auto; font-size: 0.85rem; } |
| 12 | + code { font-family: "SFMono-Regular", Consolas, monospace; } |
| 13 | + .tag { display: inline-block; background: #dbeafe; color: #1e40af; border-radius: 4px; padding: 1px 8px; font-size: 0.75rem; font-weight: bold; } |
| 14 | + table { border-collapse: collapse; width: 100%; margin: 1rem 0; } |
| 15 | + th, td { border: 1px solid #ddd; padding: 8px 12px; text-align: left; } |
| 16 | + th { background: #f6f8fa; } |
| 17 | + textarea { width: 100%; font-family: monospace; font-size: 0.85rem; padding: 10px; border: 1px solid #ddd; border-radius: 6px; } |
| 18 | + .output { background: #f0fdf4; border: 1px solid #bbf7d0; border-radius: 6px; padding: 12px; margin-top: 8px; min-height: 40px; white-space: pre; font-family: monospace; font-size: 0.85rem; } |
| 19 | + button { background: #2563eb; color: #fff; border: none; border-radius: 6px; padding: 8px 18px; cursor: pointer; font-size: 0.9rem; margin-top: 8px; } |
| 20 | + button:hover { background: #1d4ed8; } |
| 21 | + a { color: #2563eb; } |
| 22 | + </style> |
| 23 | + </head> |
| 24 | + <body> |
| 25 | + <p><a href="index.html">← tsb playground</a></p> |
| 26 | + <h1>toDatetime <span class="tag">stats</span></h1> |
| 27 | + <p> |
| 28 | + Convert scalars, arrays, or <code>Series</code> values to JavaScript |
| 29 | + <code>Date</code> objects — mirroring |
| 30 | + <a href="https://pandas.pydata.org/docs/reference/api/pandas.to_datetime.html" |
| 31 | + ><code>pandas.to_datetime()</code></a |
| 32 | + >. |
| 33 | + </p> |
| 34 | + |
| 35 | + <h2>Supported input formats</h2> |
| 36 | + <table> |
| 37 | + <tr><th>Format</th><th>Example</th><th>Result</th></tr> |
| 38 | + <tr><td>ISO 8601 date</td><td><code>"2024-03-15"</code></td><td>Mar 15 2024</td></tr> |
| 39 | + <tr><td>ISO 8601 datetime</td><td><code>"2024-03-15T12:00:00Z"</code></td><td>Mar 15 2024 12:00 UTC</td></tr> |
| 40 | + <tr><td>US format (MM/DD/YYYY)</td><td><code>"01/15/2024"</code></td><td>Jan 15 2024</td></tr> |
| 41 | + <tr><td>European (DD-MM-YYYY)</td><td><code>"15-03-2024"</code></td><td>Mar 15 2024</td></tr> |
| 42 | + <tr><td>Compact (YYYYMMDD)</td><td><code>"20240315"</code></td><td>Mar 15 2024</td></tr> |
| 43 | + <tr><td>Unix ms (number)</td><td><code>1710460800000</code></td><td>Mar 15 2024 00:00 UTC</td></tr> |
| 44 | + <tr><td>Unix s (unit="s")</td><td><code>1710460800</code></td><td>Mar 15 2024 00:00 UTC</td></tr> |
| 45 | + <tr><td>Date object</td><td><code>new Date(2024,2,15)</code></td><td>unchanged</td></tr> |
| 46 | + <tr><td>null / undefined / NaN</td><td><code>null</code></td><td><code>null</code></td></tr> |
| 47 | + </table> |
| 48 | + |
| 49 | + <h2>Error handling</h2> |
| 50 | + <table> |
| 51 | + <tr><th>errors=</th><th>Behaviour</th></tr> |
| 52 | + <tr><td><code>"raise"</code> (default)</td><td>Throws <code>TypeError</code> on unparseable input</td></tr> |
| 53 | + <tr><td><code>"coerce"</code></td><td>Returns <code>null</code> on unparseable input</td></tr> |
| 54 | + <tr><td><code>"ignore"</code></td><td>Returns the original value unchanged</td></tr> |
| 55 | + </table> |
| 56 | + |
| 57 | + <h2>Quick examples</h2> |
| 58 | + <pre><code>import { toDatetime, Series } from "tsb"; |
| 59 | + |
| 60 | +// Scalar |
| 61 | +toDatetime("2024-03-15"); // Date: Mar 15 2024 |
| 62 | +toDatetime(1710460800000); // Date from Unix ms |
| 63 | +toDatetime(1710460800, { unit: "s" }); // Date from Unix seconds |
| 64 | +toDatetime(null); // null |
| 65 | +toDatetime("nope", { errors: "coerce" }); // null |
| 66 | +toDatetime("nope", { errors: "ignore" }); // "nope" |
| 67 | + |
| 68 | +// Array |
| 69 | +toDatetime(["2024-01-01", null, "2024-06-15"]); |
| 70 | +// => [Date, null, Date] |
| 71 | + |
| 72 | +// Series |
| 73 | +const s = new Series({ data: ["2024-01-01", "2024-06-15", null] }); |
| 74 | +toDatetime(s); |
| 75 | +// => Series<Date | null> with dtype=datetime</code></pre> |
| 76 | + |
| 77 | + <h2>Python / pandas equivalent</h2> |
| 78 | + <textarea rows="10" readonly> |
| 79 | +import pandas as pd |
| 80 | + |
| 81 | +# Scalar (returns Timestamp or NaT) |
| 82 | +pd.to_datetime("2024-03-15") |
| 83 | +pd.to_datetime(1710460800000, unit="ms") |
| 84 | +pd.to_datetime(None) # NaT |
| 85 | +pd.to_datetime("nope", errors="coerce") # NaT |
| 86 | +pd.to_datetime("nope", errors="ignore") # "nope" |
| 87 | + |
| 88 | +# List |
| 89 | +pd.to_datetime(["2024-01-01", None, "2024-06-15"]) |
| 90 | + |
| 91 | +# Series |
| 92 | +s = pd.Series(["2024-01-01", "2024-06-15", None]) |
| 93 | +pd.to_datetime(s) |
| 94 | + </textarea> |
| 95 | + |
| 96 | + <h2>Live demo</h2> |
| 97 | + <p>Enter a date string or number and click <em>Convert</em>:</p> |
| 98 | + <input id="inp" type="text" value="2024-03-15" style="width:100%;padding:8px;font-size:0.9rem;border:1px solid #ddd;border-radius:6px;" /> |
| 99 | + <button onclick="runDemo()">Convert</button> |
| 100 | + <div class="output" id="out">—</div> |
| 101 | + |
| 102 | + <script type="module"> |
| 103 | + import { toDatetime, Series } from "https://esm.sh/tsb@0.0.1"; |
| 104 | + |
| 105 | + window.runDemo = function () { |
| 106 | + const raw = document.getElementById("inp").value.trim(); |
| 107 | + const out = document.getElementById("out"); |
| 108 | + try { |
| 109 | + const input = /^-?\d+$/.test(raw) ? Number(raw) : raw; |
| 110 | + const result = toDatetime(input, { errors: "coerce" }); |
| 111 | + out.textContent = result === null ? "null" : result.toString(); |
| 112 | + } catch (e) { |
| 113 | + out.textContent = "Error: " + e.message; |
| 114 | + } |
| 115 | + }; |
| 116 | + </script> |
| 117 | + </body> |
| 118 | +</html> |
0 commit comments