DevExpress DateEdit String was not recognized as a valid DateTime | Visual Studio - vb.net

I've got a DevExpress DateEdit control (named txtDoB) on my form. I've been trying to poplualte it with a Date from the variable m._DateofBirth by doing this:
txtDoB.Text = Date.ParseExact(m._DateOfBirth, "dd/MM/yy", CultureInfo.InvariantCulture)
But I receive the folowing error:
String was not recognized as a valid DateTime.
The variable is already set to Date format and the actual value is 15/02/1998.
I've tried a number of different things, the DateEdit control hasn't been configured any differently than it's default configuration.
Any help would be appreciate as its getting frustrating.

You must use DateEdit.EditValue instead of DateEdit.Text. And is better to break your code to two lines to determine where the error occurred:
parsedDate = Date.ParseExact(m._DateOfBirth, "dd/MM/yyyy", CultureInfo.InvariantCulture)
txtDoB.EditValue = parsedDate

Related

System.FormatException: 'String was not recognized as a valid DateTime.'

I have a problem on VB.Net which a part of my system has a error. It doesn't save the date thus generating this error. The format was identical to the output on the datetime Picker.
ElseIf Date.Parse(expirationDTP.Value.ToString("MM/dd/yyyy")) <= Date.Parse(Date.Now.ToString("MM/dd/yyyy")) Then
MsgBox("Select the expiration date of the stock!")
This is an image of the output.
Date Output
Thanks!
expirationDTP.Value and Date.Now already are of type Date. Why are you converting them into strings only to convert them back to Date objects?
Just compare them directly:
ElseIf expirationDTP.Value <= Date.Now Then
For future reference, the actual error occurs because Date.Parse() tries to use the current system's cultural settings to parse the date. If you need to parse a specific date format you should use Date.ParseExact():
Date.ParseExact("01/20/2018", "MM/dd/yyyy", CultureInfo.InvariantCulture)
EDIT:
Try comparing the Date properties of the two Date objects:
ElseIf expirationDTP.Value.Date <= Date.Now.Date Then
If it still doesn't work you're going to have to check if it's any preceding ElseIf or the initial If that gets executed instead. As you haven't shown any more code than that there's not much more I can do.

converting string date to dd.MM.yyyy format

I am working on vb.net application
am getting date like this :
recevdate = rs("ITIReceiveddate")
my recevdate format is like this : 2/27/2016 month/date/year
i want to convert like this : date.month.year 27.2.2016
so i wrote code like this :
Dim dt as string = DateTime.ParseExact(recevdate, "dd.MM.yyyy", Nothing)
but its getting error ..
What is wrong with my code? how i can rectify this issue?
any help is very appreciable..Thanks
DateTime.ParseExact returns a DateTime, not a string. Your project is setup with the Option Strict set to Off and this enables this kind of automatic conversions. But it is, as usual, a trap waiting to kick on unsuspecting programmers.
To execute correctly you need
Dim recevdate = "2/27/2016"
Dim dt As DateTIme = DateTime.ParseExact(recevdate, "M/d/yyyy", Nothing)
Dim formattedString = dt.ToString("d.M.yyyy")
Console.WriteLine(formattedString)
Notice that you have an error also in your formatted mask for parsing the date. If your date has only one digit for months or one digit for days then you need just one M and one d both on the parsing and in the formatting back to string

Problems converting string to DateTime

I am making a program in which there is a function that check the database for user that haven't been called for 2 weeks or more, and shows them in a ListView.
In this part I am checking how long ago they were called:
Dim r As Int32 = excelWS.UsedRange.Rows.Count
Dim bel As New ArrayList
For nm As Int32 = 1 To r Step 1
If Convert.ToInt32(DateDiff("ww", Date.ParseExact(excelWS.Cells(nm, 1).value(), "yyMMddhhmm", System.Globalization.DateTimeFormatInfo.InvariantInfo), Now, FirstDayOfWeek.Monday, FirstWeekOfYear.Jan1)) >= My.Settings.Tijdverschil Then
bel.Add(nm)
End If
Next
I get the FormatException was unhandled at the if line.
In the error description it says (roughly translated to english):
The tokens aren't recognized at valid DateTime.
--edit--
If anyone thinks the format in excel is wrong, i copied one field over, they are all like this.
1408180736
Without additional information, I wonder if your date from Excel is coming in as an OLE Automation Date. Depending on how you read the data from Excel, it may come back in this format.
If it is, you need to parse it as a double and then as an OLEDate. Something like this:
Dim oleDate as Double
Dim result as DateTime
If Double.TryParse(excelWS.Cells(nm, 1).value(), oleDate) Then
' Handle valid OLE Automation dates...
result = DateTime.FromOADate(oleDate)
End If
The trick for this is include a datetime picker in your application set visibility to false.
Then set the string as value to the datetime picker and make use of the datetime picker to get the difference between dates.

Working with Excel Dates in vb.net

I am experiencing a problem when attempting to read a date from an excel sheet. (The date column is formatted the same as the short date format of the computer its opened on). I populate the dates from the excel sheet into a datagrid with success, but when I attempt to parse the date (To format it appropriately), I get a error saying the string wasn't a valid DateTime value. The computer's short date format is dd/MM/yyyy. This is the code I use to try parsing the date. The following code is an example of where the process fails.
Dim dateParsed AS DateTime = DateTime.Parse("14/01/2013").ToString("yyyy-MM-dd")
Is there some way to programatically get the system's short date format and use ParseExact instead or any suggestions?
You want to parse exact, using the current cultures short-date format?
Dim dt As DateTime = DateTime.ParseExact(str, "d", CultureInfo.CurrentCulture)
Additionally, if you actually need to see the ShortDate format for yourself get it through the CurrentCulture.
CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern
You can try this piece of coding
Dim dt As DateTime = DateTime.Parse("1/14/2013").ToString
Dim f As String = Format(dt, "yyyy-MM-dd")

Date not converting correctly in VB.net

It seems like I keep having problems with dates. I am using the following code:
Dim LocalDateCultureProvider As New CultureInfo(CultureInfo.CurrentCulture.ToString)
Dim CurrentDate As DateTime = Convert.ToDateTime(System.DateTime.Now.ToString("dd/MM/yyyy"), System.Globalization.CultureInfo.InvariantCulture)
ExpiryDate = DateTime.ParseExact(strDate, "dd/MM/yyyy", LocalDateCultureProvider)
If DateTime.Compare(ExpiryDate, CurrentDate) < 0 Then
MsgBox("This file has expired.")
Exit Sub
End If
Here I am reading strDate as a string and for one example, the value of this is "29/09/2012" However, in the ExpiryDate line it converts to #09/29/2012# so that in the comparison with today's date which is stored (correctly in my opinion) in CurrentDate as #10/6/2012# I get the If condition to be true (wrongly).
BTW, I also tried
Dim LocalDateCultureProvider As New CultureInfo(System.Globalization.CultureInfo.InvariantCulture.ToString)
just to see if that was causing the problem. I am trying to build something that will work in all Cultures. No matter what the local settings are, I want to test for expiration by comparing the current system date with an expiration date which I receive as a string. Please tell me how to go about this so I can get consistent results.
TIA,
Chiwda
No, you parse the CurrentDate incorrectly. CultureInfo.InvariantCulture expects the month before the day but you formatted it with the day first. You are writing unnecessary code, simply fix with:
If DateTime.Compare(ExpiryDate, DateTime.Now) < 0 Then