Solved

Date format with VB.NET

Posted on 2004-10-10
11
1,234 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:BernardBouree
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
11 Comments
 
LVL 4

Accepted Solution

by:
Jigit earned 168 total points
ID: 12272199
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
 
LVL 4

Expert Comment

by:Jigit
ID: 12272204
0
 

Author Comment

by:BernardBouree
ID: 12272572
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:Dabas
ID: 12272575
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
 

Author Comment

by:BernardBouree
ID: 12272580
Dabas

onece more I need a DATE not a string .

Bernard
0
 
LVL 27

Assisted Solution

by:Dabas
Dabas earned 166 total points
ID: 12272629
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
 
LVL 27

Expert Comment

by:Dabas
ID: 12272633
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
 
LVL 10

Assisted Solution

by:123654789987
123654789987 earned 166 total points
ID: 12273944
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
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 12273958
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question