How to: Convert Miladi to Shamsi in C#


How to convert Miladi to Shamsi in C#:

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

 

namespace WindowsApplication2

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            //********* Test ************

            MessageBox.Show(Shamsi(2005, 8, 14).ToShortDateString());

            MessageBox.Show(todayShamsi().ToShortDateString());

        }

 

        private DateTime todayShamsi()

        {

            DateTime today;

            today = DateTime.Today;

            string year = today.Year.ToString();

            string month = today.Month.ToString();

            string day = today.Day.ToString();

            int Y = int.Parse(year);

            int M = int.Parse(month);

            int D = int.Parse(day);

            return Shamsi(Y, M, D);

        }

        private DateTime Shamsi(int Y, int M, int D)

        {

            if (Y == 0)

                Y = 2000;

            if (Y < 100)

                Y = Y + 1900;

            if (Y == 2000)

            {

                if (M > 2)

                {

                    DateTime Temp = new DateTime(Y, M, D);

                    Temp = Temp.AddDays(1);

                    Y = int.Parse(Temp.Year.ToString());

                    M = int.Parse(Temp.Month.ToString());

                    D = int.Parse(Temp.Day.ToString());

                }

            }

            if (M < 3 || (M == 3 && D < 21))

                Y = Y - 622;

            else Y = Y - 621;

            switch (M)

            {

                case 1: if (D < 21)

                    {

                        M = 10;

                        D = D + 10;

                    }

                    else

                    {

                        M = 11;

                        D = D - 20;

                    }

                    break;

 

                case 2: if (D < 20)

                    {

                        M = 11;

                        D = D + 11;

                    }

                    else

                    {

                        M = 12;

                        D = D - 19;

                    }

                    break;

 

                case 3:

                    if (D < 21)

                    {

                        M = 12;

                        D = D + 9;

                    }

                    else

                    {

                        M = 1;

                        D = D - 20;

                    }

                    break;

 

                case 4:

                    if (D < 21)

                    {

                        M = 1;

                        D = D + 11;

                    }

                    else

                    {

                        M = 2; D = D - 20;

                    }

                    break;

 

                case 5:

                    if (D < 22)

                    {

                        M = M - 3;

                        D = D + 10;

                    }

                    else

                    {

                        M = M - 2;

                        D = D - 21;

                    }

                    break;

 

                case 6:

                    if (D < 22)

                    {

                        M = M - 3;

                        D = D + 10;

                    }

                    else

                    {

                        M = M - 2;

                        D = D - 21;

                    }

                    break;

 

                case 7:

                    if (D < 23)

                    {

                        M = M - 3;

                        D = D + 9;

                    }

                    else

                    {

                        M = M - 2;

                        D = D - 22;

                    }

                    break;

 

                case 8:

                    if (D < 23)

                    {

                        M = M - 3;

                        D = D + 9;

                    }

                    else

                    {

                        M = M - 2;

                        D = D - 22;

                    }

                    break;

 

                case 9:

                    if (D < 23)

                    {

                        M = M - 3;

                        D = D + 9;

                    }

                    else

                    {

                        M = M - 2;

                        D = D - 22;

                    }

                    break;

 

                case 10:

                    if (D < 23)

                    {

                        M = 7;

                        D = D + 8;

                    }

                    else

                    {

                        M = 8;

                        D = D - 22;

                    }

                    break;

 

                case 11:

                    if (D < 22)

                    {

                        M = M - 3;

                        D = D + 9;

                    }

                    else

                    {

                        M = M - 2;

                        D = D - 21;

                    }

                    break;

 

                case 12:

                    if (D < 22)

                    {

                        M = M - 3;

                        D = D + 9;

                    }

                    else

                    {

                        M = M - 2;

                        D = D - 21;

                    }

                    break;

            }

            DateTime Temp1 = new DateTime(Y, M, D);

            return Temp1;

        }

    }

}


Recommended Free Ebook
Similar Articles