[英]Convert Date format from dd-mmm-yyyy (16-May-2013) to mm/dd/yyyy (09/12/2013)
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.任何答案都是可观的。
The format specifier for month is MM
not mm
, try using MM/dd/yyyy
.月份的格式说明符是MM
而不是mm
,请尝试使用MM/dd/yyyy
。 Also when using a custom format it's best to pass InvariantCulture
to avoid any clashes with the current culture your app is running under ie此外,当使用自定义格式时,最好传递InvariantCulture
以避免与您的应用程序在 ie 下运行的当前文化发生任何冲突
DateTime.Parse(txtVADate.Text.Trim()).ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
Use capital M
letter.使用大写M
字母。
m - minute米 - 分钟
M - month M - 月
您必须使用MM
而不是mm
和CultureInfo.InvariantCulture
作为第二个参数
string dt = DateTime.Parse(txtVADate.Text.Trim()).ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
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);
/ The date separator defined in the current System.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 因为小写是分钟而大写是月)
string dt = datatime.toshortdatestring`
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.