1、String类型
(1)由一个或多个字符组成,一般以“”为界;System.String类中定义了很多对字符串的操作:比较、连接、查找子串。
(2)string.Format实现字符串的格式化;
例:输入两个整数,输出他们的实数除商,并输出上的第二位小数
static void Main()
{
int i = Convert.ToInt32(Console.ReadLine());
int j = Convert.ToInt32(Console.ReadLine());
double shang = (double)i / j;
string str = string.Format("{0:F3}", shang);------------------------格式化
char c = str[str.Length - 2];
Console.WriteLine(c);
2、日期DateTime类型
(1)表示时间上的一刻,通常以日期和当天的时间表示。
例如:
Console.WriteLine(DateTime.Now);
DateTime date=new DateTime(2006,2,28,12,12,12);
Console.WriteLine(date);
Console.WriteLine(date.DayOfWeek);
Console.WriteLine(date.DayOfYear);
TimeSpan duration = new System.TimeSpan(36, 0, 0, 0);
DateTime jinian = date.Add(duration);
Console.WriteLine(jinian);
(2)DateTime的格式化输出
参数format格式详细用法
格式字符 关联属性/说明
d ShortDatePattern
D LongDatePattern
f 完整日期和时间(长日期和短时间)
F FullDateTimePattern(长日期和长时间)
g 常规(短日期和短时间)
G 常规(短日期和长时间)
m、M MonthDayPattern
r、R RFC1123Pattern
s 使用当地时间的 SortableDateTimePattern(基于 ISO 8601)
t ShortTimePattern
T LongTimePattern
u UniversalSortableDateTimePattern 用于显示通用时间的格式
U 使用通用时间的完整日期和时间(长日期和长时间)
y、Y YearMonthPattern
(3)使用用DateTime计算特定日期
DateTime.Now.ToString();//今天
//7天后
DateTime.Now.AddDays(7).ToShortDateString();
//本月范围(本月第一天和最后一天)
DateTime.Now.ToString("yyyy-MM-01");
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).AddDays(-1).ToShortDateString();
//上个月,减去一个月份(得到上个月第一天和最后一天)
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(-1).ToShortDateString();
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();