C# 如何将任何日期格式转换为 yyyy-MM-dd
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12257483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert any date format to yyyy-MM-dd
提问by GaneshT
My app parses a string data, extracts the date and identify the format of the date and convert it to yyyy-MM-dd.
我的应用程序解析字符串数据,提取日期并识别日期格式并将其转换为 yyyy-MM-dd。
The source date could be anything lime dd-mm-yyyy, dd/mm/yyyy, mm-dd-yyyy, mm/dd/yyyy or even yyyy-MM-dd.
源日期可以是任何石灰 dd-mm-yyyy、dd/mm/yyyy、mm-dd-yyyy、mm/dd/yyyy 甚至 yyyy-MM-dd。
Other than attempting different permutations and combinations using switch case, is there any other efficient way to do it?
除了使用 switch case 尝试不同的排列和组合之外,还有其他有效的方法吗?
string sourceDate = "31-08-2012";
String.Format("{0:yyyy-MM-dd}", sourceDate);
The above code simply returns the same sourceDate "31-08-2012".
上面的代码只是返回相同的 sourceDate "31-08-2012"。
采纳答案by Krishna Thota
string DateString = "11/12/2009";
IFormatProvider culture = new CultureInfo("en-US", true);
DateTime dateVal = DateTime.ParseExact(DateString, "yyyy-MM-dd", culture);
These Links might also Help you
这些链接也可能对您有所帮助
回答by Habib
Convert your string to DateTimeand then use DateTime.ToString("yyyy-MM-dd");
将您的字符串转换为DateTime然后使用DateTime.ToString("yyyy-MM-dd");
DateTime temp = DateTime.ParseExact(sourceDate, "dd-MM-yyyy", CultureInfo.InvariantCulture);
string str = temp.ToString("yyyy-MM-dd");
回答by rezna
string sourceDateText = "31-08-2012";
DateTime sourceDate = DateTime.Parse(sourceDateText, "dd-MM-yyyy")
string formatted = sourceDate.ToString("yyyy-MM-dd");
回答by Haris
You will need to parse the input to a DateTime object and then convert it to any text format you want.
您需要将输入解析为 DateTime 对象,然后将其转换为您想要的任何文本格式。
If you are not sure what format you will get, you can restrict the user to a fixed format by using validation or datetimePicker, or some other component.
如果您不确定将获得什么格式,您可以使用验证或 datetimePicker 或其他一些组件将用户限制为固定格式。
回答by jfs
This is your primary problem:
这是您的主要问题:
The source date could be anything like dd-mm-yyyy, dd/mm/yyyy, mm-dd-yyyy, mm/dd/yyyy or even yyyy-MM-dd.
源日期可以是 dd-mm-yyyy、dd/mm/yyyy、mm-dd-yyyy、mm/dd/yyyy 甚至 yyyy-MM-dd。
If you're given 01/02/2013, is it Jan 2 or Feb 1? You should solve this problem first and parsing the input will be much easier.
如果给你01/02/2013,是 1 月 2 日还是 2 月 1 日?你应该先解决这个问题,解析输入会容易得多。
I suggest you take a step back and explore what you are trying to solve in more detail.
我建议你退后一步,更详细地探索你想要解决的问题。
回答by Amit Singh
You can change your Date Format From dd/MM/yyyyto yyyy-MM-ddin following way:
您可以dd/MM/yyyy通过yyyy-MM-dd以下方式将日期格式从更改为:
string date = DateTime.ParseExact(SourceDate, "dd/MM/yyyy", CultureInfo.InvariantCulture).ToString("yyyy-MM-dd");
Here, SourceDateis variable in which you will get selected date.
在这里,SourceDate是您将获得选定日期的变量。
回答by Ravi Sharma
Try this code:
试试这个代码:
lblUDate.Text = DateTime.Parse(ds.Tables[0].Rows[0]["AppMstRealPaidTime"].ToString()).ToString("yyyy-MM-dd");

