如何将字符串转换为日期格式? [英] how to convert string into date format?
问题描述
亲爱的先生,
如何将字符串转换为日期格式?
string selected_date =2014年3月23日;
如何将此selected_date转换为日期格式...
plz help ...
1。首先,使用tryparse检查日期字符串是否有效;
2.指定所需的日期格式;
3.然后进行转换。
使用系统;
public class 计划
{
public static void Main()
{
string selected_date = 3月23日-2014\" 跨度>;
Console.WriteLine( 日期字符串为{0}, selected_date);
DateTime dt;
if (DateTime.TryParse(selected_date, out dt))
{
string format = dd -MMMM-YYYY跨度>;
Console.WriteLine(dt.ToString(format));
}
else
{
Console.WriteLine( 不是有效的日期时间);
}
}
}
进一步解决方案1.
您在问题中标记了C#,因此您无法使用CDate(selected_date)
- 这是一个VB函数(是在C#中使用VB.NET函数的方法,但我不会说如何)
有几种方法可以将字符串转换为日期,有些方法更好在某些情况下比其他人好。
让我们从开始转换.ToDateTime
DateTime d1 = Convert.ToDateTime(selected_date);
将使用您示例中的文本(这是传递日期的更好方法之一)
但是如果string selected_date =3/30/2014;
(即采用数字US格式)。我目前在哪里,这将导致FormatException
Quote:字符串无法识别作为一个有效的DateTime。
所以我要确保Visual Studio知道我今天想使用美国日期格式,使用 CultureInfo [ ^ ]
IFormatProvider CultureToUse = new System.Globalization.CultureInfo(en-US,真正);
DateTime d1 = Convert.ToDateTime(d,CultureToUse);
将字符串转换为日期的另一种方法是使用DateTime .Parse
。我实际上更喜欢使用转换,但这主要是一种主观观点。您也可以将CultureInfo与Parse
一起使用。
DateTime d2 = DateTime .Parse(selected_date);
DateTime d3 = DateTime .Parse(selected_date,CultureToUse);
最后,我首选的方法是使用DateTime。 TryParse
。为什么喜欢?因为它不会抛出异常,所以如果它能够将字符串转换为日期,则返回true
,并且false
如果不能。是的,您也可以使用CultureInfo。
DateTime d4,d5;
Boolean b = DateTime.TryParse(selected_date, out d4);
Boolean b1 = DateTime.TryParse(selected_date,CultureToUse,System.Globalization.DateTimeStyles.None, out d5);
有关TryParse的更多信息 [ ^ ]可在此链接中找到。
string myDate = 21-07-2006;
DateTime myDateTime = DateTime.Parse(myDate);
myDate = DateTime.ParseExact( value , < span class =code-string> MM / yy,System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None);
已添加预
标签。
[/编辑]
dear sir,
how to convert string into date format?
string selected_date="23-March-2014";
how to convert this selected_date into date format...
plz help...
1. First, check that the date string is a valid one using tryparse;
2. Specify the desired date format;
3. then do the conversion.
using System; public class Program { public static void Main() { string selected_date="23-March-2014"; Console.WriteLine("The date string is {0}", selected_date); DateTime dt; if (DateTime.TryParse(selected_date, out dt)) { string format = "dd-MMMM-yyyy"; Console.WriteLine(dt.ToString(format)); } else { Console.WriteLine("Not a valid datetime"); } } }
Further to solution 1.
You've tagged C# in your question so you can't useCDate(selected_date)
- that is a VB function (there are ways of using VB.NET functions in C# but I'm not going to say how)
There are a few ways of converting strings to dates and some are better than others in certain circumstances.
Let's start withConvert.ToDateTime
DateTime d1 = Convert.ToDateTime(selected_date);will work with the text you have in your example (which is one of the better ways of passing dates around)
But what ifstring selected_date="3/30/2014";
(i.e. in numeric US format). Where I currently am, that would result in aFormatException
Quote:String was not recognized as a valid DateTime.
So I'm going to make sure that Visual Studio knows that I want to use American date formats today by using a CultureInfo[^]
IFormatProvider CultureToUse = new System.Globalization.CultureInfo("en-US", true); DateTime d1 = Convert.ToDateTime(d, CultureToUse);
Another way of converting a string to a date is to useDateTime.Parse
. I actually prefer this to using Convert but that is mainly a subjective view. You can use CultureInfo withParse
too.
DateTime d2 = DateTime.Parse(selected_date); DateTime d3 = DateTime.Parse(selected_date, CultureToUse);
Finally, my preferred method is to useDateTime.TryParse
. Why preferred? Because it won't throw an exception, it returnstrue
if it was able to convert the string to a date, andfalse
if it couldn't. And yes, you can use CultureInfo with this too.
DateTime d4, d5; Boolean b = DateTime.TryParse(selected_date, out d4); Boolean b1 = DateTime.TryParse(selected_date, CultureToUse, System.Globalization.DateTimeStyles.None, out d5);
More info on TryParse[^] can be found on this link.
string myDate = "21-07-2006"; DateTime myDateTime = DateTime.Parse(myDate); myDate = DateTime.ParseExact(value, "MM/yy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
[Edit member="Tadit"]
Addedpre
tags.
[/Edit]
这篇关于如何将字符串转换为日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!