Date format in Windows CE - vb.net

I'm trying to convert the date string to "dd/MM/yyyy" format using the code below
m_dEDate = Format(CDate(strExpiry), "dd/MM/yyyy")
But system got the exception error - "parameter is incorrect". If regional short date is "dd-MMM-yy", there is no exception error.
Without setting regional manually, how could we manage to convert by coding?
Should we use System.Globalization? If so, please share me a sample.

use Date.ParseExact
Dim dateString As String = "06/15/2008"
Dim format As String = "MM/dd/yyyy"
Dim provider As CultureInfo = CultureInfo.InvariantCulture
Dim _NewDate As Date = Date.ParseExact(dateString, format, provider)

It looks like you're trying to change the format of a date representation, but you haven't told us what format you're converting from. John's code shows correctly how to parse a value - but it looks like that's the target value you want. Suppose your input is something like "2012-11-15", i.e. yyyy-MM-dd. Then you'd want:
Dim inputText = "2012-11-15"
Dim inputFormat = "yyyy-MM-dd"
Dim outputFormat = "MM/dd/yyyy"
Dim provider = CultureInfo.InvariantCulture
Dim parsedDate = Date.ParseExact(inputText, inputFormat, provider)
Dim outputDate = parsedDate.ToString(outputFormat, provider)
Note that the use of the invariant culture here isolates you completely from culture sensitivity. Is that actually what you want, or do you perhaps want to treat your input according to the user culture, but not the output?

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")

Convert string format to date format

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

VB Net String to DateTime

from string dataIni = "2015/12/21 18:28", I would like to parse it into same format DateTime. I am using following code,
Dim dataIni = "2015/12/21 18:28"
Dim dataIniParsed As DateTime = DateTime.ParseExact(dataIni, "yyyy/MM/dd HH:mm", System.Globalization.CultureInfo.InvariantCulture)
However, I am getting "21/12/2015 6:28:00 PM". How to get desired format?
Once you have parsed the text into a DateTime you no longer have a "format" as a DateTime is a data type that just represents a time and a date without any particular format. It's not until you do a .ToString() or Console.WriteLine(...), for example, that the DateTime is converted to a string.
Now, by default, .ToString() produces a string format based on your system settings. Yours is clearly producing "21/12/2015 6:28:00 PM". On my system I use an ISO date format, so my .ToString() produces "2015/12/21 18:28:00" for your date.
You can produce custom formats easily though. Try this code:
Dim dataIniFormatted As String = dataIniParsed.ToString("yyyy/MM/dd HH:mm")
This produces the string "2015/12/21 18:28". Note that this is no longer a DateTime - it's a string - and also that dataIniParsed is still a DateTime (with no inherent formatting).
You can use the code below to format your Date variable. You have to format the date to get the desired result.
Dim dataIni As String = "2015/12/21 18:28"
Dim dataIniParsed As DateTime
dataIniParsed = Convert.ToDateTime(dataIni)
MsgBox(dataIniParsed & " - " & dataIniParsed.ToString("yyyy/MM/dd HH:mm") & " - " & dataIniParsed.GetType.ToString)

Error converting string to Custom Date Format

In my application I got error when trying to convert a Date from string date format as shown below:
dateFormat = Format(CDate("2014-mar-06"), "MM/dd/yyyy")
Error
Conversion from string "2014-mar-06" to type 'Date' is not valid
This problem only comes when my Region and Language setting is Spanish(Mexico) (or any spanish but not for others) in Windows 7 . What is the problem and how to solve this?
Avoid VB6 functions like CType and use .NET methods like TryParse instead.
Also CultureInfo.InvariantCulture gets the CultureInfo object that is culture-independent (invariant)
Try this
Dim dateString = "2014-mar-06"
Dim dateValue As DateTime
If DateTime.TryParseExact(dateString, _
"yyyy-MMM-dd", CultureInfo.InvariantCulture, _
DateTimeStyles.None, dateValue) Then
Dim myDate = dateValue.ToString("MM/dd/yyyy") 'Your Date is stored in myDate
Else
'Unable to parse your dateString
End If

Textbox date to database

I have the following code which worked fine on a .net 1.1 website:
objSQLCommand.Parameters.Add(New SqlParameter("#date", SqlDbType.DateTime, 8))
objSQLCommand.Parameters("#date").Value = txtDate.Text
This doesn't work on a .net 3.5 server.
I'm getting a message saying that it can't convert a string to datetime.
Try this one,
objSQLCommand.Parameters("#date").Value = Convert.ToDate(txtDate.Text)
follow-up question, what is the format of the date in your textbox? Maybe you can take advantage of TryParse or ParseExact also.
Dim provider As CultureInfo = CultureInfo.InvariantCulture
Dim dateString as String = txtDate.Text '08/10/2012
Dim format As String = "d"
objSQLCommand.Parameters("#date").Value = Date.ParseExact(dateString, format, provider)