View Full Version : Time() Function
michaelwall
12-08-2005, 10:43 AM
What type of variable does the Time() function return?
I use s for String, i for Integer, o for Object... not sure what prefix to use here
time=Time()
BarrMan
12-08-2005, 01:02 PM
use a string for it.
TheShaner
12-08-2005, 04:13 PM
In VBScript for ASP, you don't cast variables as any kind of type, as I'm sure you know. If you were programming in VB, however, you'd cast it as a Date format. So although you're not casting any kind of type, the return value of the Time function will be in a date format. The variable you use doesn't matter, but if you'd like to keep things organized so that you know what type of value is stored in it, then you can use a "d" prefix or whatever else to designate that it's in a Date format.
-Shane
michaelwall
12-08-2005, 06:21 PM
Guys,
I've done a bit of research myself on this and although it's a variant, it's sub type is date as theShaner correctly says.
The code I got was
myTime=Time()
Response.write VarType(myTime)
'it outputs an 7 for a Date sub type
funny the Month() day() year() functions return an integer.
thanks.
TheShaner
12-08-2005, 07:00 PM
Yeah, they will output integers because they return the month, day, and year as.. well.. integers, hehe. Date() and Now(), like Time(), will output Date sub types.
-Shane
michaelwall
12-08-2005, 07:25 PM
In the code below should I use CDate(iYear & "-" & iMonth & "-" & iDay) to convert the string to a date?
Or how (or why) does the Access database allow a string input into a Date/Time datatype?
Function AccessDate(dateandtime)
Dim iDay
Dim iMonth
Dim iYear
iDay = Day(dateandtime)
iMonth = Month(dateandtime)
iYear = Year(dateandtime)
AccessDate = iYear & "-" & iMonth & "-" & iDay
End Function
Dim connection
Dim SQL, sConnString
SQL="INSERT INTO tblDate (dDate) VALUES (#" & AccessDate(NOW()) & "#)"
TheShaner
12-08-2005, 07:33 PM
Use the DateSerial function.
AccessDate = DateSerial(iYear, iMonth, iDay)
However, like you said, if a string has a date format, VB (and VBScript) can convert it into a Date format. But you should use DateSerial.
Now, looking at your code, you are really making that complicated, hehe. Just do this for your query and you can skip using that function entirely:
SQL="INSERT INTO tblDate (dDate) VALUES (#" & Date() & "#)"
-Shane
michaelwall
12-08-2005, 08:29 PM
I want to achieve the YYYY-MM-DD format so using Date() won't work as far as I'm aware.
thanks.
TheShaner
12-08-2005, 08:58 PM
But if you're just storing it in Access, Access has control of how it's formatted, no matter how you format it before you insert it. So if you want it in that format, when you extract it from the DB, that's when you apply the formatting.
-Shane
michaelwall
12-09-2005, 07:00 AM
Best to be safe when working with British/American dates.
I just ran a test on a British and American server.
If I use your SQL="INSERT INTO tblDate (dDate) VALUES (#" & Date() & "#)"
Then using the same code and same database for each test I'll end up with two different formats in the database when I open it up(remember I'm in the UK and dates are in DD/MM/YYYY).
12/08/2005 - UK test
08/12/2005 - US test
However if I use the YYYY-MM-DD format and open the database I don't - I get.
08/12/2005 - UK test
08/12/2005 - US test
What do you think Shane, my mind's made up now.
TheShaner
12-09-2005, 03:26 PM
Ah ha. Didn't realize you were working with both date formats. Go with what works then! :thumbsup:
-Shane
michaelwall
12-10-2005, 07:13 AM
thanks Shane for your help..:)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.