Solved

Date format with VB.NET

Posted on 2004-10-10
11
1,213 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
  • 3
  • 2
  • 2
  • +2
11 Comments
 
LVL 4

Accepted Solution

by:
Jigit earned 168 total points
Comment Utility
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
Comment Utility
0
 

Author Comment

by:BernardBouree
Comment Utility
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
 
LVL 27

Expert Comment

by:Dabas
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:BernardBouree
Comment Utility
Dabas

onece more I need a DATE not a string .

Bernard
0
 
LVL 27

Assisted Solution

by:Dabas
Dabas earned 166 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VB.Net - 2005 Project Not Responding 24 47
Find value in LINQ result ? 4 17
Form design in vb.net 7 18
Change data in datatable 8 15
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now