如何用文字转换日期?

时间:2009-11-03 09:18:00

标签: .net

我想在Vb.net中用英语单词转换日期。
例如:2008年5月26日至2008年5月26日

什么是最好的approch,是否存在日期时间数据类型中的任何函数?

3 个答案:

答案 0 :(得分:1)

这可能是最好的方法: -

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");

答案 1 :(得分:0)

我不知道任何内置功能。但是只有31天的时间可以考虑。构造一个用字手动替换1到31的函数是很容易的。然后追加月份和年份,如:

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

答案 2 :(得分:0)

确实考虑到可能的数量很少,硬编码可能是最简单的灵魂。但如果您不想这样做,请查看以下资源:

http://xl.barasch.com/cCo11432.htm
http://www.vb-helper.com/howto_net_number_to_words2.html