4

I am trying to parse an object of the class TimeOnly to DateTime (And of course there is no date in TimeOnly object, I just need a DateTime object which has the same time.)

We can convert DateOnly Objects to DateTime by using:

var dateTime = dateOnly.ToDateTime(TimeOnly.MinValue);

But similar process is not available while trying to convert from TimeOnly to DateTime. How do I achieve this in clean and efficient manner?

I have browsed this thread which addresses a question to store Only Time in an object. The thread is 12 years (Before .Net6; era of "TimeOnly" class) old and the answers suggest to implement a custom class to achieve the conversion (Which is an overkill in my case)

Ashish Neupane
  • 193
  • 2
  • 17
  • 2
    Such a cast makes no sense. There are infinite dates for that time. Why do you want this? – Panagiotis Kanavos Mar 17 '22 at 13:47
  • Why do you want to store it as DateTime? The entire _point_ of the new types in C# is to store only Time and only Date. Date to DateTime probably works because the default DateTime already supports date-only input and automatically assigns a time of `00:00:00.000` if none is specified. – TylerH Mar 17 '22 at 13:48
  • `TimeSpan` would make more sense here instead of `DateTime` – fubo Mar 17 '22 at 13:51
  • @PanagiotisKanavos I am working on a system which has two devices using different version of the language. As I mentioned in the question. Any Date in the DateTime object is fine since the date part is not going to be used anyway. – Ashish Neupane Mar 17 '22 at 13:53
  • And mind that there is a way to convert DateOnly to DateTime. It shouldn't be surprising to parse DateTime from TimeOnly. – Ashish Neupane Mar 17 '22 at 13:55
  • 1
    "*And mind that there is a way to convert DateOnly to DateTime.*" Again the way you did it inserts `00:00:00.000` (or midnight) into the DateTime by way of `TimeOnly.MinValue`, which is how `DateTime` has always worked when you declare it with only a date value. You are not really "converting it" in a way that you can expect to rely on, more "taking advantage of a quirk". – TylerH Mar 17 '22 at 14:05
  • @TylerH If I have a DateOnly Object that I want to change into DateTime object, The mentioned way seems to be the most efficient way. when you create an object of the class DateTime with it's constructor, you need to pass parameters which would be messy in my case. The thread you suggested doesn't solve my problem. – Ashish Neupane Mar 17 '22 at 14:20
  • 1
    @AshishNeupane I find that difficult to believe, given that the answers provide the exact solution that you accepted here (which is to convert `TimeOnly` using `.ToTimeSpan()`). – TylerH Mar 17 '22 at 14:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/243042/discussion-between-ashish-neupane-and-tylerh). – Ashish Neupane Mar 17 '22 at 20:09

1 Answers1

5

Since TimeOnly holds no date information, you'll need a reference date, then add the time contained in the TimeOnly object by using the ToTimeSpan method.

TimeOnly timeOnly = new TimeOnly(10, 10, 10);
var referenceDate = new DateTime(2022, 1, 1);
referenceDate += timeOnly.ToTimeSpan();
Console.WriteLine(dateTime); // 1/1/2022 10:10:10 AM
Mateus Schneiders
  • 4,853
  • 3
  • 20
  • 40