Convert a string to a datetime

后端 未结 5 909
鱼传尺愫
鱼传尺愫 2020-11-27 18:50

I am developing asp.net site using vb framework 3.5.

Im having difficulties converting string data into Date I tried using

相关标签:
5条回答
  • 2020-11-27 19:25

    Try converting date like this:

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

    Hope this helps.

    0 讨论(0)
  • 2020-11-27 19:44

    Try to see if the following code helps you:

    Dim iDate As String = "05/05/2005"
    Dim oDate As DateTime = Convert.ToDateTime(iDate)
    
    0 讨论(0)
  • 2020-11-27 19:45

    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 );
    
    0 讨论(0)
  • 2020-11-27 19:47

    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

    0 讨论(0)
  • 2020-11-27 19:50

    You should have to use Date.ParseExact or Date.TryParseExact with correct format string.

     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)
    
    0 讨论(0)
提交回复
热议问题