question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Convert DateTime instance to DateOnly or TimeOnly

See original GitHub issue

Background 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:open
  • Created 2 months ago
  • Comments:15 (15 by maintainers)

github_iconTop GitHub Comments

2reactions
jnyrupcommented, Aug 17, 2023

Although I think I would prefer AsTimeOnly and AsDateOnly

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

1reaction
mu88commented, Jul 20, 2023

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found