Link to home
Start Free TrialLog in
Avatar of lmred
lmredFlag for United States of America

asked on

Date/Time

I have a variable that has a date datatype. I need that variable to contain the current date as well as the time. How can I do this?

Lisa
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Dim myDate As Date

myDate = Now()

Debug.Print myDate
Try using

Dim MyDate As Date
MyDate = Now()
Avatar of Madmarlin
Madmarlin

Dim myDate As Date

myDate = Format(Now(), "yyyy-mm-dd hh:mm:ss")

So at the time I worte this

mydate = '2002-12-03 15:38:33'



Avatar of lmred

ASKER

Should have asked this question a different way. The variable I have has a value in it. It is passed into my function. I need to take that variable and format it with that date and the time. For example:

Function Save Activity(dtActivity)

It needs to be formatted here so I can save it to a SQL Server table.


End Function
CDate(variable)
Will take a string = "3/12/02 13:00:00"
and convert it to a date value
For ex:
"INSERT Table (MyDateTime) VALUES('" & CDate(MyVar) & "')"

Or for ex:
Dim s as String
s = "2/12/02 13:00:00"
text1.text = CDate(s)
Text box will display:
2/12/2002 1:00:00 PM
(Using your regional settings as a template...)
also:
s = "2/12/02"
Text1.text = CDate(s)
Text box will display:
2/12/2002
(Using your regional settings as a template...)

"INSERT Table (MyDateTime) VALUES('" & CDate(s) & "')"

Would put the date and a default time of midnight, because no time was provided...
Avatar of lmred

ASKER

the variable comes in as datatype Date....but it still doesn't have the time with it....
ASKER CERTIFIED SOLUTION
Avatar of trkcorp
trkcorp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Imred,

Why is the time important?

SQL Server will accept the date without the time and stamp a default time (I believe 12:00:00) on it

If you need to query later based on the time, or you are doing OLTP where you need to know exactly WHEN you received the transaction maybe look at getting more information passed into the function.

Evan
<<the variable comes in as datatype Date....but it still doesn't have the time with it.... >>
???????????????
A Date variable does not require a time but will accept it if provided.  In other words, if your parameter came into your function without a time, there isn't a time to extrapolate from it...  If it did then there is, period, end of story...  
None of us seem to be getting what it is you are after.
Avatar of lmred

ASKER

When I passed this variable in, it was as the keyword Date. So time wasn't appended to it. Maybe I should've passed it in as Now(). I know SQL will accept it without time, but we need the time stored as well. I didn't realize this requirement when I first coded it.
Yeah, I think passing it as Now() would fix the problem.

Let me know if it works out!

Cheers!
Well, then there you go.  Pass Now.
Avatar of lmred

ASKER

Ok! Iused trkcorp's code and it returned the date and time.
Avatar of lmred

ASKER

Thanks everybody for your help!

Lisa
You're welcome...