.net 如何转换日期的文字?

iqxoj9l9  于 2022-12-24  发布在  .NET
关注(0)|答案(4)|浏览(125)

我想在Vb.net中转换英语单词日期。
例如:二零零八年五月二十六日至二零零八年五月二十六日
最好的方法是什么,是否存在datetime数据类型的函数?

2jcobegt

2jcobegt1#

这可能是最好的方法:

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

0vvn1miw2#

我不知道有什么内置函数,但只有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
bcs8qyzn

bcs8qyzn3#

事实上,由于可能的数字数量很少,硬编码可能是最简单的解决方案。但如果你不想这么做,看看这些资源:
http://xl.barasch.com/cCo11432.htm
http://www.vb-helper.com/howto_net_number_to_words2.html

xtfmy6hx

xtfmy6hx4#

下面是我使用的东西(信用未知的作者):

public static string DateToText(DateTime dt, bool includeTime, bool isUK)
{
    string[] ordinals =
    {
       "first day",
       "second day",
       "third day",
       "fourth day",
       "fifth day",
       "sixth day",
       "seventh day",
       "eighth day",
       "ninth day",
       "tenth day",
       "eleventh day",
       "twelfth day",
       "thirteenth day",
       "fourteenth day",
       "fifteenth day",
       "sixteenth day",
       "seventeenth day",
       "eighteenth day",
       "nineteenth day",
       "twentieth day",
       "twenty first day",
       "twenty second day",
       "twenty third day",
       "twenty fourth day",
       "twenty fifth day",
       "twenty sixth day",
       "twenty seventh day",
       "twenty eighth day",
       "twenty ninth day",
       "thirtieth day",
       "thirty first day"

    };

    int day = dt.Day;
    int month = dt.Month;
    int year = dt.Year;
    DateTime dtm = new DateTime(1, month, 1);
    string date;

    if (isUK)
    {
        //date = "This " + ordinals[day - 1] + " of " + dtm.ToString("MMMM") + " " + NumberToText(year, true);

        date = "this " + ordinals[day - 1] + " of " + dtm.ToString("MMMM") + ", " + NumberToText(year, true);
    }
    else
    {
        date = dtm.ToString("MMMM") + " " + ordinals[day - 1] + " " + NumberToText(year, false);
    }

    if (includeTime)
    {
        int hour = dt.Hour;
        int minute = dt.Minute;
        string ap = "AM";

        if (hour >= 12)
        {
            ap = "PM";
            hour = hour - 12;
        }

        if (hour == 0) hour = 12;
        string time = NumberToText(hour, false);
        if (minute > 0) time += " " + NumberToText(minute, false);
        time += " " + ap;
        date += ", " + time;
    }

    return date;
}

public static string NumberToText(int number, bool isUK)
{
    if (number == 0) return "Zero";
    string and = isUK ? "" : ""; // removed the and from the text - bevan 09/16/2019
    //string and = isUK ? "and " : ""; // deals with UK or US numbering
    if (number == -2147483648) return "Minus Two Billion One Hundred " + and +
    "Forty Seven Million Four Hundred " + and + "Eighty Three Thousand " +
    "Six Hundred " + and + "Forty Eight";
    int[] num = new int[4];
    int first = 0;
    int u, h, t;
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    if (number < 0)
    {
        sb.Append("Minus ");
        number = -number;
    }
    string[] words0 = { "", "one ", "two ", "three ", "four ", "five ", "six ", "seven ", "eight ", "nine " };
    string[] words1 = { "ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen " };
    string[] words2 = { "twenty-", "thirty-", "forty-", "fifty-", "sixty-", "seventy-", "eighty-", "ninety-" };
    string[] words3 = { "thousand ", "million ", "billion " };
    num[0] = number % 1000;           // units
    num[1] = number / 1000;
    num[2] = number / 1000000;
    num[1] = num[1] - 1000 * num[2];  // thousands
    num[3] = number / 1000000000;     // billions
    num[2] = num[2] - 1000 * num[3];  // millions
    for (int i = 3; i > 0; i--)
    {
        if (num[i] != 0)
        {
            first = i;
            break;
        }
    }
    for (int i = first; i >= 0; i--)
    {
        if (num[i] == 0) continue;
        u = num[i] % 10;              // ones
        t = num[i] / 10;
        h = num[i] / 100;             // hundreds
        t = t - 10 * h;               // tens
        if (h > 0) sb.Append(words0[h] + "Hundred ");
        if (u > 0 || t > 0)
        {
            if (h > 0 || i < first) sb.Append(and);
            if (t == 0)
                sb.Append(words0[u]);
            else if (t == 1)
                sb.Append(words1[u]);
            else
                sb.Append(words2[t - 2] + words0[u]);
        }
        if (i != 0) sb.Append(words3[i - 1]);
    }
    return sb.ToString().TrimEnd();
}

相关问题