Nel mio C# app, mi passa una variabile stringa che è di formato aaaammgg-aaaammgg che rappresenta una da e per la data. Voglio ottenere gli orari di inizio e fine rispettivamente per queste date. Attualmente ho il codice qui sotto, ma mi chiedevo se ci fosse più di una soluzione elegante?come ottenere i tempi di inizio e di fine di una giornata
Così per pdr = 20.090.521-20.090.523 otterrebbe "20.090.521 00:00:00" e "20.090.523 23:59:59"
private void ValidateDatePeriod(string pdr, out DateTime startDate,
out DateTime endDate)
{
string[] dates = pdr.Split('-');
if (dates.Length != 2)
{
throw new Exception("Date period is of incorrect format");
}
if (dates[0].Length != 8 || dates[1].Length != 8)
{
throw new Exception("Split date periods are of incorrect format");
}
startDate = DateTime.ParseExact(dates[0] + " 00:00:00",
"yyyyMMdd HH:mm:ss", null);
endDate = DateTime.ParseExact(dates[1] + "23:59:59",
"yyyyMMdd HH::mm:ss", null);
}
Come altri hanno affermato, EndOfDay è probabilmente un costrutto non necessario e impreciso, in quanto non ha un punto preciso nel tempo, tranne forse lo StartOfDay di domani. – user420667