Article 6QSSQ CodeSOD: String Du Jour

CodeSOD: String Du Jour

by
Remy Porter
from The Daily WTF on (#6QSSQ)

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 StringDim d As New DateTimed = NowDim DayString As StringIf d.Day < 10 ThenDayString = "0" & d.Day.ToStringElseDayString = d.Day.ToStringEnd IfDim MonthString As StringIf d.Month < 10 ThenMonthString = "0" & d.Month.ToStringElseMonthString = d.Month.ToStringEnd IfDim YearString As String = d.Year.ToStringReturn YearString & MonthString & DayStringEnd 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."

proget-icon.png [Advertisement] Keep the plebs out of prod. Restrict NuGet feed privileges with ProGet. Learn more.
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments