dotnet / dotnet/wpf

The types `DateOnly` and `TimeOnly` do not get XAML serialized

Open
#10,529 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

Variables of types `DateOnly` and `TimeOnly` do not get serialized when converted to XAML. This is not the case for other date-related standard types like `DateTime` or `DateTimeOffset`.

### Reproduction Steps

Run the following function in a NET8 environment:
```
public void TestSerializationForDateTimeOnly()
{
var dateOnly = new DateOnly(2025, 3, 7);
var s1 = XamlServices.Save(dateOnly);
Console.WriteLine(s1);

var timeOnly = new TimeOnly(21, 49, 3);
var s2 = XamlServices.Save(timeOnly);
Console.WriteLine(s2);

var dateTime = new DateTime(dateOnly, timeOnly, DateTimeKind.Utc);
var s3 = XamlServices.Save(dateTime);
Console.WriteLine(s3);
}
```

### Expected behavior

Variables of type `DateOnly` and `TimeOnly` should be correctly serialized by `XamlServices.Save(...)`.

### Actual behavior

After running the function, notice that:
- `s1` is `
`
- `s2` is `
`
- However `s3` is `
2025-03-07T21:49:03Z`

### Regression?

As far as we know this is not a regression.

### Known Workarounds

We have found no workaround so far. We have attempted to follow the steps from https://learn.microsoft.com/en-us/dotnet/desktop/xaml-services/define-custom-types and define custom converters for `DateOnly` and `TimeOnly` and then used `TypeDescriptor.AddAttribute(...)` in order to map the converter to each of the types, but XAML serialization still did not occur. Also, we noticed that the converter `DateTimeConverter2` exists in `System.Xaml` which probably explains why `DateTime` does get serialized correctly.

### Impact

_No response_

### Configuration

OS: Windows 11 on an x64 CPU
NET version: 8.0

### Other information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.