示例#1
0
        static void Main(string[] args)
        {
            Debug.WriteLine(ConvertDate.ToFa(DateTime.Now));      //1393/08/01

            Debug.WriteLine(ConvertDate.ToFa(DateTime.Now, "d")); //93/08/01
            Debug.WriteLine(ConvertDate.ToFa(DateTime.Now, "D")); //پنج شنبه, 01 آبان 1393

            Debug.WriteLine(ConvertDate.ToFa(DateTime.Now, "t")); //21:53
            Debug.WriteLine(ConvertDate.ToFa(DateTime.Now, "T")); //21:53:26

            Debug.WriteLine(ConvertDate.ToFa(DateTime.Now, "g")); //93/08/01 21:53
            Debug.WriteLine(ConvertDate.ToFa(DateTime.Now, "G")); //93/08/01 21:53:26


            Debug.WriteLine(ConvertDate.ToFa(DateTime.Now, "f")); //پنج شنبه, 01 آبان 1393 21:53
            Debug.WriteLine(ConvertDate.ToFa(DateTime.Now, "F")); //پنج شنبه, 01 آبان 1393 21:53:26

            Debug.WriteLine(ConvertDate.ToFa(DateTime.Now, "m")); //آبان 1
            Debug.WriteLine(ConvertDate.ToFa(DateTime.Now, "M")); //

            Debug.WriteLine(ConvertDate.ToFa(DateTime.Now, "y")); //1393 آبان
            Debug.WriteLine(ConvertDate.ToFa(DateTime.Now, "Y")); //1393 آبان
            //new standard formats
            Debug.WriteLine("new standard formats ");
            Debug.WriteLine(ConvertDate.ToFa(DateTime.Now, "yy MMM"));          //93 آبان
            Debug.WriteLine(ConvertDate.ToFa(DateTime.Now, "yyyy/MM/dd "));     //1393/8/9
            Debug.WriteLine(ConvertDate.ToFa(DateTime.Now, "yy-M-d "));         //93-8-09
            Debug.WriteLine(ConvertDate.ToFa(DateTime.Now, "ddd dd MMM yyyy")); //جمعه 9 آبان 1393

            //testing convert back to DateTime from persian string
            Debug.WriteLine("ToEn test (testing convert back to DateTime from persian string) ");

            Debug.WriteLine(ConvertDate.ToEn("1393/08/01")); //2014/10/23 00:00:00
            Debug.WriteLine(ConvertDate.ToEn("01/8/1393"));  //2014/10/23 00:00:00
            Debug.WriteLine(ConvertDate.ToEn("1/8/1393"));   //2014/10/23 00:00:00
            Debug.WriteLine(ConvertDate.ToEn("1-8-93"));     //2014/10/23 00:00:00
            Debug.WriteLine(ConvertDate.ToEn("93-8-01"));    //2014/10/23 00:00:00
            Debug.WriteLine(ConvertDate.ToEn("93 8 01"));    //2014/10/23 00:00:00
            //extra spaces and different separators are handled
            Debug.WriteLine(ConvertDate.ToEn("1_8_1393 "));  //2014/10/23 00:00:00
            Debug.WriteLine(ConvertDate.ToEn(" 1_8_1393 ")); //2014/10/23 00:00:00
            Debug.WriteLine(ConvertDate.ToEn(" 1.8.1393 ")); //2014/10/23 00:00:00


            Console.WriteLine("check visual studio output window");
            Console.WriteLine("press any key to continue");
            Console.ReadKey();
        }
示例#2
0
        private void calculateDate()
        {
            DateTime datestart = DateTime.Now;


            string finishFa = maskedTextBox1.Text;

            if (finishFa == null || finishFa == "")
            {
                finishFa = "1397/5/29";
            }
            //string to FaDateTime
            FaDateTime faDate    = new  FaDateTime();
            FaDate     dateFarsi = faDate.getFaDateTime(finishFa);

            if (dateFarsi.day > faDate.getthisMonthsDay())
            {
                MessageBox.Show("خطا روز بزرگتر از روز های ماه است");
                return;
            }
            if ((dateFarsi.month < dateFarsi.thisMonth && dateFarsi.year < dateFarsi.thisYear) || (dateFarsi.year < dateFarsi.thisYear))
            {
                MessageBox.Show("خطا  سال یا ماه کوچکتر از مقدار معین است");
                return;
            }
            try {
                DateTime finishTime = ConvertDate.ToEn(finishFa);
                TimeSpan difference = finishTime - datestart;

                int days      = dateFarsi.toEndMonth;
                int daystoend = dateFarsi.toEndMonth;
                if (daystoend >= faDate.getthisMonthsDay())
                {
                    daystoend = daystoend - faDate.getthisMonthsDay();
                    this.month++;
                }
                s = difference.Seconds;
                m = difference.Minutes;
                h = difference.Hours;

                this.month = ((finishTime.Year - datestart.Year) * 12) + finishTime.Month - datestart.Month;;
                this.d     = daystoend;
            }
            catch (Exception ex)
            {
            }
        }