home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   alt.msdos.batch.nt      Fun with Windows NT batch files      68,980 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 67,707 of 68,980   
   JJ to Tom Del Rosso   
   Re: 24-hour format to 12-hour format usi   
   06 Dec 20 16:58:48   
   
   From: jj4public@gmail.com   
      
   On Sat, 5 Dec 2020 20:50:10 -0500, Tom Del Rosso wrote:   
   > The last line is the thing I want to point out. It's a single equation   
   > that converts the hour from 24-hour format to 12-hour format. I'm sure   
   > this would run faster than an IF statement and more so if the IF was   
   > followed by a GOTO. In the past I have used IF because I hadn't thought   
   > of doing it all in arithmetic. But I don't see a way to avoid the IF   
   > that chooses am and pm.   
   >   
   > set tm=%time%   
   > set hr=%tm:~0,2%   
   > if %hr% LSS 12 (set AMPM=am) else (set AMPM=pm)   
   > set /a hr=hr%%12 + !hr*12 + !(hr-12)*12   
      
   You can use the ! operator trick like you did for the hour number conversion   
   part, but it'll need more commands which makes it slower than a single IF.   
   e.g.   
      
   @echo off   
   setlocal   
   set tm=%time%   
   set hr=%tm:~0,2%   
      
   set /a hr2=hr%%12 + !hr*12 + !(hr-12)*12   
      
   set /a i=hr/12   
   rem i: 0=am, 1=pm   
   set AMPM=ap   
   call set AMPM=%%AMPM:~%i%,1%%m   
      
   echo %tm% = %hr2%%AMPM%   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca