How do I get the Date & Time (VBS)

前端 未结 7 1950
臣服心动
臣服心动 2020-12-24 09:11

How do I get the current date and time using VBS (for Windows. I\'m not looking for VBScript for ASP/ASPX or webpages).

相关标签:
7条回答
  • 2020-12-24 09: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:

    1. There are all the DateTime functions in vbscript defined here so you can mix-n-match to get the result that you want
    2. What I needed was to get the current date and format it in YYYYMMDD to do that I just needed to concat DatePart like so for the current Date: date = DatePart("yyyy",Date) & DatePart("m",Date) & DatePart("d",Date)

    That's all, I hope this helps someone.

    0 讨论(0)
  • 2020-12-24 09:27

    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
    

    Source

    0 讨论(0)
  • 2020-12-24 09:30

    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.

    0 讨论(0)
  • 2020-12-24 09:33

    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
    
    0 讨论(0)
  • 2020-12-24 09:33

    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
    
    0 讨论(0)
  • 2020-12-24 09:37

    There are also separate Time() and Date() functions.

    0 讨论(0)
提交回复
热议问题