Time Only
Last updated
Was this helpful?
Was this helpful?
@Model.StartHours@Model.Value<TimeOnly?>("startHours"){
"date": "0001-01-01T14:30:00+00:00"
}using System.Text.Json.Serialization;
namespace UmbracoProject;
public class TimeOnlyValue
{
/// <summary>
/// The time value, represented as a <see cref="DateTimeOffset"/> for storage compatibility.
/// </summary>
[JsonPropertyName("date")]
public DateTimeOffset Date { get; init; }
}TimeOnly timeOnly = TimeOnly.FromDateTime(DateTime.Now); // Your existing TimeOnly value
DateTimeOffset dateTimeOffset = new DateTimeOffset(DateOnly.MinValue, timeOnly, TimeSpan.Zero);DateTime dateTime = DateTime.Now; // Your existing DateTime value
TimeOnly timeOnly = TimeOnly.FromDateTime(dateTime);
DateTimeOffset dateTimeOffset = new DateTimeOffset(DateOnly.MinValue, timeOnly, TimeSpan.Zero);TimeOnlyValue value = new TimeOnlyValue
{
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 'startHours'.
content.SetValue("startHours", jsonValue);
// Save the change
_contentService.Save(content);