• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1287
  • Last Modified:

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 ?
  • 3
  • 2
  • 2
  • +2
3 Solutions
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") )

BernardBoureeAuthor Commented:

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


Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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!

BernardBoureeAuthor Commented:

onece more I need a DATE not a string .

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

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

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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now