How to give the dtpicker date – 1? - vb.net

Now using VS 2008
Before I used VB 6, now I Upgraded to VB 2008.
VB6 Code
sdate = DateToString(dtpicker1 - 1)
edate = DateToString(dtpicker2)
Above code is working fine.
After Upgraded to VB 2008
sdate = DateToString(dtpicker1._Value)
edate = DateToString(dtpicker2._Value)
If I Put
sdate = DateToString(dtpicker1._Value - 1)
It is showing Error.
How can I write a code like dtpicker1 – 1
Need VB code Help.

The Value is of type Object.
That is because the value of a datepicker can be either a valid date, or empty. So you need:
If IsDate(dtpicker1._Value) Then
sdate = CDate(dtpicker1._Value).AddDays(-1).ToShortDateString()
End If
If the value is empty, it actually has a type of System.DBNull , which means that for conversion to string, you can define two functions with identical names, but different parameter types:
Public Function DateToString(ByVal _date As Date) As String
Return _date.ToShortDateString()
End Function
Public Function DateToString(ByVal _date As DBNull) As String
Return "No date selected!"
End Function
You can then modify your previous code to this:
Dim theDate As Object
theDate = dtpicker1._Value
If IsDate(theDate) Then
theDate = CDate(theDate).AddDays(-1)
End If
sdate = DateToString(theDate)

I'm assuming you're trying to subtract one day from dtpicker1's value? If so, do this:
sdate = DateToString(dtpicker1._Value.AddDays(-1))

Bit unsure of what you are trying to do, but for the following solution I'm assuming that by -1 you are saying that you want to get the day before the date.
If so then use
dtpicker1._Value.AddDays(-1).ToString
On top of that if you want to format the string that comes out, you can do that by using a format string such as
dtpicker1._Value.AddDays(-1).ToString("dd/MM/yyyy")
which will print the date out like "20/08/2009".

Related

String to date error (Conversion from string to type date is not valid)

I want to crate date as MS Access like date (example -> #mm/dd/yyy#) input date can be string or date so I have created object variable to hold value of date. Then I convert it to msaccess date.
but it gives error. See the picture attached.
Before posting this question I had searched a lot, but I don't understand the solutions I found.
A major solution is ParseExact("Date String", "Format", System. IFormatProvider). But in my editor Intellisense does not recognize culturevalue. It tells me culturevalue is not defined.
Then I tried Date.tryparse(). It worked, but if I have to use this, then what is use of CDate conversion function?
Edit 1 :
Date.tryparse() does not work it returns false
in vb6
"#" & Month(dateIn) & "/" & Day(dateIn) & "/" & Year(dateIn) & "#"
works perfectly
It seems that you ask for a string expression for a VBA date value.
That can be done like this:
Dim DateIn As DateTime
DateIn = DateTime.Today
Dim Rval As String
Rval = DateIn.ToString("'#'MM'/'dd'/'yyyy'#'")
Console.WriteLine(Rval)
' Output:
' #07/10/2022#

How to get the time on a datetime column in vb.net on data reader

Could you help me point out what causing the error on my code? What i would like to achieved is to seperate the date and time in my date time column in the database (PT_TimeIn)
sDate = reader.GetDateTime("PT_TimeIn").ToShortDateString
sTime = reader.GetDateTime("PT_TimeIn").ToShortTimeString
The error is:
Conversion to string PT_TimeIn to type integer is not valid
The IDataRecord interface (which is one of the interfaces that the base DbDataReader class implements) only provides method Function GetDateTime(i As Integer) As Date. Its single parameter is an integer that specifies the field index. There is no overload of GetDateTime in IDataRecord that takes a string argument for the field name.
You could use IDataRecord's method Function GetOrdinal(name As String) As Integer to get the index of the field and pass that to the GetDateTime method. Something like this:
sDate = reader.GetDateTime(reader.GetOrdinal("PT_TimeIn")).ToShortDateString
sTime = reader.GetDateTime(reader.GetOrdinal("PT_TimeIn")).ToShortTimeString
Hope this helps.

How can I display a "dd/mm/yyyy" date as a "yyyy-mm-dd" date

I keep seeing similar questions for Python and PHP, but I don't have access to those functions since I'm not using either of them. The language I'm using is VB.NET. So if I have:
Dim date1 = "06/19/2015"
then I want to convert it to
"2015-06-19"
I can replace to "/" character using Replace() really easily, but I can't figure out how to order the day, month, and year around without it being too complex. Again, lots of similar questions on the web, but I'm having trouble finding one on that isn't using Python and PHP.
Build up a list of possible formats and use DateTime.TryParseExact(). Then pass your desired output format to DateTime.ToString().
Dim date1 As String = "06/19/2015"
Dim allowedFormats() As String = {"M/d/yy", "M/dd/yy", "MM/d/yy", "MM/dd/yy", "M/d/yyyy", "M/dd/yyyy", "MM/d/yyyy", "MM/dd/yyyy"}
Dim dt As DateTime
If DateTime.TryParseExact(date1, allowedFormats, Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, dt) Then
Dim date2 As String = dt.ToString("yyyy-MM-dd")
Debug.Print(date1 & " --> " & date2)
End If

vb.net System.IFormatProvider runtime error

I have this function in vb.net:
Function GetFirstDayOfMonth(ByVal dtDate As DateTime) As DateTime
Dim dtFrom As DateTime = dtDate
dtFrom = dtFrom.AddDays(-(dtFrom.Day - 1))
Return dtFrom
End Function
I am then calling it here:
GetFirstDayOfMonth(reader3.GetString(3).ToString("yyyy-MM-dd"))
I have checked the value of reader3.GetString(3) using a MsgBox and it equals
2015-02-23
But i am getting a runtime error saying:
Additional information: Unable to cast object of type 'System.String' to type 'System.IFormatProvider'.
You're passing a string value into a function that requires a DateTime.
Dim tmp as DateTime
' Don't use the string format here. If it's already
' a string - GetString - then it doesn't need a date format
' I recommend listening to #Hans Passant and #Bjørn-RogerKringsjå
' If your data is already a date, use the date functions to
' keep it as a date
Dim tmpStr As String = reader3.GetString(3)
If DateTime.TryParse(tmpStr, tmp) Then
GetFirstDayOfMonth(tmp)
End If
To keep it as a date without ever casting or formatting it (assuming it's a date object in the database)
Dim firstDay As DateTime = GetFirstDayOfMonth(reader3.GetDateTime(3))
Additionally, turning option strict on should turn this into a compile time error (much more useful).

how to filter by age in vb.net

I need to add a filter to filter data by a persons age.
Below is the code i am using for to filter tel and phone numbers.
The feild that collects/holds my customers DOB is "app.DOB"
Please assist me
Friend Overrides Function Filter() As String
Dim sb As New StringBuilder()
If (Not app.LandPhone.StartsWith("01")) And (Not app.LandPhone.StartsWith("02")) Then
sb.Append("Land phone must be present and start 01 or 02;")
End If
If (Not app.WorkPhone.StartsWith("01")) And (Not app.WorkPhone.StartsWith("02")) And (Not app.WorkPhone.StartsWith("08")) Then
sb.Append("Work phone must be present and start 01, 02 or 08;")
End If
If app.MobilePhone = String.Empty Then
sb.Append("Mobile phone must be present;")
End If
Return sb.ToString()
End Function
If DOb is of datetime, then the DateDiff function will be usefull.it will be like
if DateDiff(DateInterval.day,app.DOB,Date.Today)/365 > your filter value
''what you want to do
end if
Note:
now only i noticed your comment that it is string.i that case convert it to date using parseexact before using in datediff function like
if format of you DOB column is dd/MM/yyyy then
Dim dt = DateTime.ParseExact(app.DOB, "dd/MM/yyyy", Nothing)
if DateDiff(DateInterval.day,dt,Date.Today)/365 > your filter value
'' what you want
end if
you can change the format according to your grid format: