zoukankan      html  css  js  c++  java
  • 日期相關的轉換

    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;
                }
            }

  • 相关阅读:
    [转]zxing二维码的生成和解码 j2se
    [转]Android的网络与通信
    (转)Firefox:浏览器内置数据库之路
    dom Framework oop模块
    我的框架 别名机制
    获取屏幕的PDI值
    mass Framework lang模块
    王云飞 《绿色生活》(附译文)
    dom Framework query模块
    dom Framework核心模块发布!(更新)
  • 原文地址:https://www.cnblogs.com/guyuehuanhuan/p/1948136.html
Copyright © 2011-2022 走看看