Convert DateTime instance to DateOnly or TimeOnly
See original GitHub issueBackground and motivation
The Fluent Assertion syntax for DateTime
objects (e. g. 12.April(1953).At(19, 53)
) is phantastic. But IMHO it could be even better with two new extension methods .ToDateOnly()
and .ToTimeOnly()
. This would allow keeping a fluent syntax (instead of new DateOnly(1953, 4, 12)
) and shorter (instead of DateOnly.FromDateTime(12.April(1953))
) .
API Proposal
public static class FluentDateTimeExtensions
{
public static DateOnly ToDateOnly(this DateTime date);
public static TimeOnly ToTimeOnly(this DateTime date);
}
API Usage
var now = 12.April(1953);
var date = now.ToDateOnly();
var time = now.ToTimeOnly();
Alternative Designs
/
Risks
Since it’s only a new extension method to be added, a regression or breaking change should not happen.
Issue Analytics
- State:
- Created 2 months ago
- Comments:15 (15 by maintainers)
Top Results From Across the Web
Converting DateOnly and TimeOnly to DateTime and vice ...
As you can see, to convert from DateOnly to DateTime, we need to provide TimeOnly info along with it. we can provide actual...
Read more >Convert DateTime.Now to DateOnly in dd/mm/yyyy
I'm working with a DateOnly variable and I'm trying to get the DateTime.Now time in a dd/mm/yyyy format, however it's only returning the...
Read more >DateOnly.FromDateTime(DateTime) Method (System)
Returns a DateOnly instance that is set to the date part of the specified dateTime . public: static DateOnly FromDateTime(DateTime dateTime);.
Read more >Using DateOnly and TimeOnly in .NET 6 - Steve Gordon
Just as with DateOnly, we can convert from an existing DateTime into a TimeOnly using the FromDateTime static method. var currentTime = ......
Read more >Converting DateOnly and TimeOnly to DateTime and vice ...
Converting DateTime to DateOnly and TimeOnly is very easy and simple and already well supported in library itself. Source: Medium - Nitesh ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
As we’re converting another type and truncating, I vote for using the
To
prefix.https://stackoverflow.com/questions/13867829/to-vs-as-vs-get-method-prefixes
For a time I could agree with your arguments:
new(13, 37)
is okayish to read, concise, and probably unambiguous.But for a date writing
new(1953, 4, 12)
doesn’t feel good to me, as it could be both April 12 or December 4 if you’re not familiar with the API.