Skip to content

Commit b0b942a

Browse files
committed
Updated code samples
1 parent 995b8e4 commit b0b942a

File tree

4 files changed

+65
-23
lines changed

4 files changed

+65
-23
lines changed

16/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/date-only.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,35 @@ The property editor handles date-only values. The time is automatically set to 0
7979
public DateTimeOffset Date { get; init; }
8080
}
8181
```
82+
2. Convert your existing date value to `DateTimeOffset` for storage.
83+
84+
If you have a `DateOnly`:
85+
```csharp
86+
DateOnly dateOnly = DateOnly.FromDateTime(DateTime.Today); // Your existing DateOnly value
87+
DateTimeOffset dateTimeOffset = dateOnly.ToDateTime(TimeOnly.MinValue);
88+
```
89+
90+
If you have a `DateTime`:
91+
```csharp
92+
DateTime dateTime = DateTime.Today; // Your existing DateTime value
93+
DateOnly dateOnly = DateOnly.FromDateTime(dateTime);
94+
DateTimeOffset dateTimeOffset = dateOnly.ToDateTime(TimeOnly.MinValue);
95+
```
8296

83-
2. Create an instance of the class with the desired values.
97+
3. Create an instance of the class with the `DateTimeOffset` value.
8498
```csharp
85-
var value = new DateOnlyValue
99+
DateOnlyValue value = new DateOnlyValue
86100
{
87-
Date = new DateTimeOffset(DateTime.Today) // The value to store as a `DateTimeOffset`
101+
Date = dateTimeOffset
88102
};
89103
```
90-
3. Inject the `IJsonSerializer` and use it to serialize the object.
104+
105+
4. Inject the `IJsonSerializer` and use it to serialize the object.
91106
```csharp
92-
var jsonValue = _jsonSerializer.Serialize(value);
107+
string jsonValue = _jsonSerializer.Serialize(value);
93108
```
94-
4. Inject the `IContentService` to retrieve and update the value of a property of the desired content item.
109+
110+
5. Inject the `IContentService` to retrieve and update the value of a property of the desired content item.
95111
```csharp
96112
IContent content = _contentService.GetById(contentKey) ?? throw new Exception("Content not found");
97113

16/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/date-time-unspecified.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,28 +82,36 @@ The property editor handles unspecified date times with no time zone information
8282

8383
namespace UmbracoProject;
8484

85-
public class DateTimeWithTimeZone
85+
public class DateTimeUnspecified
8686
{
8787
/// <summary>
88-
/// The date and time value, represented as a <see cref="DateTimeOffset"/>.
88+
/// The date and time value, represented as a <see cref="DateTimeOffset"/> for storage compatibility.
8989
/// </summary>
9090
[JsonPropertyName("date")]
9191
public DateTimeOffset Date { get; init; }
9292
}
9393
```
9494

95-
2. Create an instance of the class with the desired values.
95+
2. Convert your existing DateTime value to `DateTimeOffset` for storage.
9696
```csharp
97-
var value = new DateTimeWithTimeZone
97+
DateTime dateTime = DateTime.Now; // Your existing DateTime value
98+
DateTimeOffset dateTimeOffset = dateTime; // Explicit conversion
99+
```
100+
101+
3. Create an instance of the class with the `DateTimeOffset` value.
102+
```csharp
103+
var value = new DateTimeUnspecified
98104
{
99-
Date = DateTimeOffset.Parse("2025-01-01T14:30") // The date and time value to store.
105+
Date = dateTimeOffset
100106
};
101107
```
102-
3. Inject the `IJsonSerializer` and use it to serialize the object.
108+
109+
4. Inject the `IJsonSerializer` and use it to serialize the object.
103110
```csharp
104-
var jsonValue = _jsonSerializer.Serialize(value);
111+
string jsonValue = _jsonSerializer.Serialize(value);
105112
```
106-
4. Inject the `IContentService` to retrieve and update the value of a property of the desired content item.
113+
114+
5. Inject the `IContentService` to retrieve and update the value of a property of the desired content item.
107115
```csharp
108116
IContent content = _contentService.GetById(contentKey) ?? throw new Exception("Content not found");
109117

16/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/date-time-with-time-zone.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ The property editor stores values in this JSON format:
137137
/// The IANA identifier of the time zone to pre-select in the editor. E.g., "Europe/Copenhagen".
138138
/// </summary>
139139
[JsonPropertyName("timeZone")]
140-
public string? TimeZone { get; init; }
140+
public string TimeZone { get; init; }
141141
}
142142
```
143143

@@ -146,13 +146,15 @@ The property editor stores values in this JSON format:
146146
var value = new DateTimeWithTimeZone
147147
{
148148
Date = DateTimeOffset.Now, // The date and time value to store.
149-
TimeZone = "Europe/Copenhagen" // Optional. The time zone to pre-select in the editor.
149+
TimeZone = "Europe/Copenhagen" // The time zone to pre-select in the editor.
150150
};
151151
```
152+
152153
3. Inject the `IJsonSerializer` and use it to serialize the object.
153154
```csharp
154155
var jsonValue = _jsonSerializer.Serialize(value);
155156
```
157+
156158
4. Inject the `IContentService` to retrieve and update the value of a property of the desired content item.
157159
```csharp
158160
IContent content = _contentService.GetById(contentKey) ?? throw new Exception("Content not found");

16/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/time-only.md

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,35 @@ The property editor handles time-only values. The date is automatically set to a
9494
}
9595
```
9696

97-
2. Create an instance of the class with the desired values.
97+
2. Convert your existing time value to `DateTimeOffset` for storage.
98+
99+
If you have a `TimeOnly`:
98100
```csharp
99-
var timeOnly = new TimeOnly(14, 30, 0);
100-
var value = new TimeOnlyValue
101+
TimeOnly timeOnly = TimeOnly.FromDateTime(DateTime.Now); // Your existing TimeOnly value
102+
DateTimeOffset dateTimeOffset = new DateTimeOffset(DateOnly.MinValue, timeOnly, TimeSpan.Zero);
103+
```
104+
105+
If you have a `DateTime`:
106+
```csharp
107+
DateTime dateTime = DateTime.Now; // Your existing DateTime value
108+
TimeOnly timeOnly = TimeOnly.FromDateTime(dateTime);
109+
DateTimeOffset dateTimeOffset = new DateTimeOffset(DateOnly.MinValue, timeOnly, TimeSpan.Zero);
110+
```
111+
112+
3. Create an instance of the class with the `DateTimeOffset` value.
113+
```csharp
114+
TimeOnlyValue value = new TimeOnlyValue
101115
{
102-
Date = timeOnly.ToDateTime(DateOnly.MinValue) // Convert TimeOnly to DateTimeOffset for storage
116+
Date = dateTimeOffset
103117
};
104118
```
105-
3. Inject the `IJsonSerializer` and use it to serialize the object.
119+
120+
4. Inject the `IJsonSerializer` and use it to serialize the object.
106121
```csharp
107-
var jsonValue = _jsonSerializer.Serialize(value);
122+
string jsonValue = _jsonSerializer.Serialize(value);
108123
```
109-
4. Inject the `IContentService` to retrieve and update the value of a property of the desired content item.
124+
125+
5. Inject the `IContentService` to retrieve and update the value of a property of the desired content item.
110126
```csharp
111127
IContent content = _contentService.GetById(contentKey) ?? throw new Exception("Content not found");
112128

0 commit comments

Comments
 (0)