Convert string format to date format - vb.net

How can I convert this string format
03.07.2019
to the following Date Format?
2019-07-03
This is how the String format I get from Soap Services looks like
Public Class ContractBill
Public StartDate As Date
End Class
Dim ctrBill As New ContractBill With {
.StartDate = Convert.ToDateTime(BillingData.START_DATE.ToString("yyyy-MM-dd"))
}
this is my attempt to convert the code to the desired format, but with this .StartDate I get the error:
System.InvalidCastException: "The object of type" System.String "cannot be converted to type" System.IFormatProvider "."
How can I get this Date format yyyy-MM-dd?

There is no format for date with ., so you have to first replace the . with - or / so you can parse your date into available formats.
string date = "03.07.2019";
DateTime value = Convert.ToDateTime(date.Replace(".", "-"));
VB.net
Dim date As String = "03.07.2019"
Dim value As DateTime = Convert.ToDateTime(date.Replace(".", "-"))

Use the DateTime.TryParseExact method since you know the format of the incoming date:
Dim enUS As New CultureInfo("en-US")
Dim inbound As String = "03.07.2019"
Dim outbound As DateTime
If DateTime.TryParseExact(inbound, "MM.dd.yyyy", enUS, DateTimeStyles.None, outbound) Then
Console.WriteLine("'{0}' is formated from: {1}", outbound.ToString("yyyy/dd/MM"), inbound)
Else
Console.WriteLine("'{0}' is not in an acceptable format.", inbound)
End If
Fiddle: Live Demo

Related

Difficulty with formatting a string to parse to date in vb.net

dim dt as string= "03/22/20 20:12:27.320"
Dim tempDate As DateTime = DateTime.ParseExact(dt, "MM/dd/yy hh:mm:ss.fff", CultureInfo.GetCultureInfo("en-US").DateTimeFormat)
This gives error sometimes : System.FormatException: 'String was not recognized as a valid DateTime.'
Why? It seems to be formatted properly. VB.net 4.6.1 framework
"hh" is 12-hour format. 20 is obviously not a valid hour in 12-hour format. If you want 24-hour format then use "HH".
Have you tried to use CDate() function?
dim dt as string= "03/22/20 20:12:27.320"
dim tempDate as Datetime = Cdate(dt)
or
dim tempDate as Datetime = Cdate("03/22/20 20:12:27.320")

VB.net yyyymmdd format needs to parse as date [duplicate]

I am developing asp.net site using vb framework 3.5.
Im having difficulties converting string data into Date
I tried using cdate function,
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.
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
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 Date from the string?
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)
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 );
Try to see if the following code helps you:
Dim iDate As String = "05/05/2005"
Dim oDate As DateTime = Convert.ToDateTime(iDate)
Try converting date like this:
Dim expenddt as Date = Date.ParseExact(edate, "dd/mm/yyyy",
System.Globalization.DateTimeFormatInfo.InvariantInfo);
Hope this helps.
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

Date time to string conversion

I have to convert a string to date time...
String data is in this form: 13.6.2013. 8:06:36
The code:
Dim pDate As String
pDate = TextBox16.Text
Dim pro_Date As DateTime = DateTime.ParseExact(pDate, "DD.M.YYYY. hh24:mm:ss", CultureInfo.InvariantCulture)
And the error I get is: string was not recognized as a valid datetime
How to convert this?
The format string is case sensitive, so this does not work: "DD.M.YYYY". Also, 24h clock is uppercase H not "hh24". You need a single H because 8:06:36 has no leading zero on the hour.
Dim pro_Date As DateTime = DateTime.ParseExact(pDate, "dd.M.yyyy. H:mm:ss", CultureInfo.InvariantCulture)
More informations acc. the "H" Custom Format Specifier

Validate the date is in correct format

I have one field which is in text format and is used for keeping release date .I have to check whether the date in field is in dd/mm/yyyy format or not.
Please suggest how to do that if the field is in string format.
I would use DateTime.TryParseExact, it will not throw an exeption but will return a bool that represents a valid conversion.
From above link:
Converts the specified string representation of a date and time to its DateTime equivalent using the specified format, culture-specific format information, and style. The format of the string representation must match the specified format exactly. The method returns a value that indicates whether the conversion succeeded.
Example:
Dim yourdate As String = "31/12/1999"
Dim mynewDate As DateTime
Dim culture As New CultureInfo("") 'Uses invariant culture
If Not Date.TryParseExact(yourdate, "dd/MM/yyyy", culture, DateTimeStyles.None, mynewDate) Then
MsgBox("Oops")
Else
MsgBox(mynewDate.ToString())
End If
DateTime.ParseExact
trycatch the call since it will throw an exception if it not in the specified format.
format = "d"
dateString = "Sun 15 Jun 2008 8:30 AM -06"
Try
result = Date.ParseExact(dateString, format, provider)
Console.WriteLine("{0} converts to {1}.", dateString, result.ToString())
Catch e As FormatException
Console.WriteLine("{0} is not in the correct format.", dateString)
End Try

Convert a string to a datetime

I am developing asp.net site using vb framework 3.5.
Im having difficulties converting string data into Date
I tried using cdate function,
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.
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
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 Date from the string?
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)
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 );
Try to see if the following code helps you:
Dim iDate As String = "05/05/2005"
Dim oDate As DateTime = Convert.ToDateTime(iDate)
Try converting date like this:
Dim expenddt as Date = Date.ParseExact(edate, "dd/mm/yyyy",
System.Globalization.DateTimeFormatInfo.InvariantInfo);
Hope this helps.
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