How do I get the current date and time using VBS (for Windows. I'm not looking for VBScript for ASP/ASPX or webpages).
-
http://www.w3schools.com/vbscript/func_date.asp – asawyer Aug 10 '11 at 13:18
-
asawyer thats for webpages. Not seeking webpages. Even if your answer was correct, I can't give you credit because you added it as a comment. – Cocoa Dev Aug 10 '11 at 13:23
7 Answers
Here's various date and time information you can pull in vbscript running under Windows Script Host (WSH):
Now = 2/29/2016 1:02:03 PM
Date = 2/29/2016
Time = 1:02:03 PM
Timer = 78826.31 ' seconds since midnight
FormatDateTime(Now) = 2/29/2016 1:02:03 PM
FormatDateTime(Now, vbGeneralDate) = 2/29/2016 1:02:03 PM
FormatDateTime(Now, vbLongDate) = Monday, February 29, 2016
FormatDateTime(Now, vbShortDate) = 2/29/2016
FormatDateTime(Now, vbLongTime) = 1:02:03 PM
FormatDateTime(Now, vbShortTime) = 13:02
Year(Now) = 2016
Month(Now) = 2
Day(Now) = 29
Hour(Now) = 13
Minute(Now) = 2
Second(Now) = 3
Year(Date) = 2016
Month(Date) = 2
Day(Date) = 29
Hour(Time) = 13
Minute(Time) = 2
Second(Time) = 3
Function LPad (str, pad, length)
LPad = String(length - Len(str), pad) & str
End Function
LPad(Month(Date), "0", 2) = 02
LPad(Day(Date), "0", 2) = 29
LPad(Hour(Time), "0", 2) = 13
LPad(Minute(Time), "0", 2) = 02
LPad(Second(Time), "0", 2) = 03
Weekday(Now) = 2
WeekdayName(Weekday(Now), True) = Mon
WeekdayName(Weekday(Now), False) = Monday
WeekdayName(Weekday(Now)) = Monday
MonthName(Month(Now), True) = Feb
MonthName(Month(Now), False) = February
MonthName(Month(Now)) = February
Set os = GetObject("winmgmts:root\cimv2:Win32_OperatingSystem=@")
os.LocalDateTime = 20131204215346.562000-300
Left(os.LocalDateTime, 4) = 2013 ' year
Mid(os.LocalDateTime, 5, 2) = 12 ' month
Mid(os.LocalDateTime, 7, 2) = 04 ' day
Mid(os.LocalDateTime, 9, 2) = 21 ' hour
Mid(os.LocalDateTime, 11, 2) = 53 ' minute
Mid(os.LocalDateTime, 13, 2) = 46 ' second
Dim wmi : Set wmi = GetObject("winmgmts:root\cimv2")
Set timeZones = wmi.ExecQuery("SELECT Bias, Caption FROM Win32_TimeZone")
For Each tz In timeZones
tz.Bias = -300
tz.Caption = (UTC-05:00) Eastern Time (US & Canada)
Next

- 2,164
- 5
- 31
- 48

- 14,557
- 12
- 70
- 91
To expound on Numenor's answer you can do something like, Format(Now(),"HH:mm:ss") using these custom date/time formating options
For everyone who is tempted to downvote this answer please be aware that the question was originally tagged VB and vbscript hence my answer, the VB tag was edited out leaving only the vbscript tag. The OP accepted this answer which I take to mean that it gave him the information that he needed.

- 53,938
- 9
- 94
- 111
-
3@Ekkehard.Horner You seem to have missed the Vb tag that Joel Coehoorn edited out on Feb 27. This question was originally tagged Vb and VBScript hence the answer that I gave, and it seemed to meet the OP's requirements since he marked it as the answer. – Mark Hall Mar 11 '12 at 04:35
-
2I just want the answer to be correct for the question as it is now. I shouldn't have down voted, my apologies. – Ekkehard.Horner Mar 11 '12 at 10:55
For VBScript use FormatDateTime, which has 5 numerical arguments to give you one of 5 predefined formats. Its not great.
FormatDateTime(now, 4)
08:12

- 3,265
- 2
- 21
- 22
This is an old question but alot of the answers in here use VB or VBA. The tag says vbscript (which is how I got here).
The answers here got kind of muddled since VB is super broad where you can have so many applications of it. My answer is solely on vbscript and accomplishes my case of formatting in YYYYMMDD in vbscript
Sharing what I've learned:
- There are all the
DateTime
functions in vbscript defined here so you can mix-n-match to get the result that you want - What I needed was to get the current date and format it in
YYYYMMDD
to do that I just needed to concatDatePart
like so for the current Date:date = DatePart("yyyy",Date) & DatePart("m",Date) & DatePart("d",Date)
That's all, I hope this helps someone.

- 2,928
- 5
- 30
- 47
-
However DatePart doesn't return leading "0", see example given on https://www.w3schools.com/asp/func_datepart.asp : `d=CDate("2010-02-16") DatePart("m",d)` returns 2 not 02. So you have to pad like in this answer below: https://stackoverflow.com/a/52822934/666414 – maxxyme Jul 03 '19 at 08:47
Show time in form 24 hours
Right("0" & hour(now),2) & ":" & Right("0" & minute(now),2) = 01:35
Right("0" & hour(now),2) = 01
Right("0" & minute(now),2) = 35
now
returns the current date and time

- 1,686
- 12
- 20
-
1Can I manipulate the returned string? for example. I want to have the time in 24 hour format (no AM/PM) – Cocoa Dev Aug 10 '11 at 13:22
There are also separate Time()
and Date()
functions.

- 2,572
- 4
- 21
- 28

- 10,327
- 5
- 46
- 69