Solved

Date/Time

Posted on 2002-03-12
16
207 Views
Last Modified: 2010-05-02
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
0
Comment
Question by:lmred
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
  • +3
16 Comments
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 6856957
Dim myDate As Date

myDate = Now()

Debug.Print myDate
0
 
LVL 20

Expert Comment

by:hes
ID: 6856966
Try using

Dim MyDate As Date
MyDate = Now()
0
 
LVL 1

Expert Comment

by:Madmarlin
ID: 6857048
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'



0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:lmred
ID: 6857158
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
0
 
LVL 4

Expert Comment

by:trkcorp
ID: 6857691
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...)
0
 
LVL 4

Expert Comment

by:trkcorp
ID: 6857708
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...
0
 
LVL 1

Author Comment

by:lmred
ID: 6857765
the variable comes in as datatype Date....but it still doesn't have the time with it....
0
 
LVL 4

Accepted Solution

by:
trkcorp earned 50 total points
ID: 6857825
So, what do you want to do, time stamp it?

Function Save Activity(dtActivity)

dim s as string
if isdate(dtactivity) then
  s = cstr(dtactivity) & " " & format$(now,"hh:mm:ss")
  dtactivity = cdate(s)

'  "INSERT Table (MyDateTime) VALUES('" & dtactivity & "')"

end if
0
 
LVL 6

Expert Comment

by:ebosscher
ID: 6857948
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
0
 
LVL 4

Expert Comment

by:trkcorp
ID: 6858043
<<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.
0
 
LVL 1

Author Comment

by:lmred
ID: 6858095
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.
0
 
LVL 6

Expert Comment

by:ebosscher
ID: 6858104
Yeah, I think passing it as Now() would fix the problem.

Let me know if it works out!

Cheers!
0
 
LVL 4

Expert Comment

by:trkcorp
ID: 6858108
Well, then there you go.  Pass Now.
0
 
LVL 1

Author Comment

by:lmred
ID: 6858126
Ok! Iused trkcorp's code and it returned the date and time.
0
 
LVL 1

Author Comment

by:lmred
ID: 6858131
Thanks everybody for your help!

Lisa
0
 
LVL 4

Expert Comment

by:trkcorp
ID: 6858169
You're welcome...
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sorting multiple rows and columns, and count duplicates in Excel 2013 4 104
Child Form in front 4 66
Excel Automation VBA 19 116
VBA Replace Function - What am I missing? 3 51
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question