Velocity Reviews > convert gregorian date to jalay date

# convert gregorian date to jalay date

h_ghanaty
Guest
Posts: n/a

 11-07-2003
how i can convert gregorian date to jalaly(Iran date)
with vb.net

Bret Mulvey [MS]
Guest
Posts: n/a

 11-09-2003
"h_ghanaty" <(E-Mail Removed)> wrote in message
news:0a5101c3a51b\$3f5bb2a0\$(E-Mail Removed)...
> how i can convert gregorian date to jalaly(Iran date)
> with vb.net

I don't know VB.NET but I came up with this C# code which you can probably
translate pretty easily. It uses the 8-of-33 leap year method which only
works within a certain range of years because of how the Jalali leap years
are calculated.

const long ticksPerDay = 864000000000L;

static int[] leapFlag =
{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0 ,0,1,0,0,0,1,0,0};

public static void GetJalaliDate(DateTime time, out int year, out int month,
out int day)
{
const int daysIn33Years = 365 * 33 + 8;
int d = (int) (time.Ticks / ticksPerDay) - 226529;
year = (d / daysIn33Years) * 33;
d %= daysIn33Years;
for (int i=0; i<33; i++)
{
int daysInYear = 365 + leapFlag[i];
if (d < daysInYear)
break;
d -= daysInYear;
year++;
}
if (d < 186)
{
month = 1 + d / 31;
day = 1 + d % 31;
}
else
{
d -= 186;
month = 7 + d / 30;
day = 1 + d % 30;
}
}