Click here to Skip to main content
Click here to Skip to main content

Vietnamese Lunar Calendar for .NET

By , 18 May 2012
 

Introduction  

This is an implementation of Am lich VN (http://www.informatik.uni-leipzig.de/~duc/amlich/) in C# / .NET 2.0. It's useful to calculate the Vietnamese lunar date in a .NET application (and I hope this source code may be used as a build-in for the Microsoft .NET Framework).

Background

This article uses the algorithm of author Ho Ngoc Duc (http://www.informatik.uni-leipzig.de/~duc/) to make a VietnameseCalendar class (it's similar to the ChineseLunarCalendar class - a build-in of Microsoft .NET 2.0).

Using the Code

You may use this class to convert a solar date (Gregorian calendar) to a Vietnamese lunar date and vice versa.

// From VN lunar date to solar date
DateTime d = new DateTime(2008, 9, 1, new VietnameseCalendar());
Console.WriteLine("Solar date of Vietnam New Year - Mau Ty = '{0}'", d);   
// From solar date to VN lunar date
VietnameseCalendar vCal = new VietnameseCalendar();
DateTime dt = new DateTime(2008, 9, 30);
vCal.FromDateTime(dt, out y, out m, out d);
Console.WriteLine("VN lunar date of 2008/9/30 = 'Nam {0} - {1}({1}) - Ngay {3} ({4})
 - Gio bat dau: {5} - Tiet {6}, Gio Hoang dao: {7}",
  VietnameseCalendar.GetYearName(y),
  VietnameseCalendar.GetMonthSpeechName(y, m),
  VietnameseCalendar.GetMonthName(y, m),
  d, VietnameseCalendar.GetDayName(dt),
  VietnameseCalendar.GetHourZeroName(dt),
  VietnameseCalendar.GetMinorSolarTerms(dt),
  VietnameseCalendar.GetPropitiousHour(dt));      

* You can use this class to get VN lunar start hour of a day; or get Minor solar terms; Propitious hours in a day,... 

* A Vietnam lunar month can be 1 to 13 (because of leap-month) as a numeric. Example:
   VN Lunar year 1979 has leap-month: 6
   So this year has 13 months (from 1 to 13, as an int) like:
    [1], [2] ... [5], [6], [7 => 6(N)], [8 (=> 7)], [9] ... [13]
   So, if you convert 1979-9-17, you will received 1979/8/26 as output numbers, but string will be "26, Tháng Bảy, Năm Kỷ Mùi" (if you use the method: GetMonthSpeechName(y, m))

Points of Interest

This class has been tested by unit-testing (in the attached file). Here is that class diagram:

VietnameseCalendar_dotnet2_src

History 

  • 16th October, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

nthachus
Software Developer (Junior)
Vietnam Vietnam
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionThanksmemberaspneter26 May '12 - 8:19 
Many thanks.
QuestionWrong date when you convert in a lunar leap yearmemberNguyen Vu Thuy10 May '12 - 20:23 
I try convert 1979 Sep 17 then get 1979/08/26 instead of 1979/07/26
 
You check in http://www.informatik.uni-leipzig.de/~duc/amlich/ then you will see it 07 because in 1979 we have two leap month (07).
 
Thanks
Richard Nguyen

AnswerRe: Wrong date when you convert in a lunar leap yearmembernthachus11 May '12 - 6:34 
First of all, thanks for your feedback.
I know those bugs because of the VietnameseCalendar.yearsCode data are incorrected.
I will correct those data after I have free time Big Grin | :-D
 
Regards.
NNT

AnswerRe: Wrong date when you convert in a lunar leap yearmembernthachus18 May '12 - 11:55 
VN Lunar year 1979 has leap-month: 6
So this year has 13 months (from 1 to 13, as an int) like:
 [1], [2] ... [5], [6], [7 => 6(N)], [8 (=> 7)], [9] ... [13]
Your checked month 7 is greater than the leap-month, so, it will be returned as 8
NNT

AnswerRe: Wrong date when you convert in a lunar leap yearmemberwentou18 May '12 - 21:48 
The magic of a site!!!
GeneralError when convert to Lunar datememberiSoftVN8 Jun '09 - 17:24 
Hello, I'm using this Class to convert my DOB to Lunar date, but It's wrong. I'm change from dd/mm/yyyy (06/07/1982) to Lunar date. Your Class In convert to dd/mm/yyyy 16/06/1982, But my DBO in Lunar date is 16/05/1982
 
Chào bạn, mình sử dụng để đổi ngày sinh mình thì kết quả không đúng. Mình đổi ngày 6 tháng 7 năm 1982 sang ngày âm lịch, thì KQ lại cho ra là 16/06/1982, nhưng thật sự đúng ra phải là 16/05/1982
 
Mình sử dụng hàm - Using
 
VietnameseCalendar cal = new VietnameseCalendar();
int intLunarDate;
int intLunarMonth;
int intLunarYear;
cal.FromDateTime(new DateTime(1982, 7, 6), out intLunarYear, out intLunarMonth, out intLunarDate);
GeneralRe: Error when convert to Lunar datemembernthachus18 May '12 - 12:08 
A lunar month can be 1 to 13.
* Example:
VN Lunar year 1982 has leap-month: 4
So this year has 13 months (from 1 to 13, as an int) like:
 [1], [2], [3], [4], [5 => 4(N)], [6 (=> 5)], [7] ... [13]
Your lunar month (6), is greater than the leap-month (4), so you will received 1982/6/16 as output numbers, but string: "16, Tháng Năm, Năm Nhâm Tuất" (if you use method:
VietnameseCalendar.GetMonthSpeechName(y, m)
NNT

QuestionLeap Year 2009memberada260324827 Apr '09 - 0:32 
hi,
 
Thanks for your great job.
But there's one problem.
This 2009 is a Leap Year and has 13 months (2 months of May).
25-5-2009 --> 2/5/2009
26-6-2009 --> 3/5/2009 (but VietnameseLunarCalendar converts it to 3/6/2009)
and 15-1-2010 actually is 1/12/2009 (but it show 1/13/2009
AnswerRe: Leap Year 2009membernthachus11 May '12 - 6:38 
Thanks for your feedback.
I will fix that bug as soon as posible (Now I don't have the time to fix it, very sorry Dead | X| )
 
Regards.
NNT

AnswerRe: Leap Year 2009membernthachus18 May '12 - 12:03 
A lunar month can be 1 to 13.
* Example:
VN Lunar year 1979 has leap-month: 6
So this year has 13 months (from 1 to 13, as an int) like:
 [1], [2] ... [5], [6], [7 => 6(N)], [8 (=> 7)], [9] ... [13]
So, if you convert 1979-9-17, you will received 1979/8/26 as output numbers, but string: "26, Tháng Bảy, Năm Kỷ Mùi" (if you use method:
VietnameseCalendar.GetMonthSpeechName(y, m)
NNT

GeneralThanksmemberphamthaihoa25 Apr '09 - 0:37 
Good job Smile | :)
GeneralThank youmemberbienca9 Feb '09 - 15:37 
Thank you for this conversion. The code is well written.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 18 May 2012
Article Copyright 2008 by nthachus
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid