我的代码解析原始数据并将其转换为如下所示的日期时间字符串格式
string birthday = "20" + year + "-" + month + "-" + day + " " + hour + ":" + minutes + ":" + second;
所以生日是一个字符串然后我将它转换为日期时间格式,如下面
DateTime bdaylater= Convert.ToDateTime(birthday);
结果如下图
7/8/2015 5:02:05 AM
,我想把它转换成下面的
2015/07/08 05:02:05.000
我怎么能得到这样的结果呢?
谢谢。
您应该在打印相同的日期变量之前格式化您的日期变量
string birthday = "2015" + "-" + "07" + "-" + "08" + " " + "05" + ":" + "02" + ":" + "05";
DateTime bdaylater= Convert.ToDateTime(birthday);
Console.WriteLine(bdaylater.ToString("yyyy/MM/dd hh:mm:ss.mmm"));
这将得到0/p 2015/07/08 05:02:05.02
小提琴例子
如果你只想要000毫秒,字符串将是"yyyy/MM/dd hh:mm:ss.000"
要将其转换为字符串格式吗?上面有3个0吗?也许这能帮上忙:
int year = 15;
int month = 7;
int day = 8;
int hour = 5;
int minutes = 2;
int second = 5;
string birthday = "20" + year + "-" + month + "-" + day + " " + hour + ":" + minutes + ":" + second;
DateTime bdaylater = Convert.ToDateTime(birthday);
Console.WriteLine(bdaylater);
Console.WriteLine(bdaylater.ToString("yyyy/MM/dd hh:mm:ss.000")); //2015/07/08 05:02:05.000
Console.ReadLine();
试试这个:
DateTime.Parse(birthday).ToString("yyyy/MM/dd hh:mm:ss.fff")