12,243,729 members (39,023 online)
alternative version

81.5K views
20 bookmarked
Posted

# Shamsi Convertor

, 11 Jan 2006
 Rate this:
Convert Hijri Shamsi Date to Gregorian and Gregorian to Hijri Shamsi
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->

## 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:

`Date.IsLeapYear(2005)`

this function return a bool value.

## Calculating Shamsi Date

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:

```If ShamsiIsLeap(Year - 1) = True And Date.IsLeapYear(GregorianDate.Year) Then
ToEid = 80 'To Eid is diffrence between point number 2 and 3
Else
ToEid = 79
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
Year -= 1 'Diffrence is 622 Years
Elapsed = 286 + GregorianDate.DayOfYear
If ShamsiIsLeap(Year) And Not Date.IsLeapYear(GregorianDate.Year) Then
Elapsed += 1
End If
Else
Elapsed = GregorianDate.DayOfYear - ToEid
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()
Dim Y As Integer
Y = Year + 621
Dim DOY As Integer = DayOfYear()
Dim ToEid As Integer
Dim YearDays As Integer
If ShamsiIsLeap(Year - 1) And Date.IsLeapYear(Y) Then
ToEid = 80
YearDays = 366
Else
ToEid = 79
YearDays = 365
End If
Dim GDays As Integer = ToEid + DOY
If GDays > YearDays Then
GDays -= YearDays
Y += 1
End If
DayOfYearToGregorian(GDays, Y)
End Sub
```

A list of licenses authors might use can be found here

## Share

 Web Developer Iran (Islamic Republic of)
I began programming with C/C++ when i was 15. Then try to learn VC++ but at the middle of my reading .NET came.

I began to read C# and VB.NET and also began designing basic websites by FrontPage and developed some websites for our school and some other companis.

Later learn Microcontroller and design some digital circuits with PIC microcontrollers for a industrial controller company.

As I learned SQL and ASP.NET developed some website such as news portals that are active now.

Now i'm a software student and teach programming in computer institues. And have my own job by getting projects from companies.

## You may also be interested in...

 First Prev Next
 main form missing Marwan11111-Jun-11 14:45 Marwan111 11-Jun-11 14:45
 Code Not Works eg_Anubhava26-Oct-09 0:38 eg_Anubhava 26-Oct-09 0:38
 It works wrong AlexQII30-Jan-09 1:26 AlexQII 30-Jan-09 1:26
 C# shamsi Maryam_F28-Jul-06 22:34 Maryam_F 28-Jul-06 22:34
 shamsi shereen20063-Apr-06 3:48 shereen2006 3-Apr-06 3:48
 Re: shamsi Hamed_ji3-Apr-06 5:41 Hamed_ji 3-Apr-06 5:41
 Shamsi date in sql server Hossein Hayatbakhsh14-Feb-06 0:53 Hossein Hayatbakhsh 14-Feb-06 0:53
 Re: Shamsi date in sql server Hamed_ji14-Feb-06 22:40 Hamed_ji 14-Feb-06 22:40
 Persia .NET API Majid Shahabfar11-Jan-06 9:41 Majid Shahabfar 11-Jan-06 9:41
 .Net 2 and accuracy of algorithms Homam8511-Jan-06 6:42 Homam85 11-Jan-06 6:42
 Re: .Net 2 and accuracy of algorithms Hamed_ji12-Jan-06 9:16 Hamed_ji 12-Jan-06 9:16
 Re: .Net 2 and accuracy of algorithms Hojjats13-Jan-06 10:15 Hojjats 13-Jan-06 10:15
 Re: .Net 2 and accuracy of algorithms Hamed_ji14-Jan-06 21:17 Hamed_ji 14-Jan-06 21:17
 Last Visit: 31-Dec-99 19:00     Last Update: 30-Apr-16 23:36 Refresh 1