Click here to Skip to main content
15,881,281 members
Articles / Programming Languages / Visual Basic
Article

Shamsi Convertor

Rate me:
Please Sign up or sign in to vote.
3.54/5 (16 votes)
11 Jan 20062 min read 111.4K   2.3K   22   15
Convert Hijri Shamsi Date to Gregorian and Gregorian to Hijri Shamsi

Introduction

One of the main iranian programmers is converting Date of conputer to Hijri Shamsi Date. The Hijri date have to kind. First Hijri Shamsi that it dates is related to Sun and the second one is hijri Ghamari that related to moon. the Hijri Shamsi is one of the most exact calendars in the world and it's made by Khayyam (iranian astrologer).

but as you know computer calendar work with gregorian calendar and any other kind of calendars need to convert from gregorian. this article try to convert Gregorian date to Hijri Shamsi and Hijri Shamsi date to Gregorian.

The main must we know for this converting is the diffrence of this calndars is 621 years. but one main problem is leap years in both calendars.

In Hijri Shamsi calendar the years that the remainder of their devide to 33 be one of 1, 5, 9, 13, 17, 22, 26, 30 this numbers is leap year.

And in Gregorian we the date class of .NET can calculate for us the gregorian year.
example:

VB.NET
Date.IsLeapYear(2005)

this function return a bool value.

Calculating Shamsi Date

Time Line of years

The point number 1 in picture show the start date of Shamsi Year

The point number 2 show Gregorian start date

the point number 3 show finish date of Shamsi year

The point number 4 show finish date of Gregorian year

First we devision 621 years from Gregorian year.

Year = GregorianDate.Year - 621

at this point we will be some where between point number 2 and 4. If we were after number 3 it means the diffrence year is 621 years but if we were before point number 3 the diffrence of years will be 622 years.

For finding this:

<BR>If ShamsiIsLeap(Year - 1) = True And Date.IsLeapYear(GregorianDate.Year) Then<BR>    ToEid = 80 'To Eid is diffrence between point number 2 and 3<BR>Else<BR>    ToEid = 79<BR>End If

If the passed days of Gregorian date were fewer than ToEid it means diffrence of years is 622 Years

If GregorianDate.DayOfYear <= ToEid Then<BR>            Year -= 1 'Diffrence is 622 Years<BR>            Elapsed = 286 + GregorianDate.DayOfYear<BR>            If ShamsiIsLeap(Year) And Not Date.IsLeapYear(GregorianDate.Year) Then<BR>                Elapsed += 1<BR>            End If<BR>        Else<BR>            Elapsed = GregorianDate.DayOfYear - ToEid<BR>        End If

Elapsed is passed days of Shamsi year. and finally we convert passed days to Shamsi Date

DayOfYearToShamsi(Elapsed)
For converting Shamsi date to Gregorian we do this:
Private Sub ToGregorian()<BR>        Dim Y As Integer<BR>        Y = Year + 621<BR>        Dim DOY As Integer = DayOfYear()<BR>        Dim ToEid As Integer<BR>        Dim YearDays As Integer<BR>        If ShamsiIsLeap(Year - 1) And Date.IsLeapYear(Y) Then<BR>            ToEid = 80<BR>            YearDays = 366<BR>        Else<BR>            ToEid = 79<BR>            YearDays = 365<BR>        End If<BR>        Dim GDays As Integer = ToEid + DOY<BR>        If GDays > YearDays Then<BR>            GDays -= YearDays<BR>            Y += 1<BR>        End If<BR>        DayOfYearToGregorian(GDays, Y)<BR>End Sub<BR>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalmain form missing Pin
Marwan11111-Jun-11 13:45
Marwan11111-Jun-11 13:45 
GeneralCode Not Works Pin
Anubhava Dimri25-Oct-09 23:38
Anubhava Dimri25-Oct-09 23:38 
GeneralIt works wrong Pin
Mohammad Abedi30-Jan-09 0:26
Mohammad Abedi30-Jan-09 0:26 
QuestionC# shamsi Pin
Maryam_F28-Jul-06 21:34
Maryam_F28-Jul-06 21:34 
Generalshamsi Pin
shereen20063-Apr-06 2:48
shereen20063-Apr-06 2:48 
GeneralRe: shamsi Pin
Hamed J.I3-Apr-06 4:41
Hamed J.I3-Apr-06 4:41 
GeneralCan't Download this code Pin
a_mohammadi_m12-Mar-06 18:37
a_mohammadi_m12-Mar-06 18:37 
GeneralRe: Can't Download this code Pin
Hamed J.I17-Mar-06 5:45
Hamed J.I17-Mar-06 5:45 
GeneralShamsi date in sql server Pin
Hossein Hayatbakhsh13-Feb-06 23:53
Hossein Hayatbakhsh13-Feb-06 23:53 
GeneralRe: Shamsi date in sql server Pin
Hamed J.I14-Feb-06 21:40
Hamed J.I14-Feb-06 21:40 
GeneralPersia .NET API Pin
Majid Shahabfar11-Jan-06 8:41
Majid Shahabfar11-Jan-06 8:41 
General.Net 2 and accuracy of algorithms Pin
Homam Hosseini11-Jan-06 5:42
Homam Hosseini11-Jan-06 5:42 
GeneralRe: .Net 2 and accuracy of algorithms Pin
Hamed J.I12-Jan-06 8:16
Hamed J.I12-Jan-06 8:16 
As you said there's bug on calculating Shamsi date in this algorithm i've tested it again and i didn't find any bug i think there's no problem in algorithm.

can you get an exact date that algorithm can't calculate. i have tested so many date even more than 200 years.

thank's for your attention

Hamed JI
GeneralRe: .Net 2 and accuracy of algorithms Pin
Hojjat Salmasian13-Jan-06 9:15
Hojjat Salmasian13-Jan-06 9:15 
GeneralRe: .Net 2 and accuracy of algorithms Pin
Hamed J.I14-Jan-06 20:17
Hamed J.I14-Jan-06 20:17 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.