none
Converting Persian Date to Gregorian Date and Gregorian to Persian Date using System.Globalization RRS feed

  • Question

  • I am working on a project where user have Gregorian and Persian Date selection for the their transactions. In database i have two fields TPersianDate and TGregorianDate, if a user select Persian date on the insert button i want to convert the same date to Gregorian and insert it in TGregorianDate and vice versa.

    Tuesday, January 22, 2019 10:24 AM

All replies

  • Take a look at the example in MSDN for PersianCalendar. It demonstrates showing a date in both Gregorian and Persian calendars. That is the code you'll need.

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, January 22, 2019 3:03 PM
    Moderator
  • Hi Naveed Karimi

    Thank you for posting here.

    According to your description, you want to convert Persian Date to Gregorian Date and convert Gregorian Date to Persian Date in your project.

    You could try the following code to achieve it.

      private void button1_Click(object sender, EventArgs e)
            {
                if(radper.Checked==true)
                {
                    PersianCalendar pc = new PersianCalendar();
                    DateTime thisDate = Convert.ToDateTime(this.textBox1.Text);
                    this.textBox2.Text = string.Format("{0},{1}/{2}/{3} {4}:{5}:{6}\n", pc.GetDayOfWeek(thisDate),
                              pc.GetMonth(thisDate),
                              pc.GetDayOfMonth(thisDate),
                              pc.GetYear(thisDate),
                              pc.GetHour(thisDate),
                              pc.GetMinute(thisDate),
                              pc.GetSecond(thisDate));
                }
                if (radgre.Checked==true)
                {
                    PersianCalendar persianCalendar = new PersianCalendar();
                    string year = textBox1.Text.Substring(textBox1.Text.LastIndexOf('/')+1,4);
                    string month = textBox1.Text.Substring(textBox1.Text.IndexOf(',')+1, 2);
                    string day = textBox1.Text.Substring(textBox1.Text.IndexOf('/')+1, 2);
                    string hour = textBox1.Text.Substring(textBox1.Text.IndexOf(' ')+1, 1);
                    string minute = textBox1.Text.Substring(textBox1.Text.IndexOf(':')+1, 2);
                    string second= textBox1.Text.Substring(textBox1.Text.LastIndexOf(':')+1, 2);
                    DateTime dateTime = persianCalendar.ToDateTime(Convert.ToInt32(year), Convert.ToInt32(month), Convert.ToInt32(day), Convert.ToInt32(hour), Convert.ToInt32(minute), Convert.ToInt32(second), 0);
                    string date1 = dateTime.ToString();
                    string time = null;
                    if(Convert.ToInt32(hour)<=12)
                    {
                        time = "AM";
                    }
                    else
                    {
                       hour = (Convert.ToInt32(hour) - 12).ToString();
                        time = "PM";
                    }
                    string date =  textBox1.Text.Substring(0,textBox1.Text.IndexOf(',')+1)+ date1.Substring(date1.IndexOf('/')+1, 2)+ date1.Substring(date1.LastIndexOf('/') + 1, 2)+"/"+date1.Substring(0,date1.IndexOf('/'))+" "+hour+":"+minute+":"+second+" "+time;
                    this.textBox2.Text = date;
                }
            }
    

    Result:

    Hope my advice could be helpful.

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 23, 2019 6:41 AM
    Moderator