Date Only
Last updated
Was this helpful?
Was this helpful?
@Model.EventDate@Model.Value<DateOnly?>("eventDate"){
"date": "2025-01-01T00:00:00+00:00"
}using System.Text.Json.Serialization;
namespace UmbracoProject;
public class DateOnlyValue
{
/// <summary>
/// The date value, represented as a <see cref="DateTimeOffset"/> for storage compatibility.
/// </summary>
[JsonPropertyName("date")]
public DateTimeOffset Date { get; init; }
}DateOnly dateOnly = DateOnly.FromDateTime(DateTime.Today); // Your existing DateOnly value
DateTimeOffset dateTimeOffset = dateOnly.ToDateTime(TimeOnly.MinValue);DateTime dateTime = DateTime.Today; // Your existing DateTime value
DateOnly dateOnly = DateOnly.FromDateTime(dateTime);
DateTimeOffset dateTimeOffset = dateOnly.ToDateTime(TimeOnly.MinValue);DateOnlyValue value = new DateOnlyValue
{
Date = dateTimeOffset
};string jsonValue = _jsonSerializer.Serialize(value);IContent content = _contentService.GetById(contentKey) ?? throw new Exception("Content not found");
// Set the value of the property with alias 'eventDate'.
content.SetValue("eventDate", jsonValue);
// Save the change
_contentService.Save(content);