一、数字转换到字符串

格式说明符     说明       示例                         输出

C                 货币      2.5.ToString("C")       ¥2.50

D               十进制数  25.ToString("D5")      00025

E                科学型    25000.ToString("E")   2.500000E+005

F                固定点    25.ToString("F2")       25.00

G               常规       2.5.ToString("G")        2.5

N               数字       2500000.ToString("N") 2,500,000.00

X              十六进制   255.ToString("X")       FF

固定长度一个数值其它方法: i=1;

i.ToString().PadLeft(10,'0'); 固定长度为10,左不足补0,结果为0000000001;

i.ToString("N").PadLeft(10,'0');结果为00000001.00;

i.ToString().PadLeft(10,' ');结果为 1;

二、日期转换到字符串

 

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();//28

dt.Ticks.ToString();//632667942284412864

dt.TimeOfDay.ToString();//13:30:28.4412864

dt.ToString();//2005-11-5 13:47:04

dt.AddYears(1).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(1).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(1000).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')[0].ToString();//2005-11-05T14:06:25

dt.GetDateTimeFormats('t')[0].ToString();//14:06

dt.GetDateTimeFormats('y')[0].ToString();//2005年11月

dt.GetDateTimeFormats('D')[0].ToString();//2005年11月5日

dt.GetDateTimeFormats('D')[1].ToString();//2005 11 05

dt.GetDateTimeFormats('D')[2].ToString();//星期六 2005 11 05

dt.GetDateTimeFormats('D')[3].ToString();//星期六 2005年11月5日

dt.GetDateTimeFormats('M')[0].ToString();//11月5日

dt.GetDateTimeFormats('f')[0].ToString();//2005年11月5日 14:06

dt.GetDateTimeFormats('g')[0].ToString();//2005-11-5 14:06

dt.GetDateTimeFormats('r')[0].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

string.Format("{0:Y}",dt);//2005年11月

string.Format("{0}",dt);//2005-11-5 14:23:23

string.Format("{0:yyyyMMddHHmmssffff}",dt);

// 计算2个日期之间的天数差

DateTime dt1 = Convert.DateTime("2007-8-1");

DateTime dt2 = Convert.DateTime("2007-8-15");

TimeSpan span = dt2.Subtract(dt1);

int dayDiff = span.Days + 1;

// 计算某年某月的天数

int days = DateTime.DaysInMonth(2007, 8);

days = 31;

// 给日期增加一天、减少一天

DateTime dt =DateTime.Now;

dt.AddDays(1); //增加一天

dt.AddDays(-1);//减少一天

日期格式模式 说明

d 月中的某一天。一位数的日期没有前导零。

dd 月中的某一天。一位数的日期有一个前导零。

ddd 周中某天的缩写名称,在 AbbreviatedDayNames 中定义。

dddd 周中某天的完整名称,在 DayNames 中定义。

M 月份数字。一位数的月份没有前导零。

MM 月份数字。一位数的月份有一个前导零。

MMM 月份的缩写名称,在 AbbreviatedMonthNames 中定义。

MMMM 月份的完整名称,在 MonthNames 中定义。

y 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示不具有前导零的年份。

yy 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示具有前导零的年份。

yyyy 包括纪元的四位数的年份。

gg 时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。

h 12 小时制的小时。一位数的小时数没有前导零。

hh 12 小时制的小时。一位数的小时数有前导零。

H 24 小时制的小时。一位数的小时数没有前导零。

HH 24 小时制的小时。一位数的小时数有前导零。

m 分钟。一位数的分钟数没有前导零。

mm 分钟。一位数的分钟数有一个前导零。

s 秒。一位数的秒数没有前导零。

ss 秒。一位数的秒数有一个前导零。

f 秒的小数精度为一位。其余数字被截断。

ff 秒的小数精度为两位。其余数字被截断。

fff 秒的小数精度为三位。其余数字被截断。

ffff 秒的小数精度为四位。其余数字被截断。

fffff 秒的小数精度为五位。其余数字被截断。

ffffff 秒的小数精度为六位。其余数字被截断。

fffffff 秒的小数精度为七位。其余数字被截断。

t 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项的第一个字符(如果存在)。

tt 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项(如果存在)。

z 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数没有前导零。例如,太平洋标准时间是“-8”。

zz 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数有前导零。例如,太平洋标准时间是“-08”。

zzz 完整时区偏移量(“+”或“-”后面跟有小时和分钟)。一位数的小时数和分钟数有前导零。例如,太平洋标准时间是“-08:00”。

: 在 TimeSeparator 中定义的默认时间分隔符。

/ 在 DateSeparator 中定义的默认日期分隔符。

% c 其中 c 是格式模式(如果单独使用)。如果格式模式与原义字符或其他格式模式合并,则可以省略“%”字符。

\ c 其中 c 是任意字符。照原义显示字符。若要显示反斜杠字符,请使用“\\”。

只有上面第二个表中列出的格式模式才能用于创建自定义模式;在第一个表中列出的标准格式字符不能用于创建自定义模式。

自定义模式的长度至少为两个字符;例如,

DateTime.ToString( "d"); // 返回 DateTime 值;“d”是标准短日期模式。

DateTime.ToString( "%d"); // 返回月中的某天;“%d”是自定义模式。

DateTime.ToString( "d "); // 返回后面跟有一个空白字符的月中的某天;“d”是自定义模式。

.NET中ToString()的用法的更多相关文章

  1. C#中string.format用法详解

    C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...

  2. 【转】C#中HttpWebRequest的用法详解

    本文实例讲述了C#中HttpWebRequest的用法.分享给大家供大家参考.具体如下: HttpWebRequest类主要利用HTTP 协议和服务器交互,通常是通过 GET 和 POST 两种方式来 ...

  3. C# 中Datatime类用法总结

    C# 中Datatime类用法总结 收集了一些记录下来,这些有的是从网上找的,有些是自己使用到的: DateTime dt = DateTime.Now; dt.ToString();//2005-1 ...

  4. C#中string.Format 用法详解

    这篇文章主要介绍了C#中string.format用法,以实例形式较为详细的讲述了string.format格式化的各种用法,非常具有实用价值,需要的朋友可以参考下 本文实例总结了C#中string. ...

  5. WPF中DataGrid中的DataGridCheckBoxColumn用法(全选,全否,反选)

    原文:WPF中DataGrid中的DataGridCheckBoxColumn用法(全选,全否,反选) 前台代码 <DataGrid.Columns> <DataGridCheckB ...

  6. Scala进阶之路-Scala中的枚举用法案例展示

    Scala进阶之路-Scala中的枚举用法案例展示 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Scala中的枚举值和Java中的枚举值有点差别,不过使用起来也都差大同小异,我这 ...

  7. .net中单选按钮RadioButton,RadioButtonList 以及纯Html中radio的用法实例?

    .net中单选按钮RadioButton,RadioButtonList 以及纯Html中radio的用法,区别? RadioButton实例及说明: <asp:RadioButton ID=& ...

  8. .NET中的枚举用法浅析

    本文简单分析了.NET中的枚举用法.分享给大家供大家参考.具体分析如下: 我理解的枚举就是编程中约定的一个“可选值”:例如QQ的在线状态,分别有    在线,Q我吧,隐身,忙碌等等...我觉得这就是一 ...

  9. java成神之——java中string的用法

    java中String的用法 String基本用法 String分割 String拼接 String截取 String换行符和format格式化 String反转字符串和去除空白字符 String获取 ...

随机推荐

  1. linux下安装phpredis

    一.redis安装 1. yum install redis (不行的先执行:yum install epel-release,再yum install redis) 2.启动服务 redis-ser ...

  2. $_REQUEST变量数组header()函数

    $_SERVER 包含http信息头,路径和服务器端的一些信息,没发送一次HTTP请求,就会创建一个$_SERVER数组Array ( [HTTP_HOST] => localhost [HTT ...

  3. C# 操作XML文件,用XML文件保存信息

    using System; using System.Collections.Generic; using System.Text; using System.Xml; using System.IO ...

  4. Spring之Spring MVC

    Spring调配半天没搞定,原来是web.xml应该放在WEB-INF的目录下,而不是webcontent目录下: java.lang.ClassNotFoundException: org.spri ...

  5. Quartz1.8.5例子(十)

    /* * Copyright 2005 - 2009 Terracotta, Inc. * * Licensed under the Apache License, Version 2.0 (the ...

  6. asp.net与MVC4的路由原理和过程【学习笔记】

    当IIS收到一个http请求,把请求信息发给对应的HttpModel(实际是实现类UrlRoutingModule),在HttpModel中会注册HttpApplication 类中的PostReso ...

  7. 寡人写的第一个HTML5页面

    好吧,其实是抄来的 <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"/> &l ...

  8. bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树

    2482: [Spoj1557] Can you answer these queries II Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 145 ...

  9. iOS各种系统通知Name

    当了一次搬运工,原地址:http://blog.csdn.net/db905517804/article/details/50569949 用法: - (void) viewWillAppear:(B ...

  10. 【HDU 4352】 XHXJ's LIS (数位DP+状态压缩+LIS)

    XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...