Convert Time into Text in VB.NET

Today, I will teach you how to convert time into text using VB.NET because usually we always display the time in numbers. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application. 2. Next, add labels named lblDate for displaying the date, LabelTime for displaying the time in numbers and LabelTimeInText for displaying the time in text. Insert also a Timer named Timer1 for accessing the Time. You must design your interface like this: output 3. Then we will code for our Form. We will have first a function named TimeInText(ByVal CurrentHour As Int32, ByVal CurrentMinute As Int32) As String to display the hour of the current time.
  1. Function TimeInText(ByVal CurrentHour As Int32, ByVal CurrentMinute As Int32) As String
  2. Dim Hour, Min As Int32
  3. Dim ClockInText As String
  4. Hour = CurrentHour
  5. Min = CurrentMinute
  6. If Hour = 1 Or Hour = 13 Then
  7. ClockInText = "One"
  8. ElseIf Hour = 2 Or Hour = 14 Then
  9. ClockInText = "Two"
  10. ElseIf Hour = 3 Or Hour = 15 Then
  11. ClockInText = "Three"
  12. ElseIf Hour = 4 Or Hour = 16 Then
  13. ClockInText = "Four"
  14. ElseIf Hour = 5 Or Hour = 17 Then
  15. ClockInText = "Five"
  16. ElseIf Hour = 6 Or Hour = 18 Then
  17. ClockInText = "Six"
  18. ElseIf Hour = 7 Or Hour = 19 Then
  19. ClockInText = "Seven"
  20. ElseIf Hour = 8 Or Hour = 20 Then
  21. ClockInText = "Eight"
  22. ElseIf Hour = 9 Or Hour = 21 Then
  23. ClockInText = "Nine"
  24. ElseIf Hour = 10 Or Hour = 22 Then
  25. ClockInText = "Ten"
  26. ElseIf Hour = 11 Or Hour = 23 Then
  27. ClockInText = "Eleven"
  28. ElseIf Hour = 0 Or Hour = 12 Then
  29. ClockInText = "Twelve"
  30. End If
  31. If Min < 10 Then
  32. ClockInText &= vbNewLine & " O' " & MinInText(Min)
  33. Else
  34. ClockInText &= " " & vbNewLine & MinInText(Min)
  35. End If
  36. Return ClockInText
  37. End Function
Then for the minute to display, we will create another function named MinInText (ByVal Min As Int32) As String
  1. Function MinInText(ByVal Min As Int32) As String
  2. Dim MiT As String, cMin As Int32
  3. cMin = Min
  4. If cMin = 1 Then
  5. MiT = "One"
  6. ElseIf cMin = 2 Then
  7. MiT = "Two"
  8. ElseIf cMin = 3 Then
  9. MiT = "Three"
  10. ElseIf cMin = 4 Then
  11. MiT = "Four"
  12. ElseIf cMin = 5 Then
  13. MiT = "Five"
  14. ElseIf cMin = 6 Then
  15. MiT = "Six"
  16. ElseIf cMin = 7 Then
  17. MiT = "Seven"
  18. ElseIf cMin = 8 Then
  19. MiT = "Eight"
  20. ElseIf cMin = 9 Then
  21. MiT = "Nine"
  22. ElseIf cMin = 10 Then
  23. MiT = "Ten"
  24. ElseIf cMin = 11 Then
  25. MiT = "Eleven"
  26. ElseIf cMin = 12 Then
  27. MiT = "Twelve"
  28. ElseIf cMin = 13 Then
  29. MiT = "Thirteen"
  30. ElseIf cMin = 14 Then
  31. MiT = "Fourteen"
  32. ElseIf cMin = 15 Then
  33. MiT = "Fifteen"
  34. ElseIf cMin = 16 Then
  35. MiT = "Sixteen"
  36. ElseIf cMin = 17 Then
  37. MiT = "Seventeen"
  38. ElseIf cMin = 18 Then
  39. MiT = "Eighteen"
  40. ElseIf cMin = 19 Then
  41. MiT = "Nineteen"
  42. ElseIf cMin = 20 Then
  43. MiT = "Twenty"
  44. ElseIf cMin = 21 Then
  45. MiT = "Twenty One"
  46. ElseIf cMin = 22 Then
  47. MiT = "Twenty Two"
  48. ElseIf cMin = 23 Then
  49. MiT = "Twenty Three"
  50. ElseIf cMin = 24 Then
  51. MiT = "Twenty Four"
  52. ElseIf cMin = 25 Then
  53. MiT = "Twenty Five"
  54. ElseIf cMin = 26 Then
  55. MiT = "Twenty Six"
  56. ElseIf cMin = 27 Then
  57. MiT = "Twenty Seven"
  58. ElseIf cMin = 28 Then
  59. MiT = "Twenty Eight"
  60. ElseIf cMin = 29 Then
  61. MiT = "Twenty Nine"
  62. ElseIf cMin = 30 Then
  63. MiT = "Thirty"
  64. ElseIf cMin = 31 Then
  65. MiT = "Thirty One"
  66. ElseIf cMin = 32 Then
  67. MiT = "Thirty Two"
  68. ElseIf cMin = 33 Then
  69. MiT = "Thirty Three"
  70. ElseIf cMin = 34 Then
  71. MiT = "Thirty Four"
  72. ElseIf cMin = 35 Then
  73. MiT = "Thirty Five"
  74. ElseIf cMin = 36 Then
  75. MiT = "Thirty Six"
  76. ElseIf cMin = 37 Then
  77. MiT = "Thirty Seven"
  78. ElseIf cMin = 38 Then
  79. MiT = "Thirty Eight"
  80. ElseIf cMin = 39 Then
  81. MiT = "Thirty Nine"
  82. ElseIf cMin = 40 Then
  83. MiT = "Forty"
  84. ElseIf cMin = 41 Then
  85. MiT = "Forty One"
  86. ElseIf cMin = 42 Then
  87. MiT = "Forty Two"
  88. ElseIf cMin = 43 Then
  89. MiT = "Forty Three"
  90. ElseIf cMin = 44 Then
  91. MiT = "Forty Four"
  92. ElseIf cMin = 45 Then
  93. MiT = "Forty Five"
  94. ElseIf cMin = 46 Then
  95. MiT = "Forty Six"
  96. ElseIf cMin = 47 Then
  97. MiT = "Forty Seven"
  98. ElseIf cMin = 48 Then
  99. MiT = "Forty Eight"
  100. ElseIf cMin = 49 Then
  101. MiT = "Forty Nine"
  102. ElseIf cMin = 50 Then
  103. MiT = "Fifty"
  104. ElseIf cMin = 51 Then
  105. MiT = "Fifty One"
  106. ElseIf cMin = 52 Then
  107. MiT = "Fifty Two"
  108. ElseIf cMin = 53 Then
  109. MiT = "Fifty Three"
  110. ElseIf cMin = 54 Then
  111. MiT = "Fifty Four"
  112. ElseIf cMin = 55 Then
  113. MiT = "Fifty Five"
  114. ElseIf cMin = 56 Then
  115. MiT = "Fifty Six"
  116. ElseIf cMin = 57 Then
  117. MiT = "Fifty Seven"
  118. ElseIf cMin = 58 Then
  119. MiT = "Fifty Eight"
  120. ElseIf cMin = 59 Then
  121. MiT = "Fifty Nine"
  122. Else
  123. MiT = " Clock"
  124. End If
  125. Return MiT
  126. End Function
We will code for the Timer1_Tick for displaying the Date, Time in Numbers and Time in Text using the functions that we declared above.
  1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  2. LabelTime.Text = Now.ToShortTimeString
  3. LabelDate.Text = Now.ToLongDateString
  4. LabelTimeInText.Text = TimeInText(Now.Hour, Now.Minute)
  5. End Sub
Then enable the time in the Form_Load.
  1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2. Timer1.Enabled = True
  3. End Sub

Output:

output For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment