THE DOSSIER TIMES
LOG: DATE-TIME-DOT-NET-COREDATE: 2023-01-27AUTHOR: AMIT PRAKASH

Date-only with (in) .Net6 πŸ˜‰

Date-only with (in) .Net6 πŸ˜‰

Date-only with (in) .Net6 πŸ˜‰

Since the beginning of C#, dates and times have been handled by the DateTime struct, which may be used for either purpose. When two needs may be satisfied by one solution, trade-offs are frequently made, as is the case with the majority of software development decisions. When it comes to DateTime, this often means that even while we put up a whole DateTime instance, we frequently just care about the date component or the time component, but very seldom both.

Now new Release with .Net6, we have two additional (grate job Microsoft)

  1. DateOnly
  2. TimeOnly

Let's talk about new two addition :

  1. DateOnly The new DateOnly struct may be used, as its name indicates, to solely represent the date component. A historical document, where we are more interested in the fact of the event than the exact moment it occurred, such as a person's date of birth, may be a good example.

Let's see how to create a fresh DateOnly instance:


var _todaysDate = new DateOnly(2023,1,27);
var _todaysUtcDate = DateOnly.FromDateTime(DateTime.Utc.Now);
  1. TimeOnly

The new TimeOnly struct can be used when we are just concerned with the time component. A daily recurring alarm event at 11 AM might serve as a decent illustration in this case. The actual date doesn't matter because it happens every day.

It's quite easy to start a new TimeOnly instance:


// Fell free to use below constructors as per your use case πŸ™‚

public TimeOnly(int hour, int minute, int second, int millisecond)
public TimeOnly(int hour, int minute, int second)
public TimeOnly(int hout, int minute)

// 5:00 am club member 
var _myTime= new TimeOnly(5,0)

// Current time while I am writing this

var _thisNewsLetterEditionTime= TimeOnly.FromDateTime(DateTime.UtcNow);

πŸ’‘

THE ARCHITECTURE LOG

Explores the design decisions, trade-offs, and hidden complexity behind real-world software systems.

Subscribe on LinkedIn

Join 2,439+ Subscribers Β· Published Weekly

INDEX TAGS:#PreflightParty#hashtag#SecurityFirst#hashtag#DataHarmony#hashtag#NoMoreWebWalls
REVIEWED
β€” CLASSIFIED BACKEND ENGINEER NOW OPERATIONAL β€” FIELD REPORTS AVAILABLE β€” CASE FILES ACCESSIBLE FOR IMMEDIATE REVIEW β€” TRANSMISSION SECURE β€” PROCEED TO INTELLIGENCE PORTALΒ Β Β Β β€” CLASSIFIED BACKEND ENGINEER NOW OPERATIONAL β€” FIELD REPORTS AVAILABLE β€” CASE FILES ACCESSIBLE FOR IMMEDIATE REVIEW β€” TRANSMISSION SECURE β€” PROCEED TO INTELLIGENCE PORTALΒ Β Β Β