Make a txt file with all months in order, each followed by a line break or a special character marker. Call string.split() using the line break or the marker as a separator, then use the month number as an index to search in the string array created by the split function. It may sound stupid, but that's because it is.
I said it was stupid. But seriously, any format could work. And it might need changes, say, if you need to change the language. Still, most languages will have date function and culture functions to get month names in the target language, so my method is just needlessly convoluted. It was literally the first thing that came to my head and I thought it was silly, so why not.
Assuming date is not a DateTime but a custom class/struct, I don't like the idea of having to new() one every time you want a string value. IMO, a hashmap/dictionary is really the easiest option.
Edit: C# offers ToString overloads with formatting for numerics as well.
switch (date.getMonth()) {
case 0:
text += 'Jan/';
break;
case 1:
text += 'Feb/';
break;
case 2:
text += 'Mar/';
break;
case 3:
text += 'Apr/';
break;
case 4:
text += 'May/';
break;
case 5:
text += 'Jun/';
break;
case 6:
text += 'Jul/';
break;
case 7:
text += 'Aug/';
break;
case 8:
text += 'Sep/';
break;
case 9:
text += 'Oct/';
break;
case 10:
text += 'Nov/';
break;
case 11:
text += 'Dec/';
break;
}
27
u/rustyredditortux Nov 11 '21
apart from a hashmap how else would you do this?
54
u/CaitaXD Nov 11 '21
It works, it's readable and it doesn't generate thechinical debt
Yall being anal
9
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Nov 11 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
1
u/leigen_zero Nov 12 '21
Without greater context, this could be from a simple excerise to learn how a switch statement works, in which case it works exactly to specification
-1
u/kristallnachte Nov 11 '21
Yikes...
Could just use the format method to just spit out the month as 3 characters...