将日期转换为单词

时间:2017-02-14 03:58:01

标签: vb.net

我正在开发需要将日期转换为文本的软件。像cheqe打印软件一样显示文本量。

示例2017/02/28应显示为

  

二月二十八日二十七日

我正在使用vb.net 2008.任何人都可以指导我如何处理这个问题吗?

2 个答案:

答案 0 :(得分:2)

确定要达到要求,请按照以下步骤操作。

在您的项目中,点击Project并选择Manage NuGet Packages...
一旦您进入并且页面已加载,请点击Browse并在搜索框中输入{{1}它会显示25到30个搜索结果,在您的情况下,您要选择Humanizer选择并安装它。

一旦安装完毕关闭Humanizer.Core.uk,然后按照我为您快速写的代码进行操作。

Nuget Package Manager


这是在控制台应用程序中完成的,如果您使用的是Windows窗体应用程序,请使用相同的方法 Imports Humanizer Module Module1 Sub Main() Console.WriteLine(dateToText()) Console.ReadKey() End Sub Private Function dateToText() As String Dim NumbersToText = DateTime.Now.Year.ToWords() Dim MonthName = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(DateTime.Now.Month) Dim dayNum As String = "" Select Case DateTime.Now.Day Case 1 dayNum = "st" Exit Select Case 2 dayNum = "nd" Exit Select Case 3 dayNum = "rd" Exit Select Case 21 dayNum = "st" Exit Select Case 22 dayNum = "nd" Exit Select Case 23 dayNum = "rd" Exit Select Case 31 dayNum = "st" Exit Select Case Else dayNum = "th" Exit Select End Select Dim result = Date.Now.Day & dayNum & " of " & MonthName & " " & NumbersToText Return result End Function End Module

答案 1 :(得分:0)

结合@Werdna回答和This example我能够创建一个将Date转换为word的类。所以我能够用This example替换 Humanizer 。这是班级

Public Class NumbersToWord


    Dim mOnesArray(8) As String
    Dim mOneTensArray(9) As String
    Dim mTensArray(7) As String
    Dim mPlaceValues(4) As String


    Public Sub New()

        mOnesArray(0) = "one"
        mOnesArray(1) = "two"
        mOnesArray(2) = "three"
        mOnesArray(3) = "four"
        mOnesArray(4) = "five"
        mOnesArray(5) = "six"
        mOnesArray(6) = "seven"
        mOnesArray(7) = "eight"
        mOnesArray(8) = "nine"

        mOneTensArray(0) = "ten"
        mOneTensArray(1) = "eleven"
        mOneTensArray(2) = "twelve"
        mOneTensArray(3) = "thirteen"
        mOneTensArray(4) = "fourteen"
        mOneTensArray(5) = "fifteen"
        mOneTensArray(6) = "sixteen"
        mOneTensArray(7) = "seventeen"
        mOneTensArray(8) = "eightteen"
        mOneTensArray(9) = "nineteen"

        mTensArray(0) = "twenty"
        mTensArray(1) = "thirty"
        mTensArray(2) = "forty"
        mTensArray(3) = "fifty"
        mTensArray(4) = "sixty"
        mTensArray(5) = "seventy"
        mTensArray(6) = "eighty"
        mTensArray(7) = "ninety"

        mPlaceValues(0) = "hundred"
        mPlaceValues(1) = "thousand"
        mPlaceValues(2) = "million"
        mPlaceValues(3) = "billion"
        mPlaceValues(4) = "trillion"

    End Sub


    Protected Function GetOnes(ByVal OneDigit As Integer) As String

        GetOnes = ""

        If OneDigit = 0 Then
            Exit Function
        End If

        GetOnes = mOnesArray(OneDigit - 1)

    End Function


    Protected Function GetTens(ByVal TensDigit As Integer) As String

        GetTens = ""

        If TensDigit = 0 Or TensDigit = 1 Then
            Exit Function
        End If

        GetTens = mTensArray(TensDigit - 2)

    End Function


    Public Function ConvertNumberToWords(ByVal NumberValue As String) As String

        Dim Delimiter As String = " "
        Dim TensDelimiter As String = "-"
        Dim mNumberValue As String = ""
        Dim mNumbers As String = ""
        Dim mNumWord As String = ""
        Dim mFraction As String = ""
        Dim mNumberStack() As String
        Dim j As Integer = 0
        Dim i As Integer = 0
        Dim mOneTens As Boolean = False

        ConvertNumberToWords = ""

        ' validate input
        Try
            j = CDbl(NumberValue)
        Catch ex As Exception
            ConvertNumberToWords = "Invalid input."
            Exit Function
        End Try

        ' get fractional part {if any}
        If InStr(NumberValue, ".") = 0 Then
            ' no fraction
            mNumberValue = NumberValue
        Else
            mNumberValue = Microsoft.VisualBasic.Left(NumberValue, InStr(NumberValue, ".") - 1)
            mFraction = Mid(NumberValue, InStr(NumberValue, ".")) ' + 1)
            mFraction = Math.Round(CSng(mFraction), 2) * 100

            If CInt(mFraction) = 0 Then
                mFraction = ""
            Else
                mFraction = "&& " & mFraction & "/100"
            End If
        End If
        mNumbers = mNumberValue.ToCharArray

        ' move numbers to stack/array backwards
        For j = mNumbers.Length - 1 To 0 Step -1
            ReDim Preserve mNumberStack(i)

            mNumberStack(i) = mNumbers(j)
            i += 1
        Next

        For j = mNumbers.Length - 1 To 0 Step -1
            Select Case j
                Case 0, 3, 6, 9, 12
                    ' ones  value
                    If Not mOneTens Then
                        mNumWord &= GetOnes(Val(mNumberStack(j))) & Delimiter
                    End If

                    Select Case j
                        Case 3
                            ' thousands
                            mNumWord &= mPlaceValues(1) & Delimiter

                        Case 6
                            ' millions
                            mNumWord &= mPlaceValues(2) & Delimiter

                        Case 9
                            ' billions
                            mNumWord &= mPlaceValues(3) & Delimiter

                        Case 12
                            ' trillions
                            mNumWord &= mPlaceValues(4) & Delimiter
                    End Select


                Case Is = 1, 4, 7, 10, 13
                    ' tens value
                    If Val(mNumberStack(j)) = 0 Then
                        mNumWord &= GetOnes(Val(mNumberStack(j - 1))) & Delimiter
                        mOneTens = True
                        Exit Select
                    End If

                    If Val(mNumberStack(j)) = 1 Then
                        mNumWord &= mOneTensArray(Val(mNumberStack(j - 1))) & Delimiter
                        mOneTens = True
                        Exit Select
                    End If

                    mNumWord &= GetTens(Val(mNumberStack(j)))

                    ' this places the tensdelimiter; check for succeeding 0
                    If Val(mNumberStack(j - 1)) <> 0 Then
                        mNumWord &= TensDelimiter
                    End If
                    mOneTens = False

                Case Else
                    ' hundreds value 
                    mNumWord &= GetOnes(Val(mNumberStack(j))) & Delimiter

                    If Val(mNumberStack(j)) <> 0 Then
                        mNumWord &= mPlaceValues(0) & Delimiter
                    End If
            End Select
        Next

        Return mNumWord & mFraction

    End Function


    Public Function DateToText(ByVal D As DateTime) As String

        Dim NumbersToText = ConvertNumberToWords(D.Year)

        Dim MonthName = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(D.Month)
        Dim dayNum As String = ""

        Select Case DateTime.Now.Day
            Case 1
                dayNum = "st"
                Exit Select
            Case 2
                dayNum = "nd"
                Exit Select
            Case 3
                dayNum = "rd"
                Exit Select
            Case 21
                dayNum = "st"
                Exit Select
            Case 22
                dayNum = "nd"
                Exit Select
            Case 23
                dayNum = "rd"
                Exit Select
            Case 31
                dayNum = "st"
                Exit Select
            Case Else
                dayNum = "th"
                Exit Select
        End Select

        Dim result = D.Day & dayNum & " of " & MonthName & " " & NumbersToText
        Return result

    End Function


End Class