如何将日期转换为mm / dd / yyyy格式 [英] How to convert date to mm/dd/yyyy format

查看:303
本文介绍了如何将日期转换为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.

这篇关于如何将日期转换为mm / dd / yyyy格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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