怎样用vb语言编写日历提示程序

怎样用vb语言编写日历提示程序,第1张

用vb自带的calendar类就可以处理

Imports System

Imports System.Globalization

Public Class SamplesCalendar

Public Shared Sub Main()

' Sets a DateTime to April 3, 2002 of the Gregorian calendar.

Dim myDT As New DateTime(2002, 4, 3, New GregorianCalendar())

' Uses the default calendar of the InvariantCulture.

Dim myCal As Calendar = CultureInfo.InvariantCulture.Calendar

' Displays the values of the DateTime.

Console.WriteLine("April 3, 2002 of the Gregorian calendar:")

DisplayValues(myCal, myDT)

' Adds 5 to every component of the DateTime.

myDT = myCal.AddYears(myDT, 5)

myDT = myCal.AddMonths(myDT, 5)

myDT = myCal.AddWeeks(myDT, 5)

myDT = myCal.AddDays(myDT, 5)

myDT = myCal.AddHours(myDT, 5)

myDT = myCal.AddMinutes(myDT, 5)

myDT = myCal.AddSeconds(myDT, 5)

myDT = myCal.AddMilliseconds(myDT, 5)

' Displays the values of the DateTime.

Console.WriteLine("After adding 5 to each component of the DateTime:")

DisplayValues(myCal, myDT)

End Sub 'Main

Public Shared Sub DisplayValues(myCal As Calendar, myDT As DateTime)

Console.WriteLine(" Era: {0}", myCal.GetEra(myDT))

Console.WriteLine(" Year: {0}", myCal.GetYear(myDT))

Console.WriteLine(" Month:{0}", myCal.GetMonth(myDT))

Console.WriteLine(" DayOfYear:{0}", myCal.GetDayOfYear(myDT))

Console.WriteLine(" DayOfMonth: {0}", myCal.GetDayOfMonth(myDT))

Console.WriteLine(" DayOfWeek:{0}", myCal.GetDayOfWeek(myDT))

Console.WriteLine(" Hour: {0}", myCal.GetHour(myDT))

Console.WriteLine(" Minute: {0}", myCal.GetMinute(myDT))

Console.WriteLine(" Second: {0}", myCal.GetSecond(myDT))

Console.WriteLine(" Milliseconds: {0}", myCal.GetMilliseconds(myDT))

Console.WriteLine()

End Sub 'DisplayValues

End Class 'SamplesCalendar

用VB做万年历,非常关键点就是农历写法,参考代码如下:

#Region " 返回农历 "

'返回农历

'cCalendar.MaxSupportedDateTime 返回支持的最大日期,即2101-1-28

'cCalendar.MinSupportedDateTime 返回支持的最小日期,即190-2-19

Private cCalendar As New System.Globalization.ChineseLunisolarCalendar

Public Function PubFunGet_CNDate(ByVal sDateTime As Date) As String

cCalendar = New System.Globalization.ChineseLunisolarCalendar

Dim lyear As Integer = cCalendar.GetYear(sDateTime)

Dim lmonth As Integer = cCalendar.GetMonth(sDateTime)

Dim lday As Integer = cCalendar.GetDayOfMonth(sDateTime)

Dim lweek As Integer = cCalendar.GetDayOfWeek(sDateTime)

'获取闰月, 0 则表示没有闰月

Dim leapMonth As Integer = cCalendar.GetLeapMonth(lyear)

Dim isleap As Boolean = False

If (leapMonth >0) Then

If (leapMonth = lmonth) Then

'闰月

isleap = True

lmonth = lmonth - 1

ElseIf (lmonth >leapMonth) Then

lmonth = lmonth - 1

End If

End If

Return String.Concat(GetLunisolarYear(lyear), IIf(isleap = True, "闰年", "年"), GetLunisolarMonth(lmonth), "月", GetLunisolarDay(lday))

End Function

'十天干

Private tiangan As String() = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"}

'十二地支

Private dizhi As String() = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"}

'十二升哪生肖

Private shengxiao As String() = {"鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "枯笑轿猴", "鸡", "狗", "猪"}

'农历月

Private months As String() = {"正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二(腊)"}

'农历日

Private days1 As String() = {"初", "十", "廿", "三"}

Private days As String() = {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十"}

'返回农历年(天干 地支 生肖)

Private Function GetLunisolarYear(ByVal year As Integer) As String

GetLunisolarYear = ""

If (year >3) Then

Dim tgIndex As Integer = (year - 4) Mod 10

Dim dzIndex As Integer = (year - 4) Mod 12

Return tiangan(tgIndex) &dizhi(dzIndex) &"[" &shengxiao(dzIndex) &"]"

End If

'无效的年份!

End Function

'没肆返回生肖

Private Function GetShengXiao(ByVal sDateTime As Date) As String

Return shengxiao(cCalendar.GetTerrestrialBranch(cCalendar.GetSexagenaryYear(sDateTime)) - 1)

End Function

'返回农历月

Private Function GetLunisolarMonth(ByVal month As Integer) As String

GetLunisolarMonth = ""

If (month <13 AndAlso month >0) Then

Return months(month - 1)

End If

'无效的月份!

End Function

'返回农历日

Private Function GetLunisolarDay(ByVal day As Integer) As String

GetLunisolarDay = ""

If (day >0 AndAlso day <32) Then

If (day <>20 AndAlso day <>30) Then

Return String.Concat(days1((day - 1) \ 10), days((day - 1) Mod 10))

Else

Return String.Concat(days((day - 1) \ 10), days1(1))

End If

End If

'无效的日!

End Function

#End Region


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12514983.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存