如何将 DateTime 转换为/从特定字符串格式(两种方式,例如给定格式为“yyyyMMdd")? [英] How to convert DateTime to/from specific string format (both ways, e.g. given Format is "yyyyMMdd")?

查看:21
本文介绍了如何将 DateTime 转换为/从特定字符串格式(两种方式,例如给定格式为“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 转换为/从特定字符串格式(两种方式,例如给定格式为“yyyyMMdd")?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆