Osy#the first language built for agents
Agents firstAgentic appsWorkflowsDurable Execution — built inSecurityTestingThe editorThe UI modelOne program

Reference / Function

Reading a date — Year, Month, Day, Hour, Minute, Second, DayOfWeek, Date

d.Year / Month / Day / Hour / Minute / Second → int · d.DayOfWeek → int (Sunday = 0) · d.Date → DateTime (midnight)

Read a component off a DateTime — its year, month, day, hour, minute, second — or its day of the week (Sunday is 0), or truncate it to midnight with .Date. The same part-readers work on a DateOnly and a TimeOnly. All are pure and run in the browser.

stable2 examples compiled by CIfunctiondatetimedatestdlib

Summary#

Given a DateTime, these read one component out of it: Year, Month, Day, Hour, Minute, Second (each an int), DayOfWeek (an int, with Sunday = 0), and Date (the same instant truncated to midnight). The same readers also work on a TimeSpan, DateOnly, TimeOnly DateOnly or TimeOnly.

Signature#

d.Year / d.Month / d.Day        -> int
d.Hour / d.Minute / d.Second    -> int
d.DayOfWeek                     -> int      // Sunday = 0, Monday = 1, … Saturday = 6
d.Date                          -> DateTime // same date, time set to 00:00:00

Description#

Each reader pulls a single field out of the value. Month is 1-based (January is 1, not 0 — unlike a JavaScript Date), and Day is the day of the month.

DayOfWeek counts from Sunday. Sunday is 0, Monday is 1, up to Saturday at 6 — so 2024-03-15, a Friday, gives 5.

Date truncates to midnight. It returns a DateTime on the same calendar day with the time cleared to 00:00:00, which is how you compare two timestamps "on the same day" or bucket by day.

The same readers work on a DateOnly and a TimeOnly. someDate.Month reads the month off a DateOnly; someTime.Hour reads the hour off a TimeOnly — the part-reader widens to whichever value you give it.

The member syntax d.Month is the everyday spelling; the compiler knows these readers as Date.Year, Date.Month, Date.Day, Date.Hour, Date.Minute, Date.Second, Date.DayOfWeek and Date.Date, and they can also be written in that call form (Date.Month(d)).

All of these are pure functions of the value, so they run in the browser with no round trip (execution side). For the current instant to read them off, see Current time (DateTime.UtcNow, DurableClock.Now).

The whole instant as one number — Date.Ticks#

When you want an instant as a single comparable/​storable number rather than as parts, Date.Ticks(d) answers it — one number that orders the same way the instants do. Use it for an ordering key or a compact stamp, not for arithmetic you could write with the date operators themselves:

```osy title="an instant as one orderable number — and it is a long" syntax long stamp = Date.Ticks(DateTime.UtcNow);


⚠ **It is a `long`, not an `int`** — the tick count passed `int`'s range in 1970, so a variable or a field holding
one must say `long`.

## Examples       {#examples}
```osy title="is a timestamp on a weekend?" test app=date-parts
bool IsWeekend(DateTime d) {
  return d.DayOfWeek == 0 || d.DayOfWeek == 6;   // Sunday is 0, Saturday is 6
}
[Test]
void Date_parts() {
  var d = DateTime.New(2024, 3, 15, 13, 45, 30);
  Assert.Equal(2024, d.Year);
  Assert.Equal(3, d.Month);             // 1-based
  Assert.Equal(15, d.Day);
  Assert.Equal(13, d.Hour);
  Assert.Equal(45, d.Minute);
  Assert.Equal(30, d.Second);
  Assert.Equal(5, d.DayOfWeek);         // Friday — Sunday is 0
  Assert.False(IsWeekend(d));           // Friday is not a weekend…
  Assert.True(IsWeekend(d.AddDays(2))); // …but the Sunday two days later is

  // .Date truncates the time to midnight, same calendar day
  Assert.Equal(0, d.Date.Hour);
  Assert.Equal(15, d.Date.Day);

  // the same readers widen to a DateOnly and a TimeOnly
  Assert.Equal(3, DateOnly.New(2024, 3, 15).Month);
  Assert.Equal(13, TimeOnly.New(13, 45, 30).Hour);
}

See also#

Related

Current time (DateTime.UtcNow, DurableClock.Now)

Read the current instant. `DateTime.UtcNow`/`Today` (and the platform-idiomatic `DurableClock.Now`/…) return the…

Date arithmetic — AddDays, AddMonths, AddYears, AddHours, AddMinutes

Move a DateTime forward or back. AddDays/AddHours/AddMinutes take a fractional amount and are exact. AddMonths and…

DateTime

A date and time. It is a wall-clock value, not an instant on a timeline, so it is never shifted by anybody's timezone…

execution side

Where a function runs. Osy# infers it from the body: a function that reads data runs on the server, a function that…