vb.net 将字符串转换为日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8634568/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:49:04  来源:igfitidea点击:

Convert a string to a datetime

vb.netdateasp.net-3.5

提问by Ishan

I am developing asp.netsite using vbframework 3.5.

我正在asp.net使用vb框架 3.5开发网站。

Im having difficulties converting string data into Date I tried using cdate function,

我在将字符串数据转换为日期时遇到了困难,我尝试使用 cdate 函数,

I have a variable sdate which is a string variable and date is stored in it which comes from textbox as dd/mm/yyyy now i want to convert this string into a Date variable as i need to perform the operations as Add a day or Subtract a day.

我有一个变量 sdate,它是一个字符串变量,日期存储在其中,它来自文本框作为 dd/mm/yyyy 现在我想将此字符串转换为日期变量,因为我需要执行添加一天或减去的操作一天。

Please guide me how to go about this. i get the error on 3rd line as,String was not recognized as a valid DateTime. I have tried to do as follows but the error comes

请指导我如何解决这个问题。我在第 3 行收到错误,为String was not recognized as a valid DateTime。我试图做如下,但错误来了

Dim sdate As String 
Dim expenddt As Date
expenddt = Date.Parse(edate)
expenddt = expenddt.AddDays(-1)

But i get the error as

但我得到的错误是

Conversion from String to type Date is not valid.

从字符串到日期类型的转换无效。

How can I get a Datefrom the string?

如何Date从字符串中获取 a ?

回答by adatapost

You should have to use Date.ParseExactor Date.TryParseExactwith correct format string.

您应该必须使用Date.ParseExactDate.TryParseExact使用正确的格式字符串。

 Dim edate = "10/12/2009"
 Dim expenddt As Date = Date.ParseExact(edate, "dd/MM/yyyy", 
            System.Globalization.DateTimeFormatInfo.InvariantInfo)

OR

或者

 Dim format() = {"dd/MM/yyyy", "d/M/yyyy", "dd-MM-yyyy"}
 Dim expenddt As Date = Date.ParseExact(edate, format,  
     System.Globalization.DateTimeFormatInfo.InvariantInfo, 
     Globalization.DateTimeStyles.None)

OR

或者

Dim format() = {"dd/MM/yyyy", "d/M/yyyy", "dd-MM-yyyy"}
Dim expenddt As Date
Date.TryParseExact(edate, format, 
    System.Globalization.DateTimeFormatInfo.InvariantInfo, 
    Globalization.DateTimeStyles.None, expenddt)

回答by Inc33

Nobody mentioned this, but in some cases the other method fails to recognize the datetime...

没有人提到这一点,但在某些情况下,另一种方法无法识别日期时间......

You can try this instead, which will convert the specified string representation of a date and time to an equivalent date and time value

你可以试试这个,它会将日期和时间的指定字符串表示形式转换为等效的日期和时间值

string iDate = "05/05/2005";
DateTime oDate = Convert.ToDateTime(iDate);
MessageBox.Show(oDate.Day + " " + oDate.Month + "  " + oDate.Year );

回答by Doug Null

Try to see if the following code helps you:

试试看下面的代码是否对你有帮助:

Dim iDate As String = "05/05/2005"
Dim oDate As DateTime = Convert.ToDateTime(iDate)

回答by Dmitry Bastron

Try to use DateTime.ParseExact method, in which you can specify both of datetime mask and original parsed string. You can read about it here: MSDN: DateTime.ParseExact

尝试使用 DateTime.ParseExact 方法,您可以在其中指定日期时间掩码和原始解析字符串。你可以在这里阅读它:MSDN:DateTime.ParseExact

回答by AlphaMale

Try converting date like this:

尝试像这样转换日期:

    Dim expenddt as Date = Date.ParseExact(edate, "dd/mm/yyyy", 
System.Globalization.DateTimeFormatInfo.InvariantInfo);

Hope this helps.

希望这可以帮助。