convert Integers to Date Objects VB - vb.net

I have 2 integers and 1 String. Now I want to convert it in date objects and display it in datepicker1 (I set its format to "short")
for example:
Dim Day As Integer = 14
Dim Month As String = "July"
Dim Year As Integer = 2016
I am expecting an answer as
7/14/2016 'this must be displayed in datepicker1
I searched through the internet and i found the same question here but this is from different language and i want it in VB. if my question has already been asked before. please post the link.
I am new to vb so i am hoping for your help guys!

Here's a way that uses Date.ParseExact:
Dim dt = Date.ParseExact($"{Year}-{month}-{day}", "yyyy-MMMM-dd", DateTimeFormatInfo.InvariantInfo)
Note that i'm using string interpolation at $"..." which is available in VS 2015/VB14. Otherwise you could use String.Format:
Dim dtStr = String.Format("{0}-{1}-{2}", Year, month, day)
Dim dt = Date.ParseExact(dtStr, "yyyy-MMMM-dd", DateTimeFormatInfo.InvariantInfo)
Here you'll find more informations about the custom date and time format strings.

Related

How do I set a range for a combo box showing a span of years

I'm fairly new to coding in VB and after much experimenting I can't find a way to do this. I'm trying to have a combo box display a range of years starting at one specified in a date variable and running to the current date, with the current date one being displayed as the default. Here is my latest version of the code trying to set up the datasource.
Dim yearDataSource = Enumerable.
Range(myEarliestDate.Year, myEarliestDate.Year - DateTime.Now.Year + 1).
OrderByDescending(Function(y) y).
ToList()
I'd appreciate any help you could give. Please keep it simple.
This function will work
Private Function GetYears(startDate As Date, endDate As Date) As List(Of Integer)
Dim startYear = startDate.Year
Dim endYear = endDate.Year
Dim years = (From year In Enumerable.Range(startYear, endYear - startYear)
Order By year Descending)
Return years.ToList()
End Function
Example call:
Dim startDate = Date.Now.AddYears(-10)
Dim endDate = Date.Now.AddYears(1)
cmbYears.DataSource = GetYears(startDate, endDate)

Get the 'YESTERDAY DATE' - VB.NET

Dim tingin As Date = DateAndTime.DateAdd(DateInterval.Day, -1, DateAndTime.Now)
Dim trimmoto As String = Trim(tingin)
MsgBox(trimmoto)
I want to get the 'DATE ONLY'
Add :-
Dim tingin As Date = DateAndTime.DateAdd(DateInterval.Day, -1, DateAndTime.Now)
Dim trimmoto As String = tingin.ToString("yyyy/MM/dd")
MsgBox(trimmoto)
Or :-
Dim tingin As Date = Now.AddDays(-1)
Dim trimmoto As String = tingin.ToString("yyyy/MM/dd")
MsgBox(trimmoto)
I believe this has been already answered.
I just want to emphasize the use of depreciated VB Functions.
Do not use the TRIM() command anymore as this is already depreciated.
This dates back to VB6 and Classic VBSCRIPT.
It is just there for backward compatibility.
Use the .NET counterpart .TOSTRING.TRIM
Same goes with FORMAT(NumberVariable,"yyyy-dd-MM").
Use .TOSTRING.FORMAT(NumberVariable,"yyyy-dd-MM") instead.

Using a variable in coded date

I am trying to pull all data entries that are within a userform selected month and year. I can get the code to run fine when I hard code the year but I want the year to come off of a text box. I converted the Textbox value to an integer using Cint() and dim'd it to "Year" in my if statement. I can get it to work if I write Cdate("3/1/2016"), but I want see if there is a way to run it like: Cdate("3/1/Year"). I tried it this way and get a typematch error on the Cdate Im pretty new to VBA so excuse my stupidity.
Ignore the "Month" variable I was just using that to put a stop on the code and step it through to see if it would enter my if statement.
Thanks in advance.
My Code
Private Sub OKBtn_Click()
Dim Sales As Range
Dim Year As Integer
Dim Month As Integer
Dim i As Integer
Year = CInt(YearText.Value)
Set Sales = Worksheets("Sales").Range("A4")
i = 0
If Sales.Offset(i, 1).Value >= CDate("3/1/2016") And Sales.Offset(i, 1).Value <= CDate(" 3/31/2016 ") Then
Month = 1
End If
In order for the CDate to work, you need to seperate the stings inside the brackets to 2 parts
1.The constant, in your case "3/1/".
2.And the variable, CInt(YearText.Value).
Option Explicit
Private Sub OKBtn_Click()
Dim DDate As Date
DDate = CDate("3/1/" & CInt(YearText.Value))
' for debug only
MsgBox "Date entered is :" & DDate
End Sub

Add Zero to dates

I am trying to get a 4 digit date with a '/' in the middle. I have it all working, except on days that are single digit.
Example:
12/03 gets displayed as 12/3. My question is, if its a single digit, is there a way to add a zero to the front.
Current code:
Dim month As Integer = Now.Month
Dim day As Integer = Now.Day
TDate.Text = month & "/" & day
You can try something like this.
DateTime.Now.ToString("MM/dd")
If you want to update your existing code, this should help.
Dim month As Integer = DateTime.Now.Month
Dim day As Integer = DateTime.Now.Day
Dim a = month & "/" & day.ToString.PadLeft(2, "0"c)
This is where you would use format strings, i.e.
Date.Now.ToString("MM/dd")
MSDN reference on date/time format strings:
https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

how to convert the string to date in vb.net

I AM FACING ONE PROBLEM.
I AM USING BELOW METHOD FOR MAKING STRING VALUE TO DATE.
string values is like this ="01/12/2002" like "dd/MM/YYYY"
My problem is that.
two string values
->1)01/01/2025
->2)1/1/2025
i am getting the Value like above 1 or 2
Dim d As Date = DateTime.ParseExact(dumm, "dd/MM/yyyy", Nothing)
if 1 comes nothing will happen but
if i get 2 i am facing error like String was not recognized as a valid DateTime.
As per my Analysts what i understood is date should be 2 digits Remaining all are two digits other wise giving the error.
but some times i am getting single digits from the excel to vb.net
how can i solve this issue...
Dim dumm As String = DtSet3.Tables(0).Rows(k + 0).Item(3).ToString
Dim d As Date = DateTime.ParseExact(dumm, "d/M/yyyy", Nothing)
i put the break point on dumm ok dumm vlaues= "1/2/2026 12:00:00 Am"
Error...............
now
Dim dumm As String = DtSet3.Tables(0).Rows(k + 0).Item(3).ToString
Dim d As Date = DateTime.ParseExact("01/02/2026", "dd/MM/yyyy", Nothing)
Working Fine.......
Use the date format string "d/M/yyyy h:m:s tt". This will handle both cases i.e. with and without leading zero for the day and month. Additionally, since your actual variable has a time component in addition to the date, you need to add the format string for parsing time as well.
However, I would advise you to use TryParseExact, which will return boolean values based on success or failure of the parse rather than throwing exceptions.
Demo for using TryParseExact along with appropriate format string.