compare times with WMIC

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

compare times with WMIC

#1 Post by npocmaka_ » 10 Jun 2015 11:38

Just found that when datetimes are compared in WMIC queries you need to use the whole 25 long datetime string (e.g. 20150610150858.712825+180 ) and decided its worth to be shared .Here's an example (pretty simple that checks if the computer has been booted today):


Code: Select all

@echo off

:: time zone is not used to deal better with - and + signs
for /f %%$ in ('wmic os get LastBootUpTime /format:value') do (
    for /f %%# in ("%%$") do set "%%#"
)
set offset=%LastBootUpTime:~21,4%

::get current time
for /f %%$ in ('wmic os get LocalDateTime /format:value') do (
    for /f %%# in ("%%$") do set "%%#"
)


set "today=%LocalDateTime:~0,8%"

set "endOfTheDay=%today%235959.999999%offset%"
set "startOfTheDay=%today%000000.000000%offset%"

set "_path=%_path:\=\\%\\"
rem and LastModified<='%edate%' and LastModified>='%bdate%'
for /f "skip=1 tokens=* delims=" %%# in ('
   wmic os where "LastBootUpTime<='%endOfTheDay%' and LastBootUpTime>='%startOfTheDay%' " get LastBootUpTime /Format:table
') do (
   for /f "tokens=* delims=" %%A in ("%%#") do (
      if "%%A" neq "" echo booted today   
   )

)


date time is a primitive type in MOF/CIM/WMI

Code: Select all

https://msdn.microsoft.com/en-us/library/aa387237(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/aa389802(v=vs.85).aspx


And is used in a lot of WMI objects (file times , os times ...)

I don't know if is possible to be used in WQL queries and within clause (with intervals):

Code: Select all

https://msdn.microsoft.com/en-us/library/aa394527(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/aa390895(v=vs.85).aspx

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: compare times with WMIC

#2 Post by Aacini » 10 Jun 2015 12:04

npocmaka_ wrote:Just found that when datetimes are compared in WMIC queries you need to use the whole 25 long datetime string (e.g. 20150610150858.712825+180 )...

See this dbenham post.

EDIT: Ops! In the original post, Dave said exactly the same ("WMIC use a 25 long datetime string with.."), but he edited that post since the last time I saw it! :?

RE-EDIT: My mistake! This is the post I wanted refer to... :oops:

npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: compare times with WMIC

#3 Post by npocmaka_ » 10 Jun 2015 14:07

Aacini wrote:
My mistake! This is the post I wanted refer to... :oops:



Missed that.Thanks.So this thread is useless.

Post Reply