Results 1 to 6 of 6

Thread: DateTime Data Type

  1. #1
    paulthepaddy is offline VB.NET Forum Genius
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2011
    Location
    UK
    Posts
    177
    Reputation
    32

    DateTime Data Type

    Hi all,

    i am trying to use the datetime datatype in my application.

    i am using a datetime picker to enter the date, and it is's format is set to custom format dd/MM/yyyy

    here is the code for declairing the value


    Code:
    Private _DateDone As DateTime
    
    Public Property DateDone As DateTime
            Get
                Return _DateDone
            End Get
            Set(value As DateTime)
                _DateDone = value
            End Set
        End Property
    
    'The Passing Of The Value
    car.DateDone = CDate(FormatDateTime(Me.DateTimePicker1.Value, DateFormat.ShortDate))
    but my date is being saved into a text file as
    <DateDone>2012-07-16T00:00:00</DateDone>

    so how would i get the 'Private _DateDone As DateTime' to be defult as the dd/MM/yyyy format

  2. #2
    Dunfiddlin's Avatar
    Dunfiddlin is offline VB.NET Forum Master
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2012
    Posts
    253
    Reputation
    32
    If that's your normal format then its found in ...

    DateTimePicker1.Value.Date

    Not sure what all the formatting and converting is for. The whole point of the DateTimePicker is to return a DateTime value.

  3. #3
    ggunter is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Apr 2008
    Posts
    137
    Reputation
    99
    Paul,

    If I remember correctly, a database stores date/times as a number of elapsed seconds from a given date (sorry, don't remember the exact date). Therefore, what you are seeing when you open the database is a human readable format not the actual value. If you want the database table to display the value a certain way then you'll need to set the format value for that field.

  4. #4
    paulthepaddy is offline VB.NET Forum Genius
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2011
    Location
    UK
    Posts
    177
    Reputation
    32
    hi, sorry as i didn't explain my issue clear enough, was quite tired last night lol.

    thanks for the replys, but i dont think they solve my problem.

    the output im getting is an XML file off my object.

    im just curious as to why it is diffrent from all the formatting.

    the output to the xml file is as follows


    <DateDone> 2012-07 -16 T00:00:00</DateDone>
    yyyy-MM-dd HH:mm:ss

    when i want

    <DateDone> 16/07/2012</DateDone>

    it just seems to be ignoring all the formatting.

    In my database i have the date/time field set to short date (dd/mm/yyyy) so it WAS throwing an exeption as the format was wrong.(not now as database not working. need to fix lol)
    i just dont want diffrent date/time formats throughout my application as im SURE this will cause issues some time.

    thanks

  5. #5
    Herman is offline VB.NET Forum Miyagee
    .NET Framework
    .NET 4.0
    Join Date
    Oct 2011
    Location
    Montreal, QC, CA
    Posts
    466
    Reputation
    348
    In your database query you should never assume that what the user input is valid. Use the SQL function CONVERT(DATETIME, @MyDate) in your query, and it won't matter which of the standard formats you use.

  6. #6
    chphz is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    Aug 2012
    Posts
    4
    Reputation
    0
    Hi paulthepaddy

    I don't think it's necessary to write the code like

    car.DateDone = CDate(FormatDateTime(Me.DateTimePicker1.Value, DateFormat.ShortDate))

    You don't need to format the datetime value when you assign a datetime value to a datetime property.

    You just need to use the function FormatDateTime Before you save the object value to XML file. I don't know what the code is, which you use to save object to XML, but I think that's the problem comes from.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking