Date format with VB.NET

I have some problems with the date formats with VB.NET
Here is my code:

dim dDateF as New DateTime()
dDateF = Today
'Suppose Today = 31/12/2003 format FRANCE
dDatef = Format(dDate, "dd/MM/yy")
'give me  12/31/03 that is the US format !!!???

My regional settings are set to FRANCE (WinXP)
and if I add the following line :
Thread.CurrentThread.CurrentCulture= New CultureInfo("fr-FR")
The result is unchanged.

Someone can help me ?
Thanks
BernardBoureeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JigitCommented:
Bonjour, BernardBouree!

DateTime stores date and time regardless regional settings, culture, format and programmer's eyes color. Date and time are stored just as date and time. Different formats make sense only when displaying the date or converting it to string. So the best way for DateTime manipulating:
1) Check date on users' input and make sure the data is entered is in correct format and is valid
2) Make all the date/time calculations, manipulations (including storing/retrieving in DB) regardless the format
3) Format date/time to the desired format when displaying them

Try running the following code:

Dim dDateF as DateTime = DateTime.Now()
Console.WriteLine( dDateF.ToString("dd/MM/yy") )
Console.WriteLine( dDateF.ToString("MM/dd/yy") )

HTH,
Jigit
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BernardBoureeAuthor Commented:
Jigit

My point is that I need to have a date (NOT A STRING) formated with the french one.
I need to transfer to a COM object a DATE with the french format taht is dd/MM/yy


Bernard

0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

DabasCommented:
Hi BernardBouree:
A date is stored in memory as a number where the integer part represents the date and the fractional part represents the time.
Formatting the date to show a user is best handled by converting the date to a string. Which is best done the way Jigit has proposed.
If you need a formatted string with the date in the format you want, you can add a string variable:

Dim sDateF as String = dDateF.ToString("dd/MM/yy") and voila, you can use the sDateF variable to suit your puposes!


Dabas
0
BernardBoureeAuthor Commented:
Dabas

onece more I need a DATE not a string .

Bernard
0
DabasCommented:
BernardBouree:
As mentioned before a date is a date.
When you say dDateF = Today (assuming 11/10/2004)
The actual value of the integer part of dDateF is 38271
It is 38271 in France, in Australia in the USA or in Mongolia. It still is 38271

Dabas
0
DabasCommented:
As to passing to the COM object, it might depend as to in which language the COM object was written and what the COM object expects a date to be.
0
123654789987Commented:
U can do this

Imports System.Globalization
Imports System.Threading

Dim dt as Date
Dim str as string

Dim culture As CultureInfo = New CultureInfo( cultureName )
str = Now.ToString("dd/mm/yyyy hh:mm tt")

dt = Convert.ToDateTime(str,CultureInfo )

dt gives the date in correct format.


See the following MSDN sample for date formatting using culture

Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic

Module StringToDateTimeDemo

    Const lineFmt As String = "{0,-18}{1,-12}{2}"
   
    ' Get the exception type name; remove the namespace prefix.
    Function GetExceptionType( ex As Exception ) As String

        Dim exceptionType   As String = ex.GetType( ).ToString( )
        Return exceptionType.Substring( _
            exceptionType.LastIndexOf( "."c ) + 1 )
    End Function

    Sub StringToDateTime( cultureName As String )

        Dim dateStrings As String( ) = { "01/02/03", _
            "2001/02/03",  "01/2002/03", "01/02/2003", _
            "21/02/03",    "01/22/03",   "01/02/23" }
        Dim culture As CultureInfo = New CultureInfo( cultureName )
           
        Console.WriteLine( )

        ' Convert each string in the dateStrings array.
        Dim dateStr As String
        For Each dateStr In  dateStrings

            Dim dateTimeValue As DateTime

            ' Display the first part of the output line.
            Console.Write( lineFmt, dateStr, cultureName, Nothing )

            ' Convert the string to a DateTime object.
            Try
                dateTimeValue = _
                    Convert.ToDateTime( dateStr, culture )

                ' Display the DateTime object in a fixed format
                ' if Convert succeeded.
                Console.WriteLine( "{0:yyyy-MMM-dd}", dateTimeValue )

            ' Display the exception type if Parse failed.
            Catch ex As Exception
                Console.WriteLine( "{0}", GetExceptionType( ex ) )
            End Try
        Next dateStr
    End Sub
   
    Sub Main( )
        Console.WriteLine( "This example of " & _
            "Convert.ToDateTime( String, IFormatProvider ) " & _
            vbCrLf & "generates the following output. Several " & _
            "strings are converted " & vbCrLf & "to DateTime " & _
            "objects using formatting information from different " & _
            vbCrLf & "cultures, and then the strings are " & _
            "displayed in a " & vbCrLf & "culture-invariant form." & _
            vbCrLf )
        Console.WriteLine( lineFmt, "Date String", "Culture", _
            "DateTime or Exception" )
        Console.WriteLine( lineFmt, "-----------", "-------", _
            "---------------------" )

        StringToDateTime( "en-US" )
        StringToDateTime( "ru-RU" )
        StringToDateTime( "ja-JP" )
    End Sub
End Module

' This example of Convert.ToDateTime( String, IFormatProvider )
' generates the following output. Several strings are converted
' to DateTime objects using formatting information from different
' cultures, and then the strings are displayed in a
' culture-invariant form.
'
' Date String       Culture     DateTime or Exception
' -----------       -------     ---------------------
'
' 01/02/03          en-US       2003-Jan-02
' 2001/02/03        en-US       2001-Feb-03
' 01/2002/03        en-US       2002-Jan-03
' 01/02/2003        en-US       2003-Jan-02
' 21/02/03          en-US       FormatException
' 01/22/03          en-US       2003-Jan-22
' 01/02/23          en-US       2023-Jan-02
'
' 01/02/03          ru-RU       2003-Feb-01
' 2001/02/03        ru-RU       2001-Feb-03
' 01/2002/03        ru-RU       2002-Jan-03
' 01/02/2003        ru-RU       2003-Feb-01
' 21/02/03          ru-RU       2003-Feb-21
' 01/22/03          ru-RU       FormatException
' 01/02/23          ru-RU       2023-Feb-01
'
' 01/02/03          ja-JP       2001-Feb-03
' 2001/02/03        ja-JP       2001-Feb-03
' 01/2002/03        ja-JP       2002-Jan-03
' 01/02/2003        ja-JP       2003-Jan-02
' 21/02/03          ja-JP       2021-Feb-03
' 01/22/03          ja-JP       FormatException
' 01/02/23          ja-JP       2001-Feb-23



0
arif_eqbalCommented:
There is no need to format the dat value in French Date Format
See as Dabas said internnally it is a Number be it in VB.NEt or COM anywhere

But when you display it to the user you need to show him a familiar format so just take care that when you display it you format it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.