I have a logic that checks the start and end date of an event. If the start and end date are in the same month then it shows only one date, for example 18 Feb, 2021. But if it's a multi-day event then it shows the event date like this, for example 18 Feb - 24 Mar, 2021.
This logic is taken care of by a ternary conditional and it's working fine, but now I would like to add a third condition for when the event is multi-day and the start & end date are in the same month. For this third condition, I would like to omit the month name in start date and only show the month once after the end date, like this: 18 - 22 Feb, 2021 and not 18 Feb - 22 Feb, 2021.
This is my ternary conditional, I was thinking changing it to a standard if else statement for more flexibility but I am not sure how to change that and what would be the logic for the third condition:
<%# (String.Format("{0:dd MMM}", (DateTime)Eval("EventStart")) == String.Format("{0:dd MMM}", (DateTime)Eval("EventEnd"))) ? String.Format("{0:dd MMM, yyy}", ((DateTime)Eval("EventEnd")).AddMinutes((double)Eval("EventEndUtcOffset"))) : String.Format("{0:dd MMM}", ((DateTime)Eval("EventStart")).AddMinutes((double)Eval("EventStartUtcOffset")))+" - "+String.Format("{0:dd MMM, yyy}",((DateTime)Eval("EventEnd")).AddMinutes((double)Eval("EventEndUtcOffset"))) %>
Any help to understand the logic of this code as it is now would be appreciated and it helps me understand how to change it to an if else statement.
So, let's start by adding some whitespace to format it as a ternary:
(String.Format("{0:dd MMM}", (DateTime)Eval("EventStart")) == String.Format("{0:dd MMM}", (DateTime)Eval("EventEnd")))
? String.Format("{0:dd MMM, yyy}", ((DateTime)Eval("EventEnd")).AddMinutes((double)Eval("EventEndUtcOffset")))
: String.Format("{0:dd MMM}", ((DateTime)Eval("EventStart")).AddMinutes((double)Eval("EventStartUtcOffset")))+" - "+String.Format("{0:dd MMM, yyy}",((DateTime)Eval("EventEnd")).AddMinutes((double)Eval("EventEndUtcOffset")))
That's fairly easy to translate directly into an if/else:
string result;
if (String.Format("{0:dd MMM}", (DateTime)Eval("EventStart")) == String.Format("{0:dd MMM}", (DateTime)Eval("EventEnd")))
{
result = String.Format("{0:dd MMM, yyy}", ((DateTime)Eval("EventEnd")).AddMinutes((double)Eval("EventEndUtcOffset")));
}
else
{
result = String.Format("{0:dd MMM}", ((DateTime)Eval("EventStart")).AddMinutes((double)Eval("EventStartUtcOffset")))+" - "+String.Format("{0:dd MMM, yyy}",((DateTime)Eval("EventEnd")).AddMinutes((double)Eval("EventEndUtcOffset")));
}
That hasn't particularly helped the readability however... Let's improve it slightly. Start by declaring some variables to avoid all of those repeated casts:
var eventStart = (DateTime)Eval("EventStart");
var eventEnd = (DateTime)Eval("EventEnd");
double eventEndUtcOffsetMins = (double)Eval("EventEndUtcOffset");
string result;
if (String.Format("{0:dd MMM}", eventStart) == String.Format("{0:dd MMM}", eventEnd))
{
result = String.Format("{0:dd MMM, yyy}", eventEnd.AddMinutes(eventEndUtcOffsetMins));
}
else
{
double eventStartUtcOffsetMins = (double)Eval("EventStartUtcOffset");
result = String.Format("{0:dd MMM}", eventStart.AddMinutes(eventStartUtcOffsetMins))+" - "+String.Format("{0:dd MMM, yyy}",eventEnd.AddMinutes(eventEndUtcOffsetMins));
}
This is getting better. We can shorten it a bit by using TimeSpans
for those offsets: that shifts the complexity from those already-long String.Format
lines, into the lines which declate eventStartUtcOffset
and eventEndUtcOffset
.
var eventStart = (DateTime)Eval("EventStart");
var eventEnd = (DateTime)Eval("EventEnd");
var eventEndUtcOffset = TimeSpan.FromMinutes((double)Eval("EventEndUtcOffset"));
string result;
if (String.Format("{0:dd MMM}", eventStart) == String.Format("{0:dd MMM}", eventEnd))
{
result = String.Format("{0:dd MMM, yyy}", eventEnd + eventEndUtcOffset);
}
else
{
var eventStartUtcOffset = TimeSpan.FromMinutes((double)Eval("EventStartUtcOffset"));
result = String.Format("{0:dd MMM}", eventStart + eventStartUtcOffset)+" - "+String.Format("{0:dd MMM, yyy}",eventEnd + eventEndUtcOffset);
}
We can also remove those calls to String.Format
, and use interpolated strings instead:
var eventStart = (DateTime)Eval("EventStart");
var eventEnd = (DateTime)Eval("EventEnd");
var eventEndUtcOffset = TimeSpan.FromMinutes((double)Eval("EventEndUtcOffset"));
string result;
if ($"{eventStart:dd MMM}" == $"{eventEnd:dd MMM}")
{
result = $"{eventEnd + eventEndUtcOffset:dd MMM, yyy}";
}
else
{
var eventStartUtcOffset = TimeSpan.FromMinutes((double)Eval("EventStartUtcOffset"));
result = $"{eventStart + eventStartUtfOffset:dd MMM} - {eventEnd + eventEndUtfOffset:dd MMM, yyy}";
}
That's a bit better -- at least we can now see what the code is doing.