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