DateTime input = DateTime.Now; //06/02/2011 04:05:07 PM
expectedOutput string = "20110602040507";
我想按照这个(年、月、日、小时、分钟、秒(模式获得基于日期的以上输出字符串。
您需要格式字符串"yyyyMMddHHmmss",它将以24小时格式给出小时数。
DateTime.Now.ToString("yyyyMMddHHmmss");
使用DateTime.ToString方法:
String format = "yyyyMMddHHmmss";
String formattedValue = input.ToString(format);