小说网 找小说 无限小说 烟雨红尘 幻想小说 酷文学 深夜书屋

完整的将日期时间转换为汉字的代码

下面的代码可以将任意日期(1000-01-01 0:0:0------>9999-12-31 23:59:59)转换为汉字显示

Function datename(ByVal mydate As String) As String

Dim i As Long, mystr(6) As String, temp As String
Const all As String = "○一二三四五六七八九十"

If Not IsDate(mydate) Then '错误处理
MsgBox "非正确日期格式!", vbInformation, "警告"
Exit Function
End If
mydate = CDate(mydate) '日期格式转换
For i = 0 To 6
mystr(i) = Choose(i + 1, Year(mydate), MonthName(Month(mydate)), Day(mydate), Hour(mydate), Minute(mydate), Second(mydate), WeekdayName(Weekday(mydate, vbSunday)))
Next
For i = 1 To 4 '生成年
temp = temp & Mid(all, CInt(Mid(mystr(0), i, 1)) + 1, 1) ' trans year to chinese character
Next
mystr(0) = temp & "年"
temp = Choose(Val(mystr(2)) / 10 + 1, "", "十", "二十", "三十") & Trim(Mid(all, Val(mystr(2)) Mod 10 + 1, 1)) & "日" '生成日
mystr(2) = Replace(temp, "十○日", "十日") '避免出现 “三十○日”
temp = Choose(Val(mystr(3)) / 10 + 1, "", "十", "二十") & Trim(Mid(all, Val(mystr(3)) Mod 10 + 1, 1)) & "时" '生成时
mystr(3) = Replace(temp, "十○时", "十时")
temp = Choose(Val(mystr(4)) / 10 + 1, "", "十", "二十", "三十", "四十", "五十") & Trim(Mid(all, Val(mystr(4)) Mod 10 + 1, 1)) & "分" '生成分
mystr(4) = Replace(temp, "十○分", "十分")
temp = Choose(Val(mystr(5)) / 10 + 1, "", "十", "二十", "三十", "四十", "五十") & Trim(Mid(all, Val(mystr(5)) Mod 10 + 1, 1)) & "秒" '生成秒
mystr(5) = Replace(temp, "十○秒", "十秒")
datename = Join(mystr, "")
End Function

Private Sub Command1_Click()
MsgBox datename(Now)
End Sub

posted on 2004-07-21 14:12  王峰炬  阅读(223)  评论(0编辑  收藏  举报

导航