[Canonical page](<https://docs.dhtmlx.com/planning-engine/net/api/core/data/moment/>)
[Full documentation index](<https://docs.dhtmlx.com/planning-engine/llms.txt>)


# Moment

A date and time at minute precision, stored as minutes since 1970-01-01 00:00. It has no time zone and no daylight saving time: the value is a wall-clock reading. Every date in the API is a `Moment`, for example [`TaskData.StartDate`](<https://docs.dhtmlx.com/planning-engine/net/api/core/data/task-data.md>) and [`SchedulingConfig.ProjectStart`](<https://docs.dhtmlx.com/planning-engine/net/api/core/data/scheduling-config.md>).

Build one with [`FromYmdHm`](<https://docs.dhtmlx.com/planning-engine/net/api/core/data/moment.md#fromymdhm>), [`FromDateTime`](<https://docs.dhtmlx.com/planning-engine/net/api/core/data/moment.md#fromdatetime>) or the constructor.

## Declaration

```csharp
public readonly record struct Moment(long Minutes) : IComparable<Moment>
```

`Moment` is a value type with value equality: two moments are equal when their `Minutes` are equal. `default(Moment)` is 1970-01-01 00:00, not "no date". Fields that can be unset use `Moment?`.

## Constructors

### Moment(long)

```csharp
public Moment(long Minutes)
```

Creates a moment from a minute count. The value isn't checked.

| Name | Type | Description |
|---|---|---|
| `Minutes` | `long` | Minutes since 1970-01-01 00:00. Negative values are earlier dates. |

## Properties

### Minutes

```csharp
public long Minutes { get; init; }
```

Minutes since 1970-01-01 00:00. Subtract two moments' `Minutes` to get the calendar distance between them in minutes.

## Methods

### ToDateTime

```csharp
public DateTime ToDateTime()
```

Returns the moment as a `DateTime` with `DateTimeKind.Unspecified`.

**Errors:** throws `ArgumentOutOfRangeException` when the moment is outside years 1-9999, which `DateTime` can't hold. Read `Minutes` to handle such dates.

### ToString

```csharp
public override string ToString()
```

Returns the moment as `YYYY-MM-DD HH:MM`, for example `2026-03-02 08:00`. A year outside 0-9999 gets an explicit sign and at least four digits: `+10000`, `-0001`.

**Errors:** throws `InvalidOperationException` when `Minutes` is outside years -262143 to 262142.

### CompareTo

```csharp
public int CompareTo(Moment other)
```

Orders moments chronologically by `Minutes`. Sorted collections such as `SortedDictionary<Moment, T>` use it.

## Operators

```csharp
public static bool operator <(Moment a, Moment b)
public static bool operator >(Moment a, Moment b)
public static bool operator <=(Moment a, Moment b)
public static bool operator >=(Moment a, Moment b)
```

Compare moments chronologically. `==` and `!=` come from the record struct and compare `Minutes`.

## Static methods

### FromYmdHm

```csharp
public static Moment? FromYmdHm(int year, int month, int day, int hour, int minute)
```

Builds a moment from calendar fields.

| Name | Type | Description |
|---|---|---|
| `year` | `int` | Year, -262143 to 262142. |
| `month` | `int` | Month, 1-12. |
| `day` | `int` | Day of the month; must exist in that month (no February 30). |
| `hour` | `int` | Hour, 0-23. |
| `minute` | `int` | Minute, 0-59. |

**Returns:** the moment, or `null` when any part is out of range or the date doesn't exist.

### FromDateTime

```csharp
public static Moment FromDateTime(DateTime value)
```

Builds a moment from the date and time fields of `value`. Seconds and fractions of a second are dropped. `DateTime.Kind` is ignored, so a UTC value is read as is; convert it to the wall-clock time you schedule in first.

| Name | Type | Description |
|---|---|---|
| `value` | `DateTime` | The date and time to convert. |

**Returns:** the moment at the same wall-clock minute.

## Example

```csharp
using Plen;

Moment start = Moment.FromYmdHm(2026, 3, 2, 8, 0)
    ?? throw new ArgumentException("invalid date");

Moment fromClr = Moment.FromDateTime(new DateTime(2026, 3, 2, 17, 30, 45));

Console.WriteLine(start);                         // 2026-03-02 08:00
Console.WriteLine(fromClr);                       // 2026-03-02 17:30
Console.WriteLine(fromClr.Minutes - start.Minutes); // 570
Console.WriteLine(start < fromClr);               // True

DateTime back = start.ToDateTime();               // Kind = Unspecified
Console.WriteLine(Moment.FromYmdHm(2026, 2, 30, 0, 0) is null); // True
```

## Related

- [`TaskData`](<https://docs.dhtmlx.com/planning-engine/net/api/core/data/task-data.md>) - task dates
- [`Worktime`](<https://docs.dhtmlx.com/planning-engine/net/api/core/data/worktime.md>) - calendar date exceptions keyed by `Moment`
- [Records, ids and dates](<https://docs.dhtmlx.com/planning-engine/net/records-ids-and-dates.md>) - dates and the other input types
- [Project data](<https://docs.dhtmlx.com/planning-engine/net/scheduling/project-data.md>) - describing a plan
