It’s not brought up frequently, but a “CodeSOD” is a “Code Sample of the Day”. Martin brings us this function, entitled StringOfToday. It’s in VB.Net, which, as we all know, has date formatting functions built in.
Public Function StringOfToday() As String
Dim d As New DateTime
d = Now
Dim DayString As String
If d.Day < 10 Then
DayString = “0” & d.Day.ToString
Else
DayString = d.Day.ToString
End If
Dim MonthString As String
If d.Month < 10 Then
MonthString = “0” & d.Month.ToString
Else
MonthString = d.Month.ToString
End If
Dim YearString As String = d.Year.ToString
Return YearString & MonthString & DayString
End Function
There’s not much new here, when it comes to formatting dates as strings. Grab the date, and pad it if it’s less than 10. Grab the month, and pad it if it’s less than 10. Grab the year, which will be 4 digits anytime within the last 2,000 years or so, so we don’t need to pad it. Concatenate it all together, and voila: a date string.
Mostly, I just enjoy this because of the name. StringOfToday. It’s like I’m in a restaurant. “Excuse me, waiter, what’s the string of the day?” “Ah, a piquant 8 digit numeric string, hand concatenated using the finest ampersands, using a bounds checked string type.” “Oh, excellent, I’m allergic to null terminators. I’ll have that.”
Keep the plebs out of prod. Restrict NuGet feed privileges with ProGet. Learn more.
Source: Read MoreÂ