Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date formatting and conversion trouble.

Status
Not open for further replies.

sknyppy

Programmer
May 14, 1999
137
US
I have a datetime field in a mySQL table that contains the value "2002-08-15 13:00:00" but when I display the variable or try to copy it to another table it automatically converts to this format: "8/15/2002 1:00:00 PM".

How can I retain the initial formatting of the value when I display or copy this value?

I'm using mySQL/ASP for database and programming.

Thanks,
Dave
 
The problem here is that when the date is put into a variable in ASP then displaying that variable will result in it showing the default display type for date. This means that your date is being stored in an SQL Date/Time field in your database. The format it is displaying (2002-08-15 13:00:00) is not the format it is stored in, copied from, or returned as. This is just the default format that it is displayed in from inside the database.
The easiest way I can think of to display the date in the same format in your ASP page would be to build it yourself, either inline or with a function.
Code:
'assume we have your date in a variable called myDate
Response.Write year(myDate) & "-" & month(myDate) & "-" & day(myDate) & " " & hour(myDate) & ":" & minute(myDate) & ":" & second(myDate)

This is probably the shortest version if you do it manually. There are scripts out there for more elaborate versions of the built in FormatDateTime function, to allow you to specify any look for the date you want, but the drawback to them is that if you will only be displaying a date in a single format that will never change, it is easier and quicker for the server if you specifiy it rather than make the machine run a couple hundred extra lines of code.

-Tarwn ------------ My Little Dictionary ---------
Reverse Engineering - The expensive solution to not paying for proper documentation
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top