Write to Date Time Picker - vb.net

Today, I want to write day, month, and year to a datetimepicker in Visual Studio.
cmd.Parameters.AddWithValue("#geboortedag", dtp_geboortedatum.Value.Day)
cmd.Parameters.AddWithValue("#geboortemaand", dtp_geboortedatum.Value.Month)
cmd.Parameters.AddWithValue("#geboortejaar", dtp_geboortedatum.Value.Year)
These work. I save day, month and year separately to three rows in my database.
However, when I want to call these values, I can't even run the thing without getting the following:
BC30068 Visual Basic AND VB.NET Expression is a value and therefore cannot be the target of an assignment.
Here's what I tried.
dtp_geboortedatum.Value.Day = row("geboortedag").ToString
dtp_geboortedatum.Value.Month = row("geboortemaand").ToString
dtp_geboortedatum.Value.Year = row("geboortejaar").ToString
All I want is to put the day, month and year I have in separate cells into the date time picker when I open a record.
PS I also tried like the help page for the error says to write to a variable first but that does nothing to help. Perhaps I did it wrong but, I can't get it to work.
Also, I've been linked to this article but this does not fix the issue. I keep getting errors that integers are strings and cannot be converted to integers, but they're integers! They're integers when they start, they're integers when they're saved into a row for integers that saves integers, they're integers when they come out. Why aren't they integers in the end when nothing special happens to them but being inputted, saved, and called?

(Posted on behalf of the question author).
None of the material I provided in the question is relevant. Turns out I had to use dt.clear(). I'm going to be honest, I don't know how this is the thing that went wrong.
I fixed the rest with cdate() and Option Strict On.

its a very long time since touched VB.NET, but I think what you need to do is pass a DateTime type to the date picker to set its value as below;
dtp_geboortedatum.Value = New DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day)
So you will need to pass your year, month and day as integers

Just substitute your row integer values for the literals I used.
Private Sub SetDate()
Dim myDay As Integer = 27
Dim myMonth As Integer = 4
Dim myYear As Integer = 2018
DateTimePicker1.Value = New DateTime(myYear, myMonth, myDay)
End Sub

Related

Remove dot´s from date

I made a program that generates a daily index code.
It gets created from
Employer (everyone has a number from 0-9)
Date of serial code requested
Everything is working fine, but I wannt to remove the dots from the date
I tried things like
date.Text = date.Text.Replace(".""", """")
or
Dim clean as String
clean = myString.Replace(".", "")
But nothing happens
May be I just didnt unterstand the using... If yes, so please help me to find a alternative.
Ok i will try to explain better.
As I lunch it a textbox gets the date of today, the textbox is called date
Ive got a combobox, from there you select the employer. Every employer has a number. For example Andreas is Number 1.
I wannt to do something like:
if combobox1.text = "Andreas" then
dailyCode.text = "1" & date.text
end if
My problem is that the date is written with dots, the daily code should not have dots.
Sorry for my bad English
In your questions Details are still incomplete.still assuming that the date(still i am confused how are you using reserved keyword) is declared as date type itself,consider formatting it with Format function itself.
Try using
Dim s As String = Format(date, "ddMMMyyyy")
hope that helps.

How to find day of the week from known date and month but variable year

My code so far is this:
intYear = CDate(varYear)
intJan = Weekday("1/1/" & intYear)
varYear is a variant that has been tested to be a valid number.
The problem is in line two, it doesn't like me trying to merge the date and month to the variable year, is there another way around this?
Edit:
I have since changed the code to this:
varJan = ("1/1/" & varYear)
intJan = Weekday(CDate(varJan))
It is still having trouble, it's saying there's a type mismatch. I know the issue is steaming again from the second line.
The solution was found by eliminating unnecessary variables and thus eliminating potential conversion problems.
The final code was as follows:
intJan = Weekday(CDate("1/1/" & varYear))

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.

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

Yet another date formatting problem :(

I seem to have a date formatting problem every day!
I am querying a table and am getting a date back in the format dd/mm/yyyy (as a string btw). Brilliant! thats what I want. But, now I want to convert that string to a date so i can do
dim dayNumber as integer = day.DayOfWeek
But when I convert it to a date it changes it to #m/dd/yyyy#. AHHHH! how can I change this?
here is my code i've tried
Dim ActivityDate As String
If dt.Rows(i)("Date") Is DBNull.Value Then
ActivityDate = ""
Else
ActivityDate = dt.Rows(i)("Date")
End If
Dim ci As New System.Globalization.CultureInfo("en-CA")
Dim theDate As Date = Date.Parse(ActivityDate, ci)
Dim day As Integer = theDate.DayOfWeek
Cheers
Brilliant! thats what I want
That's not what you want. It is the worst possible format for a date because it is so horribly ambiguous. Date string formats depend on the current culture. "4/1/2010" is Unicorn day at SO, it is day in January in Europe. "#4/1/2010#" is a legacy VB6 format.
Always store dates in a DateTime in your code. Always store dates in a database column type of datetime in your dbase. There is never any ambiguity and you'll have an easy time with the DateTime members to manipulate dates.
If you convert the string to a date, you can always output it back to the original format using a custom format string: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
The correct solution here (at least until you tell us why this isn't possible) is to update your database to use a datetime column type rather than a varchar. Now we also know that this column has no NULL values, because otherwise you'd be complaining about exceptions on your Date.Parse() call. After applying both those sentences, you can trim all that code down to a simple one-liner:
Dim day As Integer = DirectCast(dt.Rows(i)("Date"), DateTime).DayOfWeek
May I also ask why you're looping through the table row by row? I've worked in a shop where that was the norm, but since I've left there I've run in to alternatives and more and more I'm coming to find looping through a datatable as just wrong. It's an older imperative coding style, and generally you want to go for a declarative coding style.
Are you parsing it like this:
Dim newDate as DateTime = DateTime.Parse(myDate)
If the culture of your system does not use that date format, then you should get that date string as an actual date:
' canadian date format is dd/mm/yyyy
Dim ci As New System.Globalization.CultureInfo("en-CA")
Dim theDate As Date = Date.Parse("13/04/2010", ci)
Make sure you specify an exact parse format like so:
Console.WriteLine(DateTime.ParseExact("17/12/2010", "dd/mm/yyyy", null));
I am not sure what the last parameter is but it is safe to ignore it.
I'm guessing that you are seeing the #m/dd/yyyy# in the debugger, like this screenshot below. Don't worry!
A Date variable isn't stored as a string. The debugger has to convert your Date into a string to display it, and it insists on showing dates in #m/dd/yyyy# format. But that doesn't have any effect on the runtime behaviour of your program.
Screenshot of Visual Studio Debugger http://img707.imageshack.us/img707/6205/debugger.gif