当将回历日期转换为格里高利日期C#Windows应用程序时,单击回车时为什么会从文本框中清除数据?

问题描述 投票:0回答:2

我想将回历日期转换为公历日期,反之亦然,我在此链接中找到以下解决方案类

https://www.codeproject.com/Articles/8838/Convert-date-from-Hijri-Calendar-to-Gregorian-Cale?msg=5688246#xx5688246xx

我尝试过课程代码:

using System;
using System.Web;
using System.Diagnostics;
using System.Globalization;
using System.Data;
using System.Collections;

namespace Bahsas
{
    /// <summary>
    /// Summary description for Dates.
    /// </summary>
    public class Dates
    {
        private HttpContext cur;

        private const int startGreg=1900;
        private const int endGreg=2100;
        private string[] allFormats={"yyyy/MM/dd","yyyy/M/d",
            "dd/MM/yyyy","d/M/yyyy",
            "dd/M/yyyy","d/MM/yyyy","yyyy-MM-dd",
            "yyyy-M-d","dd-MM-yyyy","d-M-yyyy",
            "dd-M-yyyy","d-MM-yyyy","yyyy MM dd",
            "yyyy M d","dd MM yyyy","d M yyyy",
            "dd M yyyy","d MM yyyy"};
        private CultureInfo arCul;
        private CultureInfo enCul;
        private HijriCalendar h;
        private GregorianCalendar g;

        public Dates()
        {
            cur = HttpContext.Current;

            arCul=new CultureInfo("ar-SA");
            enCul=new CultureInfo("en-US");

            h=new  HijriCalendar();
            g=new GregorianCalendar(GregorianCalendarTypes.USEnglish);

            arCul.DateTimeFormat.Calendar=h;           
        }

           /// <summary>
        /// Check if string is hijri date and then return true 
        /// </summary>
        /// <PARAM name="hijri"></PARAM>
        /// <returns></returns>
        public bool IsHijri(string hijri)
        {
            if (hijri.Length<=0)
            {

                cur.Trace.Warn("IsHijri Error: Date String is Empty");
                return false;
            }
            try
            {    
                DateTime tempDate=DateTime.ParseExact(hijri,allFormats,
                     arCul.DateTimeFormat,DateTimeStyles.AllowWhiteSpaces);
                if (tempDate.Year>=startGreg && tempDate.Year<=endGreg)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                cur.Trace.Warn("IsHijri Error :"+hijri.ToString()+"\n"+
                                  ex.Message);
                return false;
            }
        }
        /// <summary>
        /// Check if string is Gregorian date and then return true 
        /// </summary>
        /// <PARAM name="greg"></PARAM>
        /// <returns></returns>
        public bool IsGreg(string greg)
        {
            if (greg.Length<=0)
            {

                cur.Trace.Warn("IsGreg :Date String is Empty");
                return false;
            }
            try
            {    
                DateTime tempDate=DateTime.ParseExact(greg,allFormats,
                    enCul.DateTimeFormat,DateTimeStyles.AllowWhiteSpaces);
                if (tempDate.Year>=startGreg && tempDate.Year<=endGreg)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                cur.Trace.Warn("IsGreg Error :"+greg.ToString()+"\n"+ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Return Formatted Hijri date string 
        /// </summary>
        /// <PARAM name="date"></PARAM>
        /// <PARAM name="format"></PARAM>
        /// <returns></returns>
        public string FormatHijri(string date ,string format)
        {
            if (date.Length<=0)
            {                
                cur.Trace.Warn("Format :Date String is Empty");
                return "";
            }
            try
            {                       
                DateTime tempDate=DateTime.ParseExact(date,
                   allFormats,arCul.DateTimeFormat,DateTimeStyles.AllowWhiteSpaces);
                return tempDate.ToString(format,arCul.DateTimeFormat);                            
            }
            catch (Exception ex)
            {
                cur.Trace.Warn("Date :\n"+ex.Message);
                return "";
            }
        }
        /// <summary>
        /// Returned Formatted Gregorian date string
        /// </summary>
        /// <PARAM name="date"></PARAM>
        /// <PARAM name="format"></PARAM>
        /// <returns></returns>
        public string FormatGreg(string date ,string format)
        {
            if (date.Length<=0)
            {                
                cur.Trace.Warn("Format :Date String is Empty");
                return "";
            }
            try
            {
                DateTime tempDate=DateTime.ParseExact(date,allFormats,
                    enCul.DateTimeFormat,DateTimeStyles.AllowWhiteSpaces);
                return tempDate.ToString(format,enCul.DateTimeFormat);                            
            }
            catch (Exception ex)
            {
                cur.Trace.Warn("Date :\n"+ex.Message);
                return "";
            }
        }
        /// <summary>
        /// Return Today Gregorian date and return it in yyyy/MM/dd format
        /// </summary>
        /// <returns></returns>
        public string GDateNow()
        {
            try
            {
                return DateTime.Now.ToString("yyyy/MM/dd",enCul.DateTimeFormat);
            }
            catch (Exception ex)
            {
                cur.Trace.Warn("GDateNow :\n"+ex.Message);
                return "";
            }
        }
        /// <summary>
        /// Return formatted today Gregorian date based on your format
        /// </summary>
        /// <PARAM name="format"></PARAM>
        /// <returns></returns>
        public string GDateNow(string format)
        {
            try
            {
                return DateTime.Now.ToString(format,enCul.DateTimeFormat);
            }
            catch (Exception ex)
            {
                cur.Trace.Warn("GDateNow :\n"+ex.Message);
                return "";
            }
        } 

        /// <summary>
        /// Return Today Hijri date and return it in yyyy/MM/dd format
        /// </summary>
        /// <returns></returns>
        public string HDateNow()
        {
            try
            {
                return DateTime.Now.ToString("yyyy/MM/dd",arCul.DateTimeFormat);
            }
            catch (Exception ex)
            {
                cur.Trace.Warn("HDateNow :\n"+ex.Message);
                return "";
            }
        }
        /// <summary>
        /// Return formatted today hijri date based on your format
        /// </summary>
        /// <PARAM name="format"></PARAM>
        /// <returns></returns>

        public string HDateNow(string format)
        {
            try
            {
                return DateTime.Now.ToString(format,arCul.DateTimeFormat);
            }
            catch (Exception ex)
            {
                cur.Trace.Warn("HDateNow :\n"+ex.Message);
                return "";
            }            
        }

        /// <summary>
        /// Convert Hijri Date to it's equivalent Gregorian Date
        /// </summary>
        /// <PARAM name="hijri"></PARAM>
        /// <returns></returns>
        public string HijriToGreg(string hijri)
        {            
            if (hijri.Length<=0)
            {

                cur.Trace.Warn("HijriToGreg :Date String is Empty");
                return "";
            }
            try
            {
                DateTime tempDate=DateTime.ParseExact(hijri,allFormats,
                   arCul.DateTimeFormat,DateTimeStyles.AllowWhiteSpaces);
                return tempDate.ToString("yyyy/MM/dd",enCul.DateTimeFormat);
            }
            catch (Exception ex)
            {
                cur.Trace.Warn("HijriToGreg :"+hijri.ToString()+"\n"+ex.Message);
                return "";
            }
        }
        /// <summary>
        /// Convert Hijri Date to it's equivalent Gregorian Date
        /// and return it in specified format
        /// </summary>
        /// <PARAM name="hijri"></PARAM>
        /// <PARAM name="format"></PARAM>
        /// <returns></returns>
        public string HijriToGreg(string hijri,string format)
        {
            if (hijri.Length<=0)
            {
                cur.Trace.Warn("HijriToGreg :Date String is Empty");
                return "";
            }
            try
            {
                DateTime tempDate=DateTime.ParseExact(hijri,
                   allFormats,arCul.DateTimeFormat,DateTimeStyles.AllowWhiteSpaces);
                return tempDate.ToString(format,enCul.DateTimeFormat);
            }
            catch (Exception ex)
            {
                cur.Trace.Warn("HijriToGreg :"+hijri.ToString()+"\n"+ex.Message);
                return "";
            }
        }
        /// <summary>
        /// Convert Gregoian Date to it's equivalent Hijir Date
        /// </summary>
        /// <PARAM name="greg"></PARAM>
        /// <returns></returns>
        public string GregToHijri(string greg)
        {
            if (greg.Length<=0)
            {
                cur.Trace.Warn("GregToHijri :Date String is Empty");
                return "";
            }
            try
            {
                DateTime tempDate=DateTime.ParseExact(greg,allFormats,
                    enCul.DateTimeFormat,DateTimeStyles.AllowWhiteSpaces);
                return tempDate.ToString("yyyy/MM/dd",arCul.DateTimeFormat);                
            }
            catch (Exception ex)
            {
                cur.Trace.Warn("GregToHijri :"+greg.ToString()+"\n"+ex.Message);
                return "";
            }
        }
        /// <summary>
        /// Convert Hijri Date to it's equivalent Gregorian Date and
        /// return it in specified format
        /// </summary>
        /// <PARAM name="greg"></PARAM>
        /// <PARAM name="format"></PARAM>
        /// <returns></returns>
        public string GregToHijri(string greg,string format)
        {            
            if (greg.Length<=0)
            {                
                cur.Trace.Warn("GregToHijri :Date String is Empty");
                return "";
            }
            try
            {               
                DateTime tempDate=DateTime.ParseExact(greg,allFormats,
                    enCul.DateTimeFormat,DateTimeStyles.AllowWhiteSpaces);
                return tempDate.ToString(format,arCul.DateTimeFormat);                
            }
            catch (Exception ex)
            {
                cur.Trace.Warn("GregToHijri :"+greg.ToString()+"\n"+ex.Message);
                return "";
            }
        }

        /// <summary>
        /// Return Gregrian Date Time as digit stamp
        /// </summary>
        /// <returns></returns>
        public string GTimeStamp()
        {
            return GDateNow("yyyyMMddHHmmss");
        }
        /// <summary>
        /// Return Hijri Date Time as digit stamp
        /// </summary>
        /// <returns></returns>
        public string HTimeStamp()
        {
            return HDateNow("yyyyMMddHHmmss");
        }

        /// <summary>
        /// Compare two instances of string date 
        /// and return indication of their values 
        /// </summary>
        /// <PARAM name="d1"></PARAM>
        /// <PARAM name="d2"></PARAM>
        /// <returns>positive d1 is greater than d2,
        /// negative d1 is smaller than d2, 0 both are equal</returns>
        public int Compare(string d1,string d2)
        {
            try
            {
                DateTime date1=DateTime.ParseExact(d1,allFormats,
                    arCul.DateTimeFormat,DateTimeStyles.AllowWhiteSpaces);
                DateTime date2=DateTime.ParseExact(d2,allFormats,
                    arCul.DateTimeFormat,DateTimeStyles.AllowWhiteSpaces);
                return DateTime.Compare(date1,date2);
            }
            catch (Exception ex)
            {
                cur.Trace.Warn("Compare :"+"\n"+ex.Message);
                return -1;
            }
        }
    }
}

然后我创建了2个文本框HijriDate和GreDate,例如,当我用gregorian编写日期并单击ENTER将其转换为Hijri时,就需要用到它,反之亦然。我尝试了以下代码:

private void hijriDate_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Return)
    {
      greDate.text =  date.HijriToGreg(hijriDate.Text);
    }
}

private void greDate_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Return)
    {
       hijriDate.text =  date.GregToHijri(greDate.Text);
    } 
}

[当我在hijridate文本框中输入hijri date并单击ENTER时,Hijridate文本框中的日期将被清除并在greDate文本框中转换为gregorian date,也当我在gredate文本框中输入gregorian date并单击ENTER时,在gredate文本框中将日期清除并转换为hijri hijridate文本框中的日期为什么清除它,我将如何同时保留日期回历​​和公历?

c# .net date-conversion
2个回答
1
投票

我认为您可以检查文本框hijriDate和greDate的多行属性,如果为true,则将其更改为false,当您单击Enter时,它会转到下一行,并且不会隐藏。

然后,如果您需要根据出生日期计算年龄,请使用以下代码:

private void hijriDate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)

                {
                greDate.Text = date.HijriToGreg(hijriDate.Text);
                int age = (int)((DateTime.Now - Convert.ToDateTime(greDate.Text)).TotalDays / 365.242199);
                textAge.Text =  age.ToString();
                greDate.Focus();
            }

        }

0
投票

您是否已验证各个变量中的日期?可能是因为按“ Enter”键已经移动了文本,因此在屏幕上不可见。


我写了以下代码,它工作正常:

    private void hijriDate_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Return)
        {
            greDate.Text = new Dates().HijriToGreg(hijriDate.Text);
        }
    }

    private void greDate_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Return)
        {
            hijriDate.Text = new Dates().GregToHijri(greDate.Text);
        }
    }

ScreenShot

© www.soinside.com 2019 - 2024. All rights reserved.