How do I represent a time only value in .NET?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Many business domains need clock time values such as opening hours, cutoff times, and recurring reminders without attaching a specific calendar date. In .NET, choosing the correct type for this concept is important for validation, serialization, and database mapping. The best practice in modern projects is using TimeOnly for time-of-day semantics.
Use TimeOnly in Modern .NET
TimeOnly was introduced in .NET 6 to represent only a time within one day. It avoids hidden date components and makes intent clearer than DateTime when date is irrelevant.
This type is ideal for schedules and recurring local-time settings.
Fallback for Older Framework Targets
If your target framework does not support TimeOnly, use TimeSpan constrained to a single day.
Avoid DateTime for pure time values unless you are forced by API constraints.
Serialization Strategy for APIs
For APIs, define a fixed wire format. A common choice is HH:mm:ss for clarity and interoperability.
Explicit converters avoid format drift across services.
Database Mapping and Persistence
Map time-only fields to SQL time columns where possible. This keeps storage semantics aligned with domain meaning.
With EF Core, verify generated mappings and precision in migrations. If you store seconds or fractional seconds, test round-trip behavior.
Practical checklist:
- application type is
TimeOnlyor constrainedTimeSpan - database type is
time - serialization format is documented
- round-trip tests include edge values such as midnight
Keep conversion only at boundaries, not spread across core logic.
Time-Only Versus Timestamp Semantics
A time-only value is not a unique instant on a global timeline. It needs context when execution depends on timezone.
For recurring jobs, store:
- local clock time, for example
09:00 - timezone identifier, for example
America/Toronto - daylight-saving transition policy
Without timezone metadata, jobs can shift unexpectedly during seasonal transitions.
Validation and Domain Rules
Common validation rules include:
- start time before end time for same-day windows
- minute granularity requirements
- forbidden closed intervals
Put these rules in domain services to keep behavior consistent across UI and API layers.
Common Pitfalls
A common pitfall is storing time-only data in DateTime and accidentally comparing date components. Another is assuming time-only fields contain timezone meaning. Teams often leave API format undefined, causing parsing issues across clients. Fallback TimeSpan values are sometimes accepted without range validation, leading to invalid times. Database precision mismatches can also truncate seconds unexpectedly if not tested.
Summary
- Use
TimeOnlyin .NET 6 and later for time-of-day values. - Use constrained
TimeSpanon older targets when needed. - Standardize API serialization format, such as
HH:mm:ss. - Map to database
timetype and test round-trip precision. - Store timezone context separately when schedule execution depends on region.
- Keep validation rules centralized to enforce consistent behavior.
Related reading
- How do I restart a WPF application?
- How do I restart my C WinForm Application?
- How do I round a float upwards to the nearest int in C?
- How do I run a Python script from C?
- How do I run a Python script from C?
- How do I run Visual Studio as an administrator by default?
- How do I safely cast a System.Object to a bool in C?
- How do I save a stream to a file in C#?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.