如何将DateTime转换为特定字符串格式或从特定字符串格式转换为DateTime(两种方式,例如,给定的Format为“ yyyyMMdd”)? [英] How to convert DateTime to/from specific string format (both ways, e.g. given Format is "yyyyMMdd")?
本文介绍了如何将DateTime转换为特定字符串格式或从特定字符串格式转换为DateTime(两种方式,例如,给定的Format为“ yyyyMMdd”)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在转换字符串格式的日期时间时遇到问题,但无法使用 yyyyMMdd
格式进行转换。
I am having a problem converting a datetime which is in string format but I am not able to convert it using "yyyyMMdd"
format.
我的代码是:
string tpoc = refSubClaim.BenefitsFolder.BenefitFolderIdNumber.ToString();
string[] tpocinfo = Regex.Split(tpoc,";");
for (int i = 0; i < tpocinfo.Length; i++)
{
switch (i)
{
case 0:
{
string[] tpoc2 = Regex.Split(tpocinfo[0], ",");
claimantAuxillaryRecord.TPOCDate2 = tpoc2[0].ToString();
claimantAuxillaryRecord.TPOCAmount2 = Convert.ToDecimal(tpoc2[1]);
claimantAuxillaryRecord.FundingDelayedBeyondTPOCStartDate2 = tpoc2[2].ToString();
}
break;
推荐答案
如果日期中的日期格式为 ddMMyyyy并想将其转换为 yyyyMMdd,您可以这样操作:
if you have a date in a string with the format "ddMMyyyy" and want to convert it to "yyyyMMdd" you could do like this:
DateTime dt = DateTime.ParseExact(dateString, "ddMMyyyy",
CultureInfo.InvariantCulture);
dt.ToString("yyyyMMdd");
这篇关于如何将DateTime转换为特定字符串格式或从特定字符串格式转换为DateTime(两种方式,例如,给定的Format为“ yyyyMMdd”)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文