如何从DD / MM / YYYY或MM / DD / YYYY更改日期格式YYYY-MM-DD? [英] How to change date format from DD/MM/YYYY or MM/DD/YYYY to YYYY-MM-DD?

查看:228
本文介绍了如何从DD / MM / YYYY或MM / DD / YYYY更改日期格式YYYY-MM-DD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用C#

来自:2010年6月16日或16/06/2010

from : "06/16/2010"or "16/06/2010"

到:2010-06-16

to : "2010-06-16"

你能帮我实现这个

感谢

推荐答案

如果您已经拥有了它作为一个的DateTime ,使用:

If you already have it as a DateTime, use:

string x = dt.ToString("yyyy-MM-dd");



见的有关详细信息,MSDN文档。您可以指定 CultureInfo.InvariantCulture 来强制使用西方数字等,这是更重要的,如果你使用MMM月的名称的和类似的事情,但它不会是一个坏主意,使之明确的:

See the MSDN documentation for more details. You can specify CultureInfo.InvariantCulture to enforce the use of Western digits etc. This is more important if you're using MMM for the month name and similar things, but it wouldn't be a bad idea to make it explicit:

string x = dt.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);

如果你有一个字符串,开始时,你需要分析它,然后重新格式化..当然,这意味着你需要知道原始字符串的格式。

If you have a string to start with, you'll need to parse it and then reformat... of course, that means you need to know the format of the original string.

这篇关于如何从DD / MM / YYYY或MM / DD / YYYY更改日期格式YYYY-MM-DD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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