A Conversation for The H2G2 Programmers' Corner
Visual Basic Blind Date
Lakeman Started conversation Mar 27, 2003
Well, I've been sent to the Programmers Corner, so here goes:
How can I compare two dates in visual basic and get it to tell me if one is before or after the other?
And how can I construct a properly formated date from single values for day, month and year?
Thanks.
Visual Basic Blind Date
DoctorMO (Keeper of the Computer, Guru, Community Artist) Posted Mar 27, 2003
Hello,
For compare use simple
if MyDate1 = MyDate2 Then
if MyDate1 > MyDate2 Then
if MyDate1 < MyDate2 Then
ect..
To get components from the date, use the Format function
Format(MyDate1, "dd/mm/yyyy HH:MM:SS")
ect. (you can also use this function to format the number of decimal places.
-- DoctorMO --
Visual Basic Blind Date
DoctorMO (Keeper of the Computer, Guru, Community Artist) Posted Mar 27, 2003
I better explain a bit futher, if you want a single entry of the date/time use
Format(MyDate, "HH") 'Hours
It just returns what ever contruct you give it.
-- DoctorMO --
Visual Basic Blind Date
Lakeman Posted Mar 27, 2003
Thanks for that, but I'm afraid I'm still a little confused.
I have put this:
Format (Mydate2, "dd") = Day
Format (Mydate2, "mm") = Month
Format (Mydate2, "yyyy") = Year
where Day, Month and Year are variables that are already set.
Would this return MyDate2 as 9Day, month, year) that I can then add and subtract from another date. The only other date I need is today's date which I have as:
Format (Mydate1, "dd/mm/yyyy") = Date
I doesn't seem to be working. What am I doing wrong?
Cheers.
Visual Basic Blind Date
Jonny Posted Mar 27, 2003
There's actually a function called DateDiff
so you can have a date set to last year and a date set to now and you can have
dim diff as integer
diff = DateDiff("m",today, lastyear)
And diff will always be 12 (because of m is a string constant representing month. You can also use all the other date variables instead of m (such as yyyy for year and so on).
I hope that helps, I only discovered it a couple of months ago!
Jonny
Visual Basic Blind Date
DoctorMO (Keeper of the Computer, Guru, Community Artist) Posted Mar 27, 2003
Er, no you can't set a date like that, thats how you GET date constructs... I'll have a look on how you do the SET date contructs.
-- DoctorMO --
Visual Basic Blind Date
DoctorMO (Keeper of the Computer, Guru, Community Artist) Posted Mar 27, 2003
Right...
Dim Mydate As Date
Mydate = DateValue("12/01/2003")
Mydate = Mydate + TimeValue("13:50:31")
MsgBox (Format(Mydate, "YYYY/MM/DD HH:MM:SS"))
This works, it sets the Date, then the time. and then reformats it.
-- DoctorMO --
Key: Complain about this post
Visual Basic Blind Date
- 1: Lakeman (Mar 27, 2003)
- 2: DoctorMO (Keeper of the Computer, Guru, Community Artist) (Mar 27, 2003)
- 3: DoctorMO (Keeper of the Computer, Guru, Community Artist) (Mar 27, 2003)
- 4: Lakeman (Mar 27, 2003)
- 5: Jonny (Mar 27, 2003)
- 6: DoctorMO (Keeper of the Computer, Guru, Community Artist) (Mar 27, 2003)
- 7: DoctorMO (Keeper of the Computer, Guru, Community Artist) (Mar 27, 2003)
More Conversations for The H2G2 Programmers' Corner
Write an Entry
"The Hitchhiker's Guide to the Galaxy is a wholly remarkable book. It has been compiled and recompiled many times and under many different editorships. It contains contributions from countless numbers of travellers and researchers."