DateTime Data Type

paulthepaddy

Well-known member
Joined
Apr 9, 2011
Messages
222
Location
UK
Programming Experience
Beginner
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


VB.NET:
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
 
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.
 
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.
 
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
 
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.
 
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.
 
Back
Top