Expand API surface for DateTime, DateTimeOffset, DateOnly, TimeOnly, and TimeSpan
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- csharp
- Domain
- backend-api-design
Research direction
The issue names no source files, entry points, or tests and proposes a broad API expansion across five date and time types. Start by reviewing the existing APIs against the numbered proposals, then narrow the scope to a specific, accepted change. Done requires a decided API proposal before implementation can begin.
Written by the indexing model from the issue text.
Description
Background and Motivation
Almost every project I've worked on, from small personal projects to large enterprise ones, ends up with various extension methods designed to make date and time manipulation more concise and less repetitive. Many of these are born out of inconsistencies and/or shortcomings of the current API surface on the five core date and time types: DateTime, DateTimeOffset, DateOnly, TimeOnly, and TimeSpan. Specifically, DateOnly and TimeOnly do not feel like first class citizens next to DateTime and DateTimeOffset.
In an ecosystem where version after version, compiler and syntax changes have been consitently made to reduce the amount of syntax needed to create types (ie: collection expressions), I feel like these core system types have not been given the same amount of attention. Yet date and time manipulation is probably one of the most common operations and omni present in almost every single project a .NET developer will be involved in.
Proposed API
To avoid creating a large amount of issues that are all centered around the same background and motivation, I opted instead on creating a list of proposed APIs with additional notes where appropriate. I have tried to group them as best as I can.
Missing APIs for consistency
1) DateTimeOffset.FromDateTime(DateTime dateTime) static method
DateOnlyandTimeOnlyboth already exposeFromDateTime().- A
DateTimecan already be cast into aDateTimeOffset.
2) DateTime.FromDateTimeOffset() static method
- There is already a constructor overload on
DateTimeOffsetwhich supports aDateTime. - With proposal 1,
DateTimeOffsetwould also expose the similarFromDateTime().
3) dateTime.ToDateTimeOffset(TimeSpan offset) instance method
- There is already a constructor overload on
DateTimeOffsetwhich supports aDateTimeandTimeSpan. DateOnlyinstances already expose the similarToDateTime.
4) DateTimeOffset.Today and DateOnly.Today property
DateTimeOffsetalready exposesNowandUtcNowlikeDateTime.DateTimealready exposesToday.
5) TimeOnly.Now and TimeOnly.UtcNow property
DateTimeandDateTimeOffsetalready exposeNowandUtcNow.
6) timeOnly.AddSeconds(double value), timeOnly.AddMilliseconds(double value), timeOnly.AddMicroseconds(double value), and timeOnly.AddTicks(long value) instance method
DateTimeandDateTimeOffsetinstances both already exposeAddSeconds(),AddMilliseconds(),AddMicroseconds(), andAddTicks().
7) DateOnly.FromDateTimeOffset(DateTimeOffset dateTimeOffset) and TimeOnly.FromDateTimeOffset(DateTimeOffset dateTimeOffset) static methods
DateOnlyandTimeOnlyboth already exposeFromDateTime().
8) DateTime.FromDateOnly(DateOnly dateOnly) and DateTimeOffset.FromDateOnly(DateOnly dateOnly) static methods
DateOnlyandTimeOnlyboth already exposeFromDateTime().
9) dateTime.ToDateOnly() instance method
DateOnlyinstances already exposeToDateTime().
10) dateTimeOffset.ToDateOnly() instance method
DateOnlyinstances already exposeToDateTime().- With proposal 9,
DateTimeinstances would also exposeToDateOnly().
11) dateOnly.ToDateTimeOffset() and dateOnly.ToDateTimeOffset(TimeSpan offset) instance methods
DateOnlyinstances already exposeToDateTime().- With proposal 9 and 10,
DateTimeinstances would also exposeToDateOnly()andDateTimeOffsetinstances would also expose.ToDateOnly().
12) dateTime.ToTimeOnly() and dateTimeOffset.ToTimeOnly() instance methods
DateOnlyinstances already exposeToDateTime().- With proposal 9, 10, and 11,
DateTimeinstances would also exposeToDateOnly(),DateTimeOffsetinstances would also expose.ToDateOnly(), andDateOnlyinstances would also expose.ToDateTimeOffset().
13) Implicit cast from DateOnly instances to DateTimeOffset instances
DateTimeinstances already implicitly cast toDateTimeOffset.
Expanding Today API
14) DateTime.UtcToday, DateTimeOffset.UtcToday, DateOnly.UtcToday
DateTimeandDateTimeOffsetalready expose the propertiesNowandUtcNow, this would be natural pairing to the already exposedTodayproperty.- With proposal 4, both
DateTimeOffsetandDateOnlywould also exposeToday.
New Substract APIs
15) Substract instance method variants of Add to DateTime, DateTimeOffset, DateOnly, and TimeOnly
- Add the
SubstractYears(int value)instance method toDateTime,DateTimeOffset, andDateOnly. - Add the
SubstractMonths(int value)instance method toDateTime,DateTimeOffset, andDateOnly. - Add the
SubstractDays(double value)instance method toDateTime,DateTimeOffset, andSubstractDays(int value)instance methodDateOnly. - Add the
SubstractHours(double value)instance method toDateTime,DateTimeOffset, andTimeOnly. - Add the
SubstractMinutes(double value)instance method toDateTime,DateTimeOffset, andTimeOnly. - Add the
SubstractSeconds(double value)instance method toDateTime,DateTimeOffset, andTimeOnly. - Add the
SubstractMilliseconds(double value)instance method toDateTime,DateTimeOffset, andTimeOnly. - Add the
SubstractMicroseconds(double value)instance method toDateTime,DateTimeOffset, andTimeOnly. - Add the
SubstractTicks(long value)instance method toDateTime,DateTimeOffset, andTimeOnly.
Expand TimeSpan API
16) TimeSpan.FromYears(int value) and TimeSpan.FromMonths(int value) static methods
TimeSpanalready exposes all otherFrommethods.
17) TimeSpan.Add variants and TimeSpan.Substract variants
- Adding the AddYears(), AddMonths(), AddDays(), AddHours(), AddMinutes(), AddSeconds(), AddMilliseconds(), AddMicroseconds(), AddTicks() instance methods would avoid needing to create an intermediate TimeSpan to manipulate an existing TimeSpan.
- Also in the same vein, adding the instance methods from proposal 15.
- Dominant language
- C#
- Stars
- 18.3k
- Forks
- 5.6k
- PR merge metrics
- PR metrics pending
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/runtime
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
area-System.Reflection blocking-clean-ci-optional Known Build Error os-mac-os-x untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
area-CodeGen-coreclr untriaged
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
agentic-workflows untriaged
Difficulty 1/5 Under an hour Newbie friendliness 78/100
-
area-VM-meta-mono untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
Create parent directories only after the containment check in InstallHelper.TryExtractToDirectory Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
PowerShell/PSResourceGet#2056 ·