C# 将日期格式从 dd-mmm-yyyy (16-May-2013) 转换为 mm/dd/yyyy (09/12/2013)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16521687/
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
Convert Date format from dd-mmm-yyyy (16-May-2013) to mm/dd/yyyy (09/12/2013)
提问by Chethan
I want to convert date format from dd-mmm-yyyy (16-May-2013) to date format mm/dd/yyyy (09/12/2013).
我想将日期格式从 dd-mmm-yyyy (16-May-2013) 转换为日期格式 mm/dd/yyyy (09/12/2013)。
I am using this code. But still not able to get the correct value. the month value is becoming zero.
我正在使用此代码。但仍然无法获得正确的值。月份值变为零。
string dt = DateTime.Parse(txtVADate.Text.Trim()).ToString("mm/dd/yyyy");
In the above code txtVADate is the TextBox Control Which is giving date format like dd-mmm-yyyy example (16-May-2013).
在上面的代码中 txtVADate 是 TextBox 控件,它提供日期格式,如 dd-mmm-yyyy 示例(2013 年 5 月 16 日)。
Any Answers are appreciable.
任何答案都是可观的。
采纳答案by James
The format specifier for month is MMnot mm, try using MM/dd/yyyy. Also when using a custom format it's best to pass InvariantCultureto avoid any clashes with the current culture your app is running under i.e.
月份的格式说明符MM不是mm,请尝试使用MM/dd/yyyy. 此外,当使用自定义格式时,最好传递InvariantCulture以避免与您的应用程序在 ie 下运行的当前文化发生任何冲突
DateTime.Parse(txtVADate.Text.Trim()).ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
See Custom Date and Time Format Strings.
请参阅自定义日期和时间格式字符串。
回答by Tim Schmelter
The slashes /mean: "replace me with the actual current date-separator of your culture-info".
斜线的/意思是:“用你的文化信息的实际当前日期分隔符替换我”。
To enforce /as separator you can use CultureInfo.InvariantCulture:
要强制/作为分隔符,您可以使用CultureInfo.InvariantCulture:
string dt = DateTime.Parse(txtVADate.Text.Trim())
.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
MSDN:
微软:
/ The date separator defined in the currentSystem.Globalization.DateTimeFormatInfo.DateSeparator property that is used to differentiate years, months, and days.
/当前System.Globalization.DateTimeFormatInfo.DateSeparator 属性中定义的日期分隔符,用于区分年、月和日。
( you also have to use MM instead of mm since lower case is minute whereas uppercase is month )
(您还必须使用 MM 而不是 mm 因为小写是分钟而大写是月)
回答by Kai
You have to use MMinstead of mmand CultureInfo.InvariantCultureas second parameter
您必须使用MM代替mm和CultureInfo.InvariantCulture作为第二个参数
string dt = DateTime.Parse(txtVADate.Text.Trim()).ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
回答by b1n0m
Use capital Mletter.
m - minute
M - month
使用大写M字母。
m - 分钟
M - 月
回答by coderoaq
string dt = datatime.toshortdatestring`
回答by Chandan Kumar
Here is your solution.
这是您的解决方案。
using System.Globalization;
string dt = DateTime.Parse(txtDate.Text.Trim()).ToString("mm/dd/yyyy", CultureInfo.InvariantCulture);
also can be done like this
也可以这样做
public string FormatPostingDate(string txtdate)
{
if (txtdate != null && txtdate != string.Empty)
{
DateTime postingDate = Convert.ToDateTime(txtdate);
return string.Format("{0:mm/dd/yyyy}", postingDate);
}
return string.Empty;
}

