Search K
Appearance
A DateTimeOffsetField
is useful when you need to store or work with a date including its time reference. It’s commonly used in situations where both the date and exact time of an event or action are important. Some examples might be timestamps, transaction records or appointment times. The underlying type of the field is DateTimeOffset
.
INFO
DateTimeOffset is an extension to DateTime that allows us to store timezone information for each record.
In our demo application we use a DateTimeOffset
property CreatedAt
to store when a product record was first added. The property is set automatically upon adding a new product.
public class Product
{
[Key]
public int Id { get; init; }
// Initialize on create
public DateTimeOffset CreatedAt { get; init; } = DateTimeOffset.Now;
}
We use a DateTimeOffsetField
to represent this property in our employee resource. We hide the field on all views that could modify the data.
public override IList<IResourceFieldBuilder> Fields()
{
return
[
DateTimeOffsetField
.Make("Created At", nameof(Product.CreatedAt))
.HideOnCreate()
.HideOnEdit(),
];
}
A DateTimeOffsetField
displays its value formatted according to the browser's language setting (here it's the UK's date format).
In Create and Update views, a user will see a date-time input field.
The default visibility values are as follows:
Resource View | Default |
---|---|
Index | visible |
Create | visible |
Update | visible |
Details | visible |
You can change the visibility of the field for each view. Please note, however, that fields should remain visible on Create pages if the field is required by the entity model and if no default value is set.
A DateTimeOffsetField
can be made sortable by chaining a call to Sortable()
after adding the field. Sorting will be performed chronologically.
public override IList<IResourceFieldBuilder> Fields()
{
return
[
DateTimeOffsetField
.Make("Created At", nameof(Product.CreatedAt))
.HideOnCreate()
.HideOnUpdate()
.Sortable(),
];
}
We provide a default filter for date-time fields. Chain a Filterable()
method call after the field definition. The filter will allow users to filter by a starting date-time and an end date-time.
public override IList<IResourceFieldBuilder> Fields()
{
return
[
DateTimeOffsetField
.Make("Created At", nameof(Product.CreatedAt))
.HideOnCreate()
.HideOnUpdate()
.Filterable(),
];
}