views:

349

answers:

3

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.

A: 

I'm assuming you're trying to subtract one day from dtpicker1's value? If so, do this:

sdate = DateToString(dtpicker1._Value.AddDays(-1))
Brent
When I press dtpicker1.value.(After . Press nothing display) when i enter .addday(-1).tostring. It displaying error
Gopal
What type of object is dtpicker? And what is the error?
Brent
A: 

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

link664
When I press dtpicker1.value.(After . Press nothing display) when i enter .addday(-1).tostring. It displaying error
Gopal
What's the error?
link664
+1  A: 

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