I want to convert date in english wor开发者_C百科ds in Vb.net.
For example: 26 May,2008 to Twenty sixth May 2008What will be best approch, is any function in datetime data type exists?
This probably the best approach:-
private static string[] dayText = new string[]
{"First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth",
"Nineth", "Tenth", "Eleventh", "Twelveth", "Thirteenth", "Fourteenth", "Fifteenth",
"Sixteenth", "Seventeenth", "Eighteenth", "Nineteenth", "Twentieth", "Twenty first",
"Twenty second", "Twenty third", "Twenty fourth", "Twenty fifth", "Twenty sixth",
"Twenty seventh", "Twenty eighth", "Twenty nineth", "Thirtieth", "Thirty first"};
public static string DayInWords(int day)
{
//assertion code here
return dayText[day-1];
}
...
string result = DayInWords(myDate.Day) + myDate.ToString(" MMM yyyy");
I don't know of any built in function. But there are only 31 days to consider. It would be easy enough to construct a function which manually replaced 1 to 31 with words. Then append the month and year, as in:
Function foobar(ByVal mydate As Date) As String
Dim result As String = ""
Select Case mydate.Day
Case 1
result = "First"
Case 2
result = "Second"
Case 3
result = "Third"
... etc ...
Case 31
result = "Thirty-First"
End Select
Return result & " " & mydate.ToString("MMMM yyyy")
End Function
Indeed given the small amount of possible numbers, hardcoding them is probably the easiest soultion. But if you don't want to do that, take a look at these resources:
http://xl.barasch.com/cCo11432.htm
http://www.vb-helper.com/howto_net_number_to_words2.html
精彩评论