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

  • 相关阅读:
    JAVAEE网上商城项目总结
    色盲小游戏
    jQuery(动画效果)
    Oracle exp,imp,expdp,impdp数据导入导出
    Sysbench压力测试工具简介和使用(二)
    Sysbench压力测试工具简介和使用(一)
    Eclipse常用快捷键汇总
    常用数据库连接URL地址大全
    H2数据库使用
    DbVisualizer 解决中文乱码问题
  • 原文地址:https://www.cnblogs.com/guyuehuanhuan/p/1948136.html
Copyright © 2011-2022 走看看