Intern Day69

Convert进行数据类型的转换:

  • Convert.ToInt16()转换为short

    • 范围:-32768 ~ 32767
  • Convert.ToInt32()转换为int

    • Convert.ToInt32(null):返回0但不会产生异常

    • 范围:-2,147,483,648 ~ 2,147,483,647

    • 适合:将Object类型转换为int

    • 一般用这个比较多

  • Convert.ToInt64()转换为long

  • 范围:-9223372036854775808 ~ 9223372036854775808

  • int.Parse(string sParameter)

    • 是个构造函数,参数类型只支持string

    • int.Parse(null) 会产生异常

  • Convert.ToString():转换为string

  • Convert.ToDateTime():转换为日期型(datetime)

  • System.DateTime currentTime=new System.DateTime();

  • 取当前年月日时分秒:currentTime=System.DateTime.Now;

  • 取当前年 int 年/月/日/时/分/秒=currentTime.Year/Month/……;

  • 获取系统当前日期(含时间):DateTime.Now

  • 获取系统当前日期(不含时间):DateTime.Today

  • Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); 对时间进行转换,转换成yyyy-MM-dd HH:mm:ss的格式

原文地址:https://www.cnblogs.com/OFSHK/p/14706807.html