Algorithm for converting the Gregorian calendar and the lunar calendar in VB. NET

Source: Internet
Author: User

'The following is a calendar algorithm about VB.

'Date data definition method:

'The first 12 bytes represent 1-12 months for a large month or a small month, 1 for a large month for 30 days, and 0 for a small month for 29 days;

'1st digit indicates a leap month. The value 1 indicates 30 days for a large month, and the value 0 indicates 29 days for a small month;

'1970 is the month of A leap month. If it is not the month of A leap month, it is given A month. The values of 10, 11, and 12 are represented by A, B, and C, respectively, in hexadecimal notation;

'The last four digits are the Lunar New Year of the current year. January 1 The date of the Gregorian calendar. For example, the date 0131 represents January 31 .

Function GetChgDate (ByVal vDate As String, ByVal ChgType As conversion method) As String

Dim vvdate () As String = Split (vDate ,"-")

If vvdate (0)> 2011 Or vvdate (0) <1900 Then

Throw New NotSupportedException ("This function only provides calendar conversion from January 1, 1900 to January 1, 2011! ")

End If

Dim AddMonth, AddDay, AddYear, getDay, I As Integer

Dim RunYue As Boolean

Dim conDate As DateTime

Dim daList (2011) As String

DaList (1900) = "010010110110180131"

DaList (1901) = "010010101110000219"

DaList (1902) = "101001010111000208"

DaList (1903) = "010100100110150129"

DaList (1904) = "110100100110000216"

DaList (1905) = "110110010101000204"

DaList (1906) = "011010101010140125"

DaList (1907) = "010101101010000213"

DaList (1908) = "100110101101000202"

DaList (1909) = "010010101110120122"

DaList (1910) = "010010101110000210"

DaList (1911) = "101001001101160130"

DaList (1912) = "101001001101000218"

DaList (1913) = "110100100101000206"

DaList (1914) = "110101010100150126"

DaList (1915) = "101101010101000214"

DaList (1916) = "010101101010000204"

DaList (1917) = "100101101101020123"

DaList (1918) = "100101011011000211"

DaList (1919) = "010010011011170201"

DaList (1920) = "010010011011000220"

DaList (1921) = "101001001011000208"

DaList (1922) = "101100100101150128"

DaList (1923) = "011010100101000216"

DaList (1924) = "011011010100000205"

DaList (1925) = "101011011010140124"

DaList (1926) = "001010110110000213"

DaList (1927) = "100101010111000202"

DaList (1928) = "010010010111120123"

DaList (1929) = "010010010111000210"

DaList (1930) = "011001001011060130"

DaList (1931) = "110101001010000217"

DaList (1932) = "111010100101000206"

DaList (1933) = "011011010100150126"

DaList (1934) = "010110101101000214"

DaList (1935) = "001010110110000204"

DaList (1936) = "100100110111030124"

DaList (1937) = "100100101110000211"

DaList (1938) = "110010010110170131"

DaList (1939) = "110010010101000219"

DaList (1940) = "110101001010000208"

DaList (1941) = "110110100101060127"

DaList (1942) = "101101010101000215"

DaList (1943) = "010101101010000205"

DaList (1944) = "101010101101140125"

DaList (1945) = "001001011101000213"

DaList (1946) = "100100101101000202"

DaList (1947) = "110010010101120122"

DaList (1948) = "101010010101000210"

DaList (1949) = "101101001010170129"

DaList (1950) = "011011001010000217"

DaList (1951) = "101101010101000206"

DaList (1952) = "010101011010150127"

DaList (1953) = "010011011010000214"

DaList (1954) = "101001011011000203"

DaList (1955) = "010100101011130124"

DaList (1956) = "010100101011000212"

DaList (1957) = "101010010101080131"

DaList (1958) = "111010010101000218"

DaList (1959) = "011010101010000208"

DaList (1960) = "101011010101060128"

DaList (1961) = "101010110101000215"

DaList (1962) = "010010110110000205"

DaList (1963) = "101001010111040125"

DaList (1964) = "101001010111000213"

DaList (1965) = "010100100110000202"

DaList (1966) = "111010010011030121"

DaList (1967) = "110110010101000209"

DaList (1968) = "010110101010170130"

DaList (1969) = "010101101010000217"

DaList (1970) = "100101101101000206"

DaList (1971) = "010010101110150127"

DaList (1972) = "010010101101000215"

DaList (1973) = "101001001101000203"

DaList (1974) = "110100100110140123"

DaList (1975) = "110100100101000211"

DaList (1976) = "110101010010180131"

DaList (1977) = "101101010100000218"

DaList (1978) = "101101101010000207"

DaList (1979) = "100101101101060128"

DaList (1980) = "100101011011000216"

DaList (1981) = "010010011011000205"

DaList (1982) = "101001001011140125"

DaList (1983) = "101001001011000213"

DaList (1984) = "1011001001011A0202"

DaList (1985) = "011010100101000220"

DaList (1986) = "011011010100000209"

DaList (1987) = "101011011010060129"

DaList (1988) = "101010110110000217"

DaList (1989) = "100100110111000206"

DaList (1990) = "010010010111150127"

DaList (1991) = "010010010111000215"

DaList (1992) = "011001001011000204"

Dalist (1993) = "011010100101030123"

Dalist (1994) = "111010100101000210"

Dalist (1995) = "011010110010180131"

Dalist (1996) = "010110101100000219"

Dalist (1997) = "101010110110000207"

Dalist (1998) = "100100110110150128"

Dalist (1999) = "100100101110000216"

Dalist (2000) = "110010010110000205"

Dalist (2001) = "110101001010140124"

Dalist (2002) = "110101001010000212"

Dalist (2003) = "110110100101000201"

Dalist (2004) = "010110101010120122"

Dalist (2005) = "010101101010000209"

Dalist (2006) = "101010101101170129"

Dalist (2007) = "001001011101000218"

Dalist (2008) = "100100101101000207"

Dalist (2009) = "110010010101150126"

Dalist (2010) = "101010010101000214"

DaList (2011) = "101101001010000214"

AddYear = vvdate (0)

If ChgType = Conversion method.

AddMonth = Val (Mid (daList (AddYear), 15, 2 ))

AddDay = Val (Mid (daList (AddYear), 17, 2 ))

ConDate = DateSerial (AddYear, AddMonth, AddDay)

AddDay = vvdate (2)

For I = 1 To vvdate (1)-1

AddDay = AddDay + 29 + Val (Mid (daList (vvdate (0), I, 1 ))

Next I

Return DateAdd (DateInterval. Day, AddDay-1, conDate)

Else

CHUSHIHUA:

AddMonth = Val (Mid (daList (AddYear), 15, 2 ))

AddDay = Val (Mid (daList (AddYear), 17, 2 ))

ConDate = DateSerial (AddYear, AddMonth, AddDay)

Dim vtDate As DateTime

Try

VtDate = CType (vDate, DateTime)

Catch ex As Exception

Throw New NotSupportedException ("Date input error! ")

End Try

GetDay = DateDiff (DateInterval. Day, conDate, vtDate)

If getDay <0 Then AddYear = AddYear-1: GoTo CHUSHIHUA

AddDay = 1

AddMonth = 1

For I = 1 To getDay

AddDay = AddDay + 1

If AddDay = 30 + Mid (daList (AddYear), AddMonth, 1) Or (RunYue And AddDay = 30 + Mid (daList (AddYear), 13, 1) Then

If RunYue = False And AddMonth = Val ("& H" & Mid (daList (AddYear), 14, 1) Then

RunYue = True

Else

RunYue = False

AddMonth = AddMonth + 1

End If

AddDay = 1

End If

Next

Return AddYear & "-" & AddMonth & "-" & AddDay

End If

End Function

Enum Conversion Method

Lunar calendar to Gregorian calendar = 1

Gregorian to Lunar Calendar = 0

End Enum

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.