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

> 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.

<!-- id: function-date-parts · area: function · stability: stable · html: https://osysharp.com/reference/function/date-parts/ -->

## Summary        {#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](https://osysharp.com/reference/types/duration-and-parts/) `DateOnly` or `TimeOnly`.

## Signature      {#signature}
```osy syntax
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    {#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](https://osysharp.com/reference/function/execution-side/)). For the current instant to read them off, see [Current time (DateTime.UtcNow, DurableClock.Now)](https://osysharp.com/reference/function/current-time/).

### The whole instant as one number — `Date.Ticks`   {#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
}
```

```osy title="the exact answers, pinned" run app=date-parts
[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       {#see-also}
- [Date arithmetic — AddDays, AddMonths, AddYears, AddHours, AddMinutes](https://osysharp.com/reference/function/date-arithmetic/) — adding days/months/years, and its calendar clamping
- [Current time (DateTime.UtcNow, DurableClock.Now)](https://osysharp.com/reference/function/current-time/) — reading the current instant to pull parts from
- [DateTime](https://osysharp.com/reference/types/datetime/) — the `DateTime` type and how it is stored
