dateshift — Shift datetime values to calendar boundaries or weekday occurrences.
dateshift implements the public start, end, and day-of-week forms with singular calendar units and optional current, next, previous, nearest, or integer occurrence rules. Week calculations begin on Sunday. Weekday indices and integer rules are read from authoritative integer storage before calendar evaluation.
Syntax
out = dateshift(t, boundary, unit, rule)
out = dateshift(t, 'dayofweek', weekday, rule)Inputs
t- Type
Any- Required
- Yes
- Description
- Host datetime value; numeric serials and relative text are not converted.
boundary- Type
StringScalar- Required
- Yes
- Description
- 'start' or 'end'.
unit- Type
StringScalar- Required
- Yes
- Description
- Singular year, quarter, month, week, day, hour, minute, or second.
rule- Type
Any- Required
- No
- Default
'current'- Description
- current, next, previous, nearest, or an exact integer scalar/array occurrence offset.
t- Type
Any- Required
- Yes
- Description
- Host datetime scalar or array.
boundary- Type
StringScalar- Required
- Yes
- Default
'dayofweek'- Description
- Day-target selection.
weekday- Type
Any- Required
- Yes
- Description
- A scalar weekday name, weekday/weekend group, or exact numeric indices 1..7.
rule- Type
Any- Required
- No
- Default
'next'- Description
- current, next, previous, nearest, or exact integer occurrence controls; scalar or same size.
Returns
out- Type
Any- Description
- Host datetime object, preserving input Format and exact non-scalar shape under scalar expansion, including empty geometry.
Errors
RunMat:datetime:InvalidInput- When
- Input values cannot be parsed, converted, or broadcast to a valid datetime result.
- Message
datetime: invalid input
RunMat:datetime:InvalidArgument- When
- Arguments or option grammar do not match supported datetime forms.
- Message
datetime: invalid argument
RunMat:dateshift:TooManyOutputs- When
- More than one output is requested.
- Message
dateshift: too many output arguments
Behavior
dateshift(t, 'start', unit) and dateshift(t, 'end', unit) default to the current unit. Supported singular units are year, quarter, month, week, day, hour, minute, and second.
The end of a year, quarter, month, or week is midnight at the beginning of its last day. The end of a day, hour, minute, or second is exactly the start of the next unit.
dateshift(t, 'dayofweek', dow) accepts weekday names or numeric indices from 1 for Sunday through 7 for Saturday. weekday and weekend select the corresponding day groups.
Rules can be current, next, previous, nearest, or integer scalar/arrays. Datetime, weekday, and rule arrays must have matching sizes or be scalar; scalar expansion preserves the non-scalar input shape.
Plural unit aliases, nearest as a boundary token, and a custom week-start argument are rejected because they are not part of this public contract.
There is no resident datetime output. Resident arguments are accepted only through the declared RunMat extension and are gathered to a host result.
Placement and effects
dateshift operates on host datetime objects. Resident arguments are compatibility-gated before provider access.
No. Inputs and outputs are host datetime objects. A compatibility-gated resident input is gathered before calendar evaluation.
No GPU kernel is dispatched. MATLAB-compatible mode rejects resident arguments before provider access; RunMat extension mode gathers them and returns a host object.
This pure host operation reads stored datetime serials, not Clock. It may throw or suspend at admitted provider gathers and cooperative checkpoints. Resident controls follow the declared datetime input extension; terminal provider and cancellation errors retain their control-flow authority. There is no device result, GPU kernel, fusion route, or distributed support. Portable Runtime declarations are not backend execution evidence.
Syntax and argument roles
out = dateshift(t, boundary, unit, rule)
out = dateshift(t, 'dayofweek', weekday, rule)
t must be a datetime object, not relative text or a numeric serial. boundary and calendar unit are scalar text. Unit names are singular and case-insensitive. Day targets additionally accept sun/sunday, mon/monday, tue/tues/tuesday, wed/wednesday, thu/thur/thurs/thursday, fri/friday, and sat/saturday.
The optional rule defaults to current for start/end and next for dayofweek. Numeric weekday and rule controls accept real single/double and all eight native integer classes, with exact integer validation. Weekdays are 1..7; rules must fit int64. Day-target occurrences are additionally bounded to magnitude 200000000 before checked whole-week arithmetic.
There is exactly one output. The input Format is preserved. Scalar expansion preserves the full non-scalar shape, including empty arrays; multiple non-scalar shapes must match exactly.
Current implementation frontier
Calendar arithmetic retains the existing signed serial-date conversion, supported Chrono calendar range, leap-day handling, and binary64 serial-day precision. Nonfinite stored serial elements pass through unchanged. Absolute typed datetime operations require no ambient calendar or Clock fallback.
Empty datetime or numeric controls now preserve their full scalar-expanded empty geometry instead of being mistaken for one output element. Calendar output allocation and copied control vectors are not yet generally managed/cooperatively copied; independent MATLAB parity and complete backend qualification remain incomplete.
Finite execution frontier
Finite Managed calendar work is currently unsupported: the canonical host-allocation policy refuses before gathers, calendar scratch, or output publication. An initially unbounded call is checked again after suspension. Full managed scratch/output ownership remains an explicit implementation obligation; this safety fence is not Managed completion.
Examples
Shift to the Sunday at the start of the current week
out = dateshift(datetime(2024, 3, 14), 'start', 'week');Shift to the next Monday occurrence
out = dateshift(datetime(2024, 3, 14), 'dayofweek', 'Monday');Shift to the end of the current month
out = dateshift(datetime(2024, 2, 10), 'end', 'month');Using dateshift with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how dateshift changes the result.
Run a small dateshift example, explain the result, then change one input and compare the output.
FAQ
When does a week begin?⌄
Sunday, matching the compatibility contract used by MATLAB calendar weeks.
Does end of month mean the last millisecond?⌄
No. For month, quarter, year, and week, it is midnight at the beginning of the final calendar day.
Related Datetime functions
Calendar Duration
caldays · calendarDuration · calendarDuration.eq · calendarDuration.minus · calendarDuration.ne · calendarDuration.plus · calmonths · calquarters · calweeks · calyears · datetick · iscalendarduration · isdatetime
Construction
Operators
datetime.eq · datetime.ge · datetime.gt · datetime.le · datetime.lt · datetime.minus · datetime.ne · datetime.plus
Indexing
Duration
days · duration · duration.eq · duration.ge · duration.gt · duration.le · duration.lt · duration.minus · duration.ne · duration.plus · duration.subsasgn · duration.subsref · hours · isduration · milliseconds · minutes · seconds · years
Source you can read
Every RunMat function's source is public. Read exactly how dateshift is executed, line by line, in Rust.
- View the source for dateshift in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.