如何将日期转换为mm / dd / yyyy格式
问题描述:
我想将日期格式转换为mm / dd / yyyy。无论日期格式是在文本框中,我想将其转换为mm / dd / yyyy。
I want to convert dateformat to mm/dd/yyyy. Whatever dateformat is coming in textbox, I want to convert it into mm/dd/yyyy.
答
首先,转换为datetime对象。最常见的标准通过以下工作:
First you need to get it into a datetime object. The most common standards work via:
DateTime x = DateTime.Parse(txtDate.Text);
如果你想要一个怪异的格式,你还需要知道它是什么格式:
If you expect a freaky format, you still have to know what format it is:
DateTime x;
DateTime.TryParseExact(txtDate.Text, "YYddd", out x);
然后只需输出数据:
string date = x.ToString("MM/dd/yyyy");
但是你真的需要使用正则表达式,验证器,scout的荣誉来执行你的格式化 / em>。
But you really need to enforce your formatting using regex, validators, scout's honor - something.