将YYYYMMDD字符串转换为MM / DD / YYYY字符串 [英] Convert YYYYMMDD string to MM/DD/YYYY string

查看:430
本文介绍了将YYYYMMDD字符串转换为MM / DD / YYYY字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期以YYYYDDMM格式存储为字符串。我想以 MM / DD / YYYY格式显示该值。我在用C#编程。我正在使用的当前代码如下:

I have a date that is stored as a string in the format YYYYDDMM. I would like to display that value in a 'MM/DD/YYYY' format. I am programming in c#. The current code that I am using is as follows:

txtOC31.Text = dr["OC31"].ToString().Trim();
strOC31date = dr["OC31DATE"].ToString().Trim();
DateTime date31 = DateTime.Parse(strOC31date);
strOC31date = String.Format("{0:MM/dd/yyyy}", date31);

但是,我收到一个错误消息,因为YYYYMMDD字符串(strOC31date)没有被识别为有效字符串约会时间。

However, I am getting an error because the YYYYMMDD string (strOC31date) is not being recognized as a valid datetime.

推荐答案

DateTime.ParseExact带有示例

DateTime.ParseExact with an example

string res = "20120708";
DateTime d = DateTime.ParseExact(res, "yyyyddMM", CultureInfo.InvariantCulture);
Console.WriteLine(d.ToString("MM/dd/yyyy"));

这篇关于将YYYYMMDD字符串转换为MM / DD / YYYY字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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