C#数据转换类ConvertEx,封装.Net的Convert对象
作者:C/S框架网|www.cscode.ne  发布日期:2021-03-11 12:10:01
  C#数据转换类ConvertEx,封装.Net的Convert对象


C#数据转换类ConvertEx,封装.Net的Convert对象



C# Code:

/// <summary>
///C#数据转换类ConvertEx,封装.Net的Convert对象.
/// </summary>
public class ConvertEx
{
  
/// <summary>
  
/// 将日期转换为指定格式的字符串
  
/// </summary>
  
/// <param name="dt">日期</param>
  
/// <param name="format">日期格式, 如: yyyy-MM-dd</param>
  
/// <returns>返回字符串类型</returns>
  
public static string ToDateString(DateTime dt, string format)
  {
    
return dt.ToString(format, DateTimeFormatInfo.CurrentInfo);
  }
  
  
/// <summary>
  
/// 将日期转换为年/月/日格式的字符串
  
/// </summary>
  
/// <param name="dt">日期</param>
  
/// <returns>返回字符串类型</returns>
  
public static string ToCharYYYYMMDDLong(DateTime dt)
  {
    
return dt.ToString("yyyy/MM/dd", DateTimeFormatInfo.CurrentInfo);
  }
  
  
/// <summary>
  
/// 将日期转换为年月(YYMM)格式的字符串
  
/// </summary>
  
/// <param name="dt">日期</param>
  
/// <returns>返回字符串类型</returns>
  
public static string ToCharYYMM(DateTime dt)
  {
    
return dt.ToString("yyMM", DateTimeFormatInfo.CurrentInfo);
  }
  
  
/// <summary>
  
/// 将日期转换为年(YY)格式的字符串
  
/// </summary>
  
/// <param name="dt">日期</param>
  
/// <returns>返回字符串类型</returns>
  
public static string ToCharYY(DateTime dt)
  {
    
return dt.ToString("yy", DateTimeFormatInfo.CurrentInfo);
  }
  
  
/// <summary>
  
/// 将日期转换为年(YYYY)格式的字符串
  
/// </summary>
  
/// <param name="dt">日期</param>
  
/// <returns>返回字符串类型</returns>
  
public static string ToCharYYYY(DateTime dt)
  {
    
return dt.ToString("yyyy", DateTimeFormatInfo.CurrentInfo);
  }
  
  
  
/// <summary>
  
/// 将日期转换为年月(YYYYMM)格式的字符串
  
/// </summary>
  
/// <param name="dt">日期</param>
  
/// <returns>返回字符串类型</returns>
  
public static string ToCharYYYYMM(DateTime dt)
  {
    
return dt.ToString("yyyyMM", DateTimeFormatInfo.CurrentInfo);
  }
  
  
/// <summary>
  
/// 将日期转换为年月日(YYYYMMDD)格式的字符串,当日期为系统最小日期否返回空字符串
  
/// </summary>
  
/// <param name="dt">日期</param>
  
/// <returns>返回字符串类型</returns>
  
public static string ToCharYYYYMMDD(DateTime dt)
  {
    
string temp = dt.ToString("yyyyMMdd", DateTimeFormatInfo.CurrentInfo);
    
if (temp == "00010101") temp = "";
    
return temp;
  }
  
  
/// <summary>
  
/// 将日期转换为年月日时分秒(YYYYMMDDHHMMSS)格式的字符串
  
/// </summary>
  
/// <param name="dt">日期</param>
  
/// <returns>返回字符串类型</returns>
  
public static string ToCharYYYYMMDDHHMMSS(DateTime dt)
  {
    
return dt.ToString("yyyyMMddHHmmss", DateTimeFormatInfo.CurrentInfo);
  }
  
  
public static string ToCharYYYYMMDDHHMM(DateTime dt)
  {
    
return dt.ToString("yyyyMMddHHmm", DateTimeFormatInfo.CurrentInfo);
  }
  
  
/// <summary>
  
/// 将日期转换为日/月/年 时:分:秒(DD/MM/YYYY HH:MM:SS)格式的字符串
  
/// </summary>
  
/// <param name="dt">日期</param>
  
/// <returns>返回字符串类型</returns>
  
public static string ToCharDD_MM_YYYY_HHMMSS(DateTime dt)
  {
    
return dt.ToString("dd/MM/yyyy HH:mm:ss", DateTimeFormatInfo.CurrentInfo);
  }
  
  
/// <summary>
  
/// 将日期转换为年/月/日 时:分:秒(YYYY/MM/DD HH:MM:SS)格式的字符串
  
/// </summary>
  
/// <param name="dt">日期</param>
  
/// <returns>返回字符串类型</returns>
  
public static string ToCharYYYY_MM_DD_HHMMSS(DateTime dt)
  {
    
return dt.ToString("yyyy/MM/dd HH:mm:ss", DateTimeFormatInfo.CurrentInfo);
  }
  
  
/// <summary>
  
/// 将日期转换为年月日 时:分:秒(YYYYMMDD HH:MM:SS)格式的字符串
  
/// </summary>
  
/// <param name="dt">日期</param>
  
/// <returns>返回字符串类型</returns>
  
public static string ToCharYYYYMMDDHHMMSS000(DateTime dt)
  {
    
return dt.ToString("yyyyMMdd HH:mm:ss:000", DateTimeFormatInfo.CurrentInfo);
  }
  
  
/// <summary>
  
/// 将日期转换为年月日(YYMMDD)格式的字符串
  
/// </summary>
  
/// <param name="dt">日期</param>
  
/// <returns>返回字符串类型</returns>
  
public static string ToCharYYMMDD(DateTime dt)
  {
    
return dt.ToString("yyMMdd", DateTimeFormatInfo.CurrentInfo);
  }
  
  
/// <summary>
  
/// 转换为Boolean类型
  
/// </summary>
  
/// <param name="o">必须是可转换为Boolean类型的字符串如"True","False",或其它特殊对象</param>
  
/// <returns>返回bool类型</returns>
  
public static bool ToBoolean(object o)
  {
    
if (null == o) return false;
    
try
    {
      
return Convert.ToBoolean(o.ToString());
    }
    
catch { return false; }
  }
  
  
/// <summary>
  
/// 转换为浮点类型
  
/// </summary>
  
/// <param name="o"></param>
  
/// <returns></returns>
  
public static float ToFloat(object o)
  {
    
if (null == o) return 0;
    
try
    {
      
return (float)Convert.ToDouble(o.ToString());
    }
    
catch { return 0; }
  }
  
  
/// <summary>
  
/// 转换为decimal类型(有效十进制数)
  
/// </summary>
  
/// <param name="o"></param>
  
/// <returns></returns>
  
public static decimal ToDecimal(object o)
  {
    
if (null == o) return 0;
    
try
    {
      
return Convert.ToDecimal(o.ToString());
    }
    
catch { return 0; }
  }
  
  
/// <summary>
  
/// 四舍五入
  
/// </summary>
  
/// <param name="d"></param>
  
/// <returns></returns>
  
public static object ToDecimalFormat(decimal d,int round,string format= "0.00")
  {
    
return (object)(decimal.Round(d, round).ToString(format));
  }
  
  
/// <summary>
  
/// 转换为日期对象
  
/// </summary>
  
/// <param name="o">日期对象(泛型),如为空的字段数据DBNull.NULL</param>
  
/// <returns></returns>
  
public static DateTime ToDateTime(DateTime? o)
  {
    
if (null == o) return DateTime.MinValue;
    
try
    {
      
return DateTime.Parse(o.ToString());
    }
    
catch { return DateTime.MinValue; }
  }
  
  
/// <summary>
  
/// 转换为SQL支持的日期格式, 日期范围:1753-1-1 ~ 9999-12-31
  
/// </summary>
  
/// <param name="o"></param>
  
/// <returns></returns>
  
public static DateTime ToSqlDateTime(object o)
  {
    DateTime defMinValue
= DateTime.Parse("1753-01-01 00:00:00");
    DateTime defMaxValue
= DateTime.Parse("9999-12-31 23:59:59");
    
if (null == o) return defMinValue;//传入空值,返回预设值
    

    DateTime dt;
    
if (DateTime.TryParse(o.ToString(), out dt))
    {
      
if (dt < defMinValue || dt > defMaxValue)
      
return defMinValue;//无效日期,预设返回SQL支持的最小日期
      
else
      
return dt;
    }
    
return defMinValue;
  }
  
  
/// <summary>
  
/// 转换日期字符串. 对象为空则返回'null'字符串(无单引号).
  
/// 用于动态组合SQL字符串.
  
/// </summary>
  
/// <param name="o">日期对象</param>
  
/// <param name="dateFormat">日期格式</param>
  
/// <param name="quotationMark">返回的日期字符串是否打上单引号</param>
  
public static string ToDateTimeString(object o, string dateFormat, bool quotationMark)
  {
    
try
    {
      DateTime
? dateGenType = ToDateTime(o);
      
string datestr = string.Empty;
      
if (dateGenType == null)
      
return "";
      
else
      {
        DateTime dt
= DateTime.Parse(dateGenType.ToString());
        datestr
= dt.ToString(dateFormat, DateTimeFormatInfo.CurrentInfo);
      }
      
if (quotationMark)
      
return "'" + datestr + "'";
      
else
      
return datestr;
    }
    
catch
    {
      
return "";
    }
  }
  
  
/// <summary>
  
/// 转换为日期对象
  
/// </summary>
  
/// <param name="o"></param>
  
/// <returns></returns>
  
public static DateTime? ToDateTime(object o)
  {
    
if (null == o) return null;
    
try
    {
      
return Convert.ToDateTime(o.ToString());
    }
    
catch { return null; }
  }
  
  
public static DateTime ToDateTimeEx(object o)
  {
    
if (null == o) return DateTime.MinValue;
    
try
    {
      DateTime dt
= Convert.ToDateTime(o.ToString());
      
if (dt < DateTime.Parse("1900-01-01"))
      
return DateTime.Parse("1900-01-01");
      
else
      
return dt;
    }
    
catch { return DateTime.MinValue; }
  }
  
  
/// <summary>
  
/// 转换为整数
  
/// </summary>
  
/// <param name="o"></param>
  
/// <returns></returns>
  
public static int ToInt(object o)
  {
    
if (null == o) return 0;
    
try
    {
      
return Convert.ToInt32(o.ToString());
    }
    
catch { return 0; }
  }
  
  
/// <summary>
  
/// 转换为字符串
  
/// </summary>
  
/// <param name="obj">当对象为空,返回string.Empty</param>
  
/// <returns></returns>
  
public static string ToString(object obj)
  {
    
if (obj == null) return string.Empty;
    
return obj.ToString().Trim();
  }
  
  
/// <summary>
  
/// 截取左侧指定长度字符串
  
/// </summary>
  
/// <param name="content">字符串</param>
  
/// <param name="length">长度</param>
  
/// <returns></returns>
  
public static string StrLeft(string content, int length)
  {
    
if (String.IsNullOrEmpty(content)) return "";
    
if (content.Length <= length)
    
return content;
    
else
    
return content.Substring(0, length);
  }
  
}


//来源:C/S框架网 | www.csframework.com | QQ:23404761



C/S框架网|原创精神.创造价值.打造精品

扫一扫加微信
C/S框架网作者微信 C/S框架网|原创作品.质量保障.竭诚为您服务


上一篇 下一篇