• 日期相關的轉換


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

  • 相关阅读:
    Python随笔——Map之键对应多值的处理
    Python操作cx_Oracle笔记
    jmeter进阶之Beanshell引用Java代码
    碎片记录——JMeter之 http post json对象与参数化调用,以及beanshell 引用Java源码
    java源码生成可运行jar
    小记:web安全测试之——固定session漏洞
    Git关联JIRA的issue
    APP稳定性测试-monkey执行
    maven之assembly插件
    wrapper配置文件详解
  • 原文地址:https://www.cnblogs.com/guyuehuanhuan/p/1948136.html
Copyright © 2020-2023  润新知