Jump to content

How do you convert a WMI DateTime using just batch script?


0phoi5

Recommended Posts

Hi all,

I'm using...

wmic /node:%Hostname% os get lastbootuptime

... which gives the output as a WMIDateTime (Last time a PC was rebooted)...

20160104102930

Is there a way for me to convert this output to a nicely formatted date/time from within the batch file?

In this case, I'd like the output to be something like 04/01/2016 10:29

I've had a look around the net, but couldn't find what I needed. Surely batch can do this, without having to rely on Javascript/Powershell?

Thank you.

Edited by haze1434
Link to comment
Share on other sites

If you've got your program output from wmic in an environment variable (%X% in the example) then you can use something like:

set lastBootUpTime="%X:~06,2%/%X:~4,2%/%X:~0,4% %X:~8,2%:%X:~10,2%"
Link to comment
Share on other sites

Try something like

for /f "skip=1 tokens=2 delims==." %%A in ('wmic os get lastbootuptime /format:value') do set BootTime=%%A

I'm afraid this didn't work.

The output is still 20160104102930

My code at the moment is...

for /f "skip=1 tokens=1 delims=. " %%A in ('wmic /node:%Hostname% os get lastbootuptime') do if not defined BootTime set BootTime=%%A

I have also tried...

for /f "skip=1 tokens=2 delims==." %%A in ('wmic os get lastbootuptime /format:value') do set BootTime=%%A

... but %BootTime% still echos 20160104102930

I also tried converting it using...

set BOOTyear=%BootTime:~-4,4%
set BOOTmonth=%BootTime:~-8,2%
set BOOTday=%BootTime:~0,2%
set BootTimeFin=%BOOTyear%%BOOTmonth%%BOOTday%

...but %BootTimeFin% randomly echos as 29300420 (absolutely no idea where these numbers are coming from!)

Link to comment
Share on other sites

The line

for /f "skip=1 tokens=2 delims==." %%A in ('wmic os get lastbootuptime /format:value') do set BootTime=%%A

is just to put the output of the wmic command into the BootTime environment variable, so the value of 20160104102930 is correct. You then nee to parse it. When you do

set BOOTyear=%BootTime:~-4,4%
set BOOTmonth=%BootTime:~-8,2%
set BOOTday=%BootTime:~0,2%
set BootTimeFin=%BOOTyear%%BOOTmonth%%BOOTday%

you are setting BOOTyear to be the last 4 characters (2930) as you are asking it to start 4 characters from the end of the string and take the next 4 characters. You would want the -4 to be 0 to get the correct year. You are making the same sort of mistakes with BOOTmonth and BOOTday.

Try converting it with the following

set BOOTyear=%BootTime:~0,4%
set BOOTmonth=%BootTime:~4,2%
set BOOTday=%BootTime:~6,2%
set BootTimeFin=%BOOTyear%%BOOTmonth%%BOOTday%

If you want to get a better understanding of how substringing environment varaibles works in batch then try reading http://ss64.com/nt/syntax-substring.html

Link to comment
Share on other sites

Perfect :happy:

for /f "skip=1 tokens=1 delims=. " %%A in ('wmic /node:%Hostname% os get lastbootuptime') do if not defined BootTime set BootTime=%%A

set BOOTyear=%BootTime:~0,4%
set BOOTmonth=%BootTime:~4,2%
set BOOTday=%BootTime:~6,2%
set BOOThour=%BootTime:~8,2%
set BOOTminute=%BootTime:~10,2%
set "BootTime=%BOOTday%/%BOOTmonth%/%BOOTyear% at %BOOThour%:%BOOTminute%"
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...