A Conversation for The H2G2 Programmers' Corner

Visual Basic Blind Date

Post 1

Lakeman

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

Post 2

DoctorMO (Keeper of the Computer, Guru, Community Artist)

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

Post 3

DoctorMO (Keeper of the Computer, Guru, Community Artist)

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

Post 4

Lakeman

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

Post 5

Jonny

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

Post 6

DoctorMO (Keeper of the Computer, Guru, Community Artist)

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

Post 7

DoctorMO (Keeper of the Computer, Guru, Community Artist)

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