Mojtaba SH
Mojtaba SH

Reputation: 171

How to set a persian calender on windows form in C#

using this code I force the textbox to only accept persian words:

private void CommenttxtBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    System.Globalization.CultureInfo Language = 
        new System.Globalization.CultureInfo("Fa-ir");

    InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(Language);
}

and whit a timer I put an english calender on my form:

private void ShowTime_Tick(object sender, EventArgs e)
{
    ShowTimelbl.Text = DateTime.Now.ToString();
}

is there anyway to make the datetime shows persian (jalali) time ?

Upvotes: 1

Views: 1114

Answers (4)

Hossein Ahmadi
Hossein Ahmadi

Reputation: 1

using System.Globalization;

DateTime dt = DateTime.Now;
PersianCalendar jc = new PersianCalendar();
private string[] weeks = new string[] {"شنبه", "یکشنبه", "دوشنبه", "سه شنبه", "چهارشنبه", "پنجشنبه", "جمعه" };
private string[] months = new string[] { "فروردین", "اردیبهشت", "خرداد", "تیر","مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند" };
private string[] days = new string[] { "یک", "دو", "سه", "چهار", "پنج", "شش", "هفت", "هشت", "نه", "ده", "یازده", "دوازده", "سیزده", "چهارده", "پانزده", "شانزده", "هفده", "هجده", "نوزده", " بیست", "بیست و یک", "بیست و دوم", "بیست و سوم", "بیست و چهارم", "بیست و پنجم", "بیست و ششم", "بیست و هفتم", "بیست و هشتم", "بیست و نهم", "سی" };
private void Persian_Date_Load(object sender, EventArgs e)
    {
       lbl.Text="امروز"+"\r\n"+ jc.GetYear(DateTime.Now) + "/" + jc.GetMonth(DateTime.Now) + "/" + jc.GetDayOfMonth(DateTime.Now)+"\r\n"+ jc.GetYear(DateTime.Now).ToString()+" "+weeks[(int)jc.GetDayOfWeek(DateTime.Now)+1] +" "+ days[jc.GetDayOfMonth(DateTime.Now)-1]+ " " + months[jc.GetMonth(DateTime.Now) - 1];

    }

Upvotes: 0

Mojtaba SH
Mojtaba SH

Reputation: 171

tank you both, using your guide I edited the cod to also shows the time:

        private void ShowTime_Tick(object sender, EventArgs e)
    { 
        var PersianCalender = new PersianCalendar();
        var datetime = DateTime.Now;
        var year = PersianCalender.GetYear(datetime);
        var month = PersianCalender.GetMonth(datetime);
        var day = PersianCalender.GetDayOfMonth(datetime);
        var hour = PersianCalender.GetHour(datetime);
        var minute = PersianCalender.GetMinute(datetime);
        var second = PersianCalender.GetSecond(datetime);

        var datetimeshow = string.Format
                     ("{0}:{1}:{2}  {3}/{4}/{5}", hour, minute, second, year, month, day);
        ShowTimelbl.Text = datetimeshow;
    }

Upvotes: 1

Domysee
Domysee

Reputation: 12846

You can pass the CultureInfo to the ToString method:

DateTime.Now.ToString(new System.Globalization.CultureInfo("Fa-ir"));

Result:

"03/22/2016 12:51:28 ب.ظ"

Upvotes: 3

Alireza
Alireza

Reputation: 5503

You need to create an instance of PersianCalendar class. It can convert any DateTime value to persian year, month and day of month.

var persianCalendar = new PersianCalendar();
var dateTime = DateTime.Now;
var year = persianCalendar.GetYear(dateTime);
var month = persianCalendar.GetMonth(dateTime);
var day = persianCalendar.GetDayOfMonth(dateTime);
var date = string.Format("{0}/{1}/{2}", year, month, day);

then show this date string to the user.

Upvotes: 3

Related Questions