博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

日期相關的轉換

Posted on 2011-01-31 00:31  ☆Keep★Moving☆  阅读(160)  评论(0编辑  收藏  举报

private DateTime? StringToDateTime(string strDate)
        {
            try
            {
                if (strDate.Trim().Length <= 0)
                {
                    return null;
                }
                else
                {
                    return DateTime.ParseExact(strDate, "d/M/yyyy", System.Globalization.CultureInfo.InvariantCulture);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

 

‍ public const string GC_DateTimeFormat_OnlyDate = "dd/MM/yyyy";
        public const string GC_TimeFormat = "HH:mm";
        /// <summary>
        /// dd/MM/yyyy HH:mm:ss
        /// </summary>
        public const string GC_DateTimeFormat = "dd/MM/yyyy HH:mm:ss";
        /// <summary>
        /// dd/MM/yyyy HH:mm
        /// </summary>
        public const string GC_SmallDateTimeFormat = "dd/MM/yyyy HH:mm";

 

 

public static DateTime StringToDateTime(string strDate)
        {
            try
            {
                if (strDate.Trim().Length <= 0)
                {
                    return DateTime.MinValue;
                }
                else
                {
                    return DateTime.ParseExact(strDate, GC_DateTimeFormat_OnlyDate, System.Globalization.CultureInfo.InvariantCulture);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public static DateTime StringToDateTime(string strDate, string strTime)
        {
            try
            {
                if (strDate.Trim().Length <= 0)
                {
                    return DateTime.MinValue;
                }
                else
                {
                    if (strTime.Trim() == "")
                    {
                        return StringToDateTime(strDate);
                    }
                    else
                    {
                        return DateTime.ParseExact(strDate + " " + strTime, GC_DateTimeFormat_OnlyDate + " " + GC_TimeFormat, System.Globalization.CultureInfo.InvariantCulture);
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// 顯示格式dd/mm/yyyy
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string DateTimeToString(DateTime dt)
        {
            try
            {
                if (dt == DateTime.MinValue)
                {
                    return "";
                }
                else
                {
                    DateTime temp = DateTime.Parse(dt.ToString());
                    return temp.ToString(GC_DateTimeFormat_OnlyDate);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// 顯示自定格式的日期

        /// </summary>
        /// <param name="dt"></param>
        /// <param name="strDatetimeFormate"></param>
        /// <returns></returns>
        public static string DateTimeToString(DateTime dt, string strDatetimeFormate)
        {
            try
            {
                if (dt == DateTime.MinValue)
                {
                    return "";
                }
                else
                {
                    DateTime temp = DateTime.Parse(dt.ToString());
                    return temp.ToString(strDatetimeFormate);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static DateTime? StringToNullableDateTime(string strDate)
        {
            try
            {
                if (strDate.Trim().Length <= 0)
                {
                    return null;
                }
                else
                {
                    return DateTime.ParseExact(strDate, GC_DateTimeFormat, System.Globalization.CultureInfo.InvariantCulture);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public static DateTime? StringToNullableDateTime(string strDate, string strTime)
        {
            try
            {
                if (strDate.Trim().Length <= 0)
                {
                    return null;
                }
                else
                {
                    if (strTime.Trim() == "")
                    {
                        return StringToNullableDateTime(strDate);
                    }
                    else
                    {
                        return DateTime.ParseExact(strDate + " " + strTime, GC_DateTimeFormat + " " + GC_TimeFormat, System.Globalization.CultureInfo.InvariantCulture);
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static string NullableDateTimeToString(DateTime? dt)
        {
            try
            {
                if (dt == null)
                {
                    return "";
                }
                else
                {
                    DateTime temp = DateTime.Parse(dt.ToString());
                    return temp.ToString(GC_DateTimeFormat);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static string NullableDateTimeToString(DateTime? dt, string strDatetimeFormate)
        {
            try
            {
                if (dt == null)
                {
                    return "";
                }
                else
                {
                    DateTime temp = DateTime.Parse(dt.ToString());
                    return temp.ToString(strDatetimeFormate);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
       

        public static string NullableDateTimeToString_OnlyTime(DateTime? dt)
        {
            try
            {
                if (dt == null)
                {
                    return "";
                }
                else
                {
                    DateTime temp = DateTime.Parse(dt.ToString());
                    return temp.ToString(GC_TimeFormat);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }