datetime — Create MATLAB-compatible datetime arrays from components, text, or serial date numbers.
datetime constructs MATLAB-style datetime objects. RunMat currently supports creation from year-month-day component inputs, from text scalars or text arrays, from serial date numbers, and from numeric inputs marked with ConvertFrom='datenum'. The resulting object stores serial dates internally and exposes a writable Format property used by display, string, and char conversion.
How datetime works in RunMat
datetime()with no arguments returns the current local date and time.datetime(Y, M, D)anddatetime(Y, M, D, H, MN, S)construct datetimes from numeric components. Scalar inputs expand to match non-scalar component arrays.datetime(text)accepts string scalars, string arrays, and character vectors. Supported text includes common ISO-like forms,dd-MMM-yyyyforms, andnow.datetime(serials, 'ConvertFrom', 'datenum')interprets numeric values as MATLAB serial date numbers.datetime(serials)also accepts raw serial date numbers for the current implementation, producing datetime values directly from those serials.- The
Formatproperty controls text rendering. Assigningt.Format = 'yyyy-MM-dd HH:mm:ss'updates how the object displays without changing the stored point in time. - Linear indexing
t(k)is supported and returns datetime values. - Comparison operators and
plus/minusday arithmetic are supported. Subtracting two datetime values returns numeric day deltas.
How datetime runs on the GPU
datetime does not allocate GPU objects or invoke provider kernels. If a numeric input originates on the GPU, RunMat gathers it before building the datetime object.
GPU memory and residency
No. RunMat represents datetime values as host-side objects with an internal serial-date tensor and a Format property. Even when the constructor receives gathered numeric data, the resulting object remains resident on the CPU.
Examples
Constructing a scalar datetime from numeric components
t = datetime(2024, 4, 9, 13, 30, 0)Expected output:
t =
09-Apr-2024 13:30:00Parsing text into datetimes
t = datetime(["2024-04-09 13:30:00"; "2024-04-10 08:15:00"])Expected output:
t =
09-Apr-2024 13:30:00
10-Apr-2024 08:15:00Creating datetimes from serial date numbers
d = datetime([739351; 739352], 'ConvertFrom', 'datenum')Expected output:
d =
09-Apr-2024 00:00:00
10-Apr-2024 00:00:00Changing display format without changing the stored value
t = datetime(2024, 4, 9, 13, 30, 0);
t.Format = 'yyyy-MM-dd HH:mm:ss';
disp(t)Expected output:
2024-04-09 13:30:00Extracting calendar and clock components
t = datetime(2024, 4, 9, 13, 30, 5);
y = year(t)
m = month(t)
d = day(t)
h = hour(t)Expected output:
y = 2024
m = 4
d = 9
h = 13Adding days and subtracting datetimes
t0 = datetime(2024, 4, 9);
t1 = t0 + 7;
delta = t1 - t0Expected output:
delta = 7FAQ
What does the Format property change?
Only the textual representation. The stored serial date number is unchanged, so comparisons and arithmetic still refer to the same instant.
How is subtraction interpreted?
Subtracting one datetime from another returns a numeric delta measured in days. Subtracting a numeric value from a datetime shifts it backward by that many days.
Can I index datetime arrays?
Yes. Linear () indexing is supported and preserves the datetime type.
Does datetime run on the GPU?
No. datetime values are represented as host-side objects. Numeric inputs may be gathered first, but the resulting datetime object remains on the CPU.
Related functions to explore
These functions work well alongside datetime. Each page has runnable examples you can try in the browser.
year, month, day, hour, minute, second, string, char, disp
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how datetime works, line by line, in Rust.
- View datetime.rs on GitHub
- Learn how the runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.