Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将字符串格式化为字符串日期格式.net 2.0_C#_.net - Fatal编程技术网

C# 将字符串格式化为字符串日期格式.net 2.0

C# 将字符串格式化为字符串日期格式.net 2.0,c#,.net,C#,.net,给定一个字符串值,例如“03252013”,我需要将该字符串转换为“DDMMYYYY”格式。 你用什么功能?结果应该是“25032013” 谢谢使用DateTime.ParseExact方法。然后使用ToString转换为适当的格式 var dt = DateTime.ParseExact("03252013", "MMddyyyy", CultureInfo.InvariantCulture); var result = dt.ToString("ddMMyyyy"); //25032013

给定一个字符串值,例如“03252013”,我需要将该字符串转换为“DDMMYYYY”格式。 你用什么功能?结果应该是“25032013”


谢谢

使用
DateTime.ParseExact
方法。然后使用
ToString
转换为适当的格式

var dt = DateTime.ParseExact("03252013", "MMddyyyy", CultureInfo.InvariantCulture);
var result = dt.ToString("ddMMyyyy"); //25032013

使用
DateTime.ParseExact
方法。然后使用
ToString
转换为适当的格式

var dt = DateTime.ParseExact("03252013", "MMddyyyy", CultureInfo.InvariantCulture);
var result = dt.ToString("ddMMyyyy"); //25032013

只需解析然后重新格式化:

string input = "03252013"
DateTime date = DateTime.ParseExact(input, "MMddyyyy", CultureInfo.InvariantCulture);
string resultDate = date.ToString("ddMMyyyy");

只需解析然后重新格式化:

string input = "03252013"
DateTime date = DateTime.ParseExact(input, "MMddyyyy", CultureInfo.InvariantCulture);
string resultDate = date.ToString("ddMMyyyy");

如果你的需求真的那么简单,我会考虑把价值观转换为……/P>
string input = "03252013";
string output = input.Substring(2, 2) + input.Substring(0, 2) + input.Substring(4, 4);
…不要忘记在使用子字符串之前验证输入(检查长度为8可能就足够了)


注意:如果您的输入或输出格式可能会发生更改,那么按照许多其他人的建议使用DateTime.Parse技术将更为理想。但如果这真的是唯一的情况,那么这种方法应该提供更好的性能。。。稍微;)
string input = "03252013";
string output = input.Substring(2, 2) + input.Substring(0, 2) + input.Substring(4, 4);
…不要忘记在使用子字符串之前验证输入(检查长度为8可能就足够了)


注意:如果您的输入或输出格式可能会发生更改,那么按照许多其他人的建议使用DateTime.Parse技术将更为理想。但如果这真的是唯一的情况,那么这种方法应该提供更好的性能。。。稍微;)

您可以使用以下代码部分的帮助进行转换

string resultDate = DateTime.ParseExact(myTestDate, "MMDDYYYY", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None).ToString("DDMMYYYY");

您可以使用以下代码部分的帮助进行转换

string resultDate = DateTime.ParseExact(myTestDate, "MMDDYYYY", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None).ToString("DDMMYYYY");
使用以下命令:
resultDate=DateTime.Parse(myTestDate,“d”).toString()
“d”是一种格式提供程序,它在如下日期向Farm说明:DD/MM/YYYY

使用此格式:
resultDate=DateTime.Parse(myTestDate,“d”).toString()
“d”是一个格式提供程序,它在以下日期向Farm显示:DD/MM/YYYY