需要将其它类型的变量,转换为字符串类型的一些常见方法与属性。

  字符型转换为字符串

          // C 货币
2.5.ToString("C"); // ¥2.50
// D 10进制数
.ToString("D5"); // 25000
// E 科学型
.ToString("E"); // 2.500000E+005
// F 固定点
.ToString("F2"); // 25.00 "F?"表示保持几位小数
// G 常规
2.5.ToString("G"); // 2.5
// N 数字
.ToString("N"); // 2,500,000.00
// X 16进制
.ToString("X"); // FF
// C# 日期格式
DateTime dt = DateTime.Now;
dt.ToString();//2005-11-5 13:21:25
dt.ToFileTime().ToString();//127756416859912816
dt.ToFileTimeUtc().ToString();//127756704859912816
dt.ToLocalTime().ToString();//2005-11-5 21:21:25
dt.ToLongDateString().ToString();//2005年11月5日
dt.ToLongTimeString().ToString();//13:21:25
dt.ToOADate().ToString();//38661.5565508218
dt.ToShortDateString().ToString();//2005-11-5
dt.ToShortTimeString().ToString();//13:21
dt.ToUniversalTime().ToString();//2005-11-5 5:21:25
dt.Year.ToString();//2005
dt.Date.ToString();//2005-11-5 0:00:00
dt.DayOfWeek.ToString();//Saturday
dt.DayOfYear.ToString();//309
dt.Hour.ToString();//13
dt.Millisecond.ToString();//441
dt.Minute.ToString();//30
dt.Month.ToString();//11
dt.Second.ToString();//
dt.Ticks.ToString();//632667942284412864
dt.TimeOfDay.ToString();//13:30:28.4412864
dt.ToString();//2005-11-5 13:47:04
dt.AddYears().ToString();//2006-11-5 13:47:04
dt.AddDays(1.1).ToString();//2005-11-6 16:11:04
dt.AddHours(1.1).ToString();//2005-11-5 14:53:04
dt.AddMilliseconds(1.1).ToString();//2005-11-5 13:47:04
dt.AddMonths().ToString();//2005-12-5 13:47:04
dt.AddSeconds(1.1).ToString();//2005-11-5 13:47:05
dt.AddMinutes(1.1).ToString();//2005-11-5 13:48:10
dt.AddTicks().ToString();//2005-11-5 13:47:04
dt.CompareTo(dt).ToString();//0
dt.Add(?).ToString();//问号为一个时间段
dt.Equals("2005-11-6 16:11:04").ToString();//False
dt.Equals(dt).ToString();//True
dt.GetHashCode().ToString();//1474088234
dt.GetType().ToString();//System.DateTime
dt.GetTypeCode().ToString();//DateTime
dt.GetDateTimeFormats('s')[].ToString();//2005-11-05T14:06:25
dt.GetDateTimeFormats('t')[].ToString();//14:06
dt.GetDateTimeFormats('y')[].ToString();//2005年11月
dt.GetDateTimeFormats('D')[].ToString();//2005年11月5日
dt.GetDateTimeFormats('D')[].ToString();//2005 11 05
dt.GetDateTimeFormats('D')[].ToString();//星期六 2005 11 05
dt.GetDateTimeFormats('D')[].ToString();//星期六 2005年11月5日
dt.GetDateTimeFormats('M')[].ToString();//11月5日
dt.GetDateTimeFormats('f')[].ToString();//2005年11月5日 14:06
dt.GetDateTimeFormats('g')[].ToString();//2005-11-5 14:06
dt.GetDateTimeFormats('r')[].ToString();//Sat, 05 Nov 2005 14:06:25 GMT
string.Format("{0:d}", dt);//2005-11-5
string.Format("{0:D}", dt);//2005年11月5日
string.Format("{0:f}", dt);//2005年11月5日 14:23
string.Format("{0:F}", dt);//2005年11月5日 14:23:23
string.Format("{0:g}", dt);//2005-11-5 14:23
string.Format("{0:G}", dt);//2005-11-5 14:23:23
string.Format("{0:M}", dt);//11月5日
string.Format("{0:R}", dt);//Sat, 05 Nov 2005 14:23:23 GMT
string.Format("{0:s}", dt);//2005-11-05T14:23:23
string.Format("{0:t}", dt);//14:23
string.Format("{0:T}", dt);//14:23:23
string.Format("{0:u}", dt);//2005-11-05 14:23:23Z
string.Format("{0:U}", dt);//2005年11月5日 6:23:23

toString("X2")

转化为16进制字符串。
大写X:ToString("X2")即转化为大写的16进制。
小写x:ToString("x2")即转化为小写的16进制。
2表示输出两位,不足2位的前面补0,如 0x0A 如果没有2,就只会输出0xA

转载:https://m.jb51.net/article/17013.htm

C#字符串与时间格式化的更多相关文章

  1. Python学习笔记 (2) :字符串输出、操作、格式化和日期、时间格式化

    一.字符串输出及运算 1.常用输出格式及方法 ')#单引号 ")#双引号 """)#三个引号 1234567890 1234567890 1234567890 ...

  2. C# 学习笔记(二) 时间格式化字符串

    1. 以下4种时间格式化符号输出的固定时间格式在各个区域设置中都应是相同的: 标准格式字符串 由 DateTimeFormatInfo.InvariantInfo 属性定义 自定义格式字符串 “O”或 ...

  3. iOS 获取当前时间格式化字符串

    iOS 获取当前时间格式化字符串 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保 ...

  4. 表单序列化json字符串和js时间格式化

    js时间格式化 new Date().format("时间格式") Date.prototype.format = function(fmt) { var o = {        ...

  5. strftime 日期时间格式化

    strftime() 函数根据区域设置格式化本地时间/日期,函数的功能将时间格式化,或者说格式化一个时间字符串. size_t strftime(char *strDest,size_t maxsiz ...

  6. ie 与 Chrome 时间格式化问题.

    ie 与 Chrome 时间格式化通用: new Date(res[i].Time.replaceAll("-", "/")).format("yyy ...

  7. 【转】深入理解Java:SimpleDateFormat安全的时间格式化

    [转]深入理解Java:SimpleDateFormat安全的时间格式化 想必大家对SimpleDateFormat并不陌生.SimpleDateFormat 是 Java 中一个非常常用的类,该类用 ...

  8. Java日期工具类,Java时间工具类,Java时间格式化

    Java日期工具类,Java时间工具类,Java时间格式化 >>>>>>>>>>>>>>>>>&g ...

  9. SimpleDateFormat时间格式化存在线程安全问题

    想必大家对SimpleDateFormat并不陌生.SimpleDateFormat 是 Java 中一个非常常用的类,该类用来对日期字符串进行解析和格式化输出,但如果使用不小心会导致非常微妙和难以调 ...

随机推荐

  1. selenium webdriver学习--------iframe的处理

    有时候我们在定位一个页面元素的时候发现一直定位不了,反复检查自己写的定位器没有任何问题,代 码也没有任何问题.这时你就要看一下这个页面元素是否在一个iframe中,这可能就是找不到的原因之一.如果你在 ...

  2. Linux之find命令

    1.find命令的作用 主要用于操作系统文件.目录的查找. 2.find命令常用参数 -name #按文件名查找 -type #按文件类型查找:b/p/c/p/l/f -size #但文件大小查找,G ...

  3. Java语法进阶12-集合

    集合 集合:是一种容器,用来装对象的容器,不能装基本数据类型. 数组也是容器,可以用来装基本数据类型,也可以用来装对象. 本质上,集合需要用对应的数据结构实现,是多个类实现接口Collection系列 ...

  4. 转:Eclipse中创建Maven版的Web工程(详解)

    一.搭建步骤 ♦首先创建一个Maven的Project,如下图: ♦点击Next,勾选 Create a simple project ♦点击Next,注意Packing要选择war,因为我们创建的是 ...

  5. SpringCache自定义过期时间及自动刷新

    背景前提 阅读说明(十分重要) 对于Cache和SpringCache原理不太清楚的朋友,可以看我之前写的文章:Springboot中的缓存Cache和CacheManager原理介绍 能关注Spri ...

  6. 浏览器url访问tomcat出现错误 java.lang.NoSuchMethodError解决方法

    一般该类错误: 找不到方法或找不到类, 都是maven pom 仓库依赖的问题,有时是 因为缺少该依赖类,可以考虑添加相关依赖: 有时因为依赖冲突, 可以到 maven 的仓库下面把 有关该类的包 全 ...

  7. openstack网络(二)

    连接物理服务器 单网卡 多网卡 跨主机安装网络服务 使用单个控制节点安装 使用专用网络节点安装 连接物理服务器 每个物理主机所需的网卡数量取决于云平台使用领域,组织的安全性和性能要求以及硬件的可用性. ...

  8. openlayers4 入门开发系列之前端动态渲染克里金插值 kriging 篇(附源码下载)

    前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...

  9. BZOJ2440完全平方数(莫比乌斯反演)

    Description 小 X 自幼就很喜欢数.但奇怪的是,他十分讨厌完全平方数.他觉得这些数看起来很令人难受.由此,他也讨厌所有是完全平方数的正整数倍的数.然而这丝毫不影响他对其他数的热爱. 这天是 ...

  10. CF579 - A Raisinng bacteria

    You are a lover of bacteria. You want to raise some bacteria in a box. Initially, the box is empty. ...