2

Quick question for an ASP experts out there! I am reading a SQL database using ASP. The fieldtype in SQL is "datetime" ie. 2012-08-15 08:08:20.140

However when I read this via Classic ASP ADODB RecordSet I get "15/08/2012". This is slightly un-helpful as I would like it to read in in the exact same format as it appears on SQL Server.

Is there any way I can stop ASP changing the date and just read it in as it appears from SQL Server in YYYY-MM-DD format?

Dean
  • 360
  • 1
  • 10
  • 27
  • 1
    possible duplicate of [How to transform a date-string in classic asp](http://stackoverflow.com/questions/91734/how-to-transform-a-date-string-in-classic-asp) – Diodeus - James MacFarlane Oct 04 '12 at 18:13
  • 1
    Don't think it's a duplicate: this one asks about how to avoid an automatic conversion whilst the other asks how to convert the date format. – The Unfun Cat Oct 05 '12 at 09:46

1 Answers1

0

You could use a combination of SetLocale and FormatDateTime.

About SetLocale: http://www.w3schools.com/vbscript/func_setlocale.asp

About FormatDateTime: http://www.w3schools.com/vbscript/func_formatdatetime.asp

Here is an example:

SetLocale 4105 ' 4105 => Canada, which is ISO-8601 (YYYY-MM-DD)

SQL = "select user_date from user"
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open Application("ConnectionString")
Set cmd = Server.CreateObject("ADODB.Command")
cmd.ActiveConnection = conn
cmd.CommandType = 1
cmd.CommandText = SQL
Set rst = Server.CreateObject("ADODB.Recordset")
rst.CursorLocation = 3
Set rst = cmd.Execute

echo FormatDateTime(rst('user_date').Value,vbShortDate);
ews2001
  • 2,176
  • 3
  • 26
  • 38