How to convert month, day, year, hour, min. to date (VBS)?

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
OlgaFB
Pro Scripter
Posts: 58
Joined: Mon Nov 01, 2004 3:04 pm
Contact:

How to convert month, day, year, hour, min. to date (VBS)?

Post by OlgaFB » Fri Aug 04, 2006 12:47 pm

Hi all,

Probably a stupid question, but I'm not finding any way to do it now... I see that VBScript can take a date string and read from there, but the manual says, it's under condition, that this string presents the system date/time format settings, and I don't know which format settings will be on that computer.

What can I do?

Thank you,
Olga.

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Aug 04, 2006 1:00 pm

Can you clarify what you are trying to do? I don't really understand what you are saying. VBScript has a number of date formatting functions which let you format the date anyway you want to, regardless of system settings.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

OlgaFB
Pro Scripter
Posts: 58
Joined: Mon Nov 01, 2004 3:04 pm
Contact:

Post by OlgaFB » Fri Aug 04, 2006 1:12 pm

Thank you, Marcus!!

I get as an input from the user day, month, year, hour, minutes and from this I should form a date that I can give then to functions like Add in Macro Scheduler or DateAdd in VBS.

I just remembered there was in JScript a function forming a date from separate numbers representing day, month, etc. - but I don't seem to find it in VBS. I see only CDate, which takes a date string, DateValue - the same, etc., but how do I form a date in the system format if I don't' know it?

Which functions did you have in mind that could do this?

Thank you!
Olga.

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Aug 04, 2006 1:23 pm

Use DateSerial. DateSerial takes the component parts and returns a VBScript date value:

http://msdn.microsoft.com/library/en-us ... 027662.asp

E.g:

Code: Select all

VBSTART
VBEND

Day>dd
Month>mm
Year>yyyy

VBEval>DateAdd("d",1,DateSerial(%yyyy%,%mm%,%dd%)),tomorrow

VBEval>Year("%tomorrow%"),yyyy
VBEval>Month("%tomorrow%"),mm
VBEval>Day("%tomorrow%"),dd
DateDiff is also useful for determining the number of days between dates. Don't forget you can also subtract days from dates with DateAdd by specifying a negative number.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

OlgaFB
Pro Scripter
Posts: 58
Joined: Mon Nov 01, 2004 3:04 pm
Contact:

Post by OlgaFB » Fri Aug 04, 2006 2:13 pm

Thank you!!! Seems to be exactly what I needed!

I thought how I can calculate the date together with time, so I wrote it like this:

Code: Select all

Function ReturnDate(intYears, intMonth, intDays, intHours, intMinutes)
        Dim MyTime
	MyTime = DateSerial(intYears, intMonth, intDays)
	MyTime = DateAdd("h", intHours, MyTime)
	MyTime = DateAdd("n", intMinutes, MyTime)
	ReturnDate = MyTime
End Function

'now the returned can be given to DateAdd, DatePart, DateDiff etc.  or printed out like this:

MsgBox Left(WeekDayName(Weekday(MyTime)),3) & ", " & Day(MyTime) & " " & MonthName(Month(MyTime),True)  & " " & Year(MyTime) & " " & FormatDateTime(MyTime,vbShortTime)
Thank you very much!
Olga.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts