如何将字符串转换为日期格式? [英] how to convert string into date format?

查看:215
本文介绍了如何将字符串转换为日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的先生,

如何将字符串转换为日期格式?

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 use CDate(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 with Convert.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 if string selected_date="3/30/2014"; (i.e. in numeric US format). Where I currently am, that would result in a FormatException

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 use DateTime.Parse. I actually prefer this to using Convert but that is mainly a subjective view. You can use CultureInfo with Parse too.

DateTime d2 = DateTime.Parse(selected_date);
DateTime d3 = DateTime.Parse(selected_date, CultureToUse);



Finally, my preferred method is to use DateTime.TryParse. Why preferred? Because it won't throw an exception, it returns true if it was able to convert the string to a date, and false 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"]
Added pre tags.
[/Edit]


这篇关于如何将字符串转换为日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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