tags:

views:

1749

answers:

3

Hi,

I need to get a date field from MM/dd/yyyy to yyyy/MM/dd in vb.net but it should still be a date field afterward so that I can match it against a date in a database.

At the moment all I'm managing to do is to change it to a string in that format.

I tried this type of code which also did not work.

 DateTime.Parse(yourDataAsAString).ToString("yyyy-MM-dd")
 fromDeString = String.Format("{0:yyyy/MM/dd}", aDate)
 fromDate = Format("{0:yyyy/MM/dd}", aDate)

Any help would be much apreciated, thanks

A: 

See this question.

Jeff Moser
That link points to the revision history of the question I think you want to refer to...
McWafflestix
Oops. Fixed. Thanks!
Jeff Moser
I'm still left with the date in a string format which causes a data type mismatch when I do a select statement
Domitius
A: 

Here's a sample module demonstrating the functionality you desire.

Imports System.Globalization
Module Module1
    Sub Main()
        Dim culture As New CultureInfo("en-us", True)
        Dim mmDDyy As String = "10/23/2009"
        Dim realDate As Date = Date.ParseExact(mmDDyy, "mm/dd/yyyy", culture)
        Dim yyMMdd As String = realDate.ToString("yyyy/MM/dd")
    End Sub

End Module

Hope this helps.

Kind Regards Noel

Bigtoe
I need yyMMdd to be "Dim yyMMdd AS Date" in the format specified not a String.but thanks anyway
Domitius
You missing the point Domitius. The realDate variable is the date, as a Date. yyMMDD is just a string representation of it. Change realDate
Bigtoe
+1  A: 

You're not understanding that a date object does not store the digits in any particular format. The only way to get the digits formatted in the order you want is to convert it to a string. Why do you need to compare them in a particular format? A date is a date no matter how it is formatted. 12/15/78 == 1978/12/15.

If you are not able to compare dates from the DB against a date object in VB, it is likely that the date you are comparing to in the database is being returned to you in string format, in which case you should covert it to a date object for comparison.

Dim sDate As String = "2009/12/15" 'Get the date from the database and store it as a string
Dim dDate As New Date(2009, 12, 15) 'Your date object, set to whatever date you want to compare against

Select Case Date.Compare(dDate, Date.Parse(sDate))
   Case 0
      'The dates are equal
   Case Is > 0
      'The date in the database is less
   Case Is < 0
      'The date in the database is greater
End Select