string字符串转换时间格式(包含精确时分秒) | 您所在的位置:网站首页 › 分秒换算题目怎么写 › string字符串转换时间格式(包含精确时分秒) |
1.精确到日期格式(yyyy-MM-dd): string dateString = "20110526"; DateTime dt = DateTime.ParseExact(dateString, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture); Console.WriteLine(dt.ToString("yyyy-MM-dd"));
2.精确到时分秒格式(yyyy-MM-dd HH:mm:ss): string s = "20120304120312"; DateTime dt = DateTime.ParseExact(s, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture); Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss")); 或者这么写 static string s = "20120304120312"; private static int F(int i, int l) { return Convert.ToInt32(s.Substring(i, l)); } private void button1_Click(object sender, EventArgs e) { DateTime dt = new DateTime(F(0, 4), F(4, 2), F(6, 2), F(8, 2), F(10, 2), F(12, 2)); textBox1.Text = dt.ToString(); }
|
CopyRight 2018-2019 实验室设备网 版权所有 |