banner



For F Tokens 1 2 Delims

Rob van der Woude's Scripting Pages

Appointment and Time in Windows NT 4 and later

Parsing Date and Time

We often want to get the value for the day, month or twelvemonth instead of the consummate date string.
To achieve this, we can cull between FOR /F or SET'due south substring functionality.
All following code assumes nosotros got the values for Today and Now using one of the techniques described earlier in The Basics.

FOR /F

To utilize FOR /F we must know what delimiter is used. For date values, either nuance or forward slash is a rubber guess:

FOR /F "tokens=1-three delims=/-" %%A IN ("%Today%") DO ( 	Gear up DayMonth=%%A 	SET MonthDay=%%B 	Ready Twelvemonth=%%C )

Now all we need to do is find out which value contains the mean solar day and which one the month.

There are 2 means to find the date format used by the electric current user, and thus the gild of twenty-four hour period and month: the DATE command itself, or the registry. The first is simpler, but still language dependent, the latter is safer but more complex.

The DATE command displays the appointment order when it prompts for input:

C:\>appointment The current date is: 22-09-2022  Enter the new date: (dd-mm-yy)

Or:

C:\>appointment The electric current engagement is: Thu 9/22/2022  Enter the new date: (m/d/yy)

The same string designating the order tin can be institute in the registry value HKEY_CURRENT_USER\Command \International\sShortDate.

Note that this string is withal language dependent, every bit in Dutch it will be dd-MM-jjjj, and in French information technology will probably be something similar jj-MM-aaaa.
Both designate the aforementioned order of day and month, simply the strings are completely different.

Only apply this method if you are 100% certain of the system language.

If you are, you tin utilize this technique to read and set the values in the correct order:

:: For REG.EXE iii.0 (Windows XP) and later on versions FOR /F "tokens=3" %%A IN ('REG QUERY "HKCU\Control Panel\International" /five sShortDate 2ˆ>NUL') DO ( 	SET sShortDate=%%A ) :: For earlier REG.EXE versions FOR /F "tokens=3" %%A IN ('REG QUERY "HKCU\Control Panel\International\sShortDate"    2ˆ>NUL') Do ( 	Ready sShortDate=%%A ) Repeat.%sShortDate% | FINDSTR /R /B /I /C:"dd*[-/]mm*[-/]yyyy$" >NUL IF NOT ERRORLEVEL i ( 	SET Day=%%A 	SET Calendar month=%%B 	SET Yr=%%C ) Echo.%sShortDate% | FINDSTR /R /B /I /C:"mm*[-/]dd*[-/]yyyy$" >NUL IF Not ERRORLEVEL 1 ( 	Ready Day=%%B 	Ready Month=%%A 	SET Year=%%C ) Repeat.%sShortDate% | FINDSTR /R /I /C:"yy*[-/]mm*[-/]dd" >NUL IF NOT ERRORLEVEL i ( 	SET Mean solar day=%%C 	SET Month=%%B 	Prepare Twelvemonth=%%A )

Registry

A safer way is using the registry values iDate and sDate for engagement values, and iTime and sTime for time values. These values can be plant in HKEY_CURRENT_USER\Control \International likewise.
The "s" in sDate and southTime stands for due southeparator (delimiter). The "i" in iDate and iFourth dimension probably stands for international setting.

Date/Time Format Settings
Name 0 ane ii
iDate MM/DD/YYYY DD/MM/YYYY YYYY/MM/DD
iTime 12 hr clock 24 hour clock N/A
iTLZero no leading zero (ix:15) leading zero (09:fifteen) Due north/A

We tin read the value of iDate using REG.EXE, native every bit of Windows 2000, or available for NT4 in i of the Resource Kits:

:: For REG.EXE 3.0 (Windows XP) and later on versions FOR /F "tokens=three" %%A IN ('REG QUERY "HKCU\Control Panel\International" /v iDate 2ˆ>NUL') DO ( 	SET iDate=%%A ) :: For earlier REG.EXE versions FOR /F "tokens=iii" %%A IN ('REG QUERY "HKCU\Control Console\International\iDate"    2ˆ>NUL') DO ( 	SET iDate=%%A ) Repeat HKEY_CURRENT_USER\Command Console\International\iDate=%iDate%

Using two similar REG commands makes sure that nosotros volition get the required value, no affair which version of REG.EXE is available.

Date/Time Format Settings
Name Value
s1159 AM or any other designation you lot cull for the morn (ignored if iTime = one)
s2359 PM or any other designation you cull for the afternoon (ignored if iTime = one)
sDate - or / or whatsoever engagement separator yous choose
sTime : or . or whatever time separator y'all cull

The lawmaking to retrieve the value of sDate looks familiar:

:: For REG.EXE 3.0 (Windows XP) and later versions FOR /F "tokens=3" %%A IN ('REG QUERY "HKCU\Control Panel\International" /v sDate 2ˆ>NUL') DO ( 	Set up sDate=%%A ) :: For earlier REG.EXE versions FOR /F "tokens=3" %%A IN ('REG QUERY "HKCU\Control Panel\International\sDate"    2ˆ>NUL') Practise ( 	Set up sDate=%%A ) ECHO HKEY_CURRENT_USER\Command Panel\International\sDate=%sDate%

Now allow's build on what nosotros found so far:

FOR /F "tokens=1-three delims=%sDate%" %%A IN ("%Today%") Practise ( 	IF "%iDate%"=="0" ( 		SET Day=%%B 		SET Month=%%A 		Gear up Year=%%C 	) 	IF "%iDate%"=="one" ( 		SET Day=%%A 		SET Month=%%B 		Ready Year=%%C 	) 	IF "%iDate%"=="2" ( 		SET Day=%%C 		Ready Calendar month=%%B 		SET Twelvemonth=%%A 	) )
Notation: So far, I call back this method and the DEBUG RTC method are the only truthful batch solutions that volition give y'all 100% accuracy (unfortunately, running DEBUG requires a 32-fleck Os and ambassador privileges).
It is possible to screw up the registry settings and intermission this method too, but that situation is highly unlikely!
All other methods assume at to the lowest degree either a known day/month order or a known language.

Parsing time values requires like techniques.
For more than details, take a expect at my various versions of SortDate.bat and SortTime.bat.

Ready

Oftentimes the date format will be known, at least partly.
We may know the order of twenty-four hour period and month, just even then there may be some systems that use leading zeroes, and others that don't.
Or nosotros may be unsure of the separators used.

Note: In large networks it is advisable to use group policies to enforce a single enterprise wide standard date and time format.

Parse the time, ensure leading zeroes:

Fix Now=%Time: =0% Set Hours=%Now:~0,two% SET Minutes=%At present:~3,2% ECHO.%At present% | Observe /I "P" >NUL && Gear up /A Hours += 12

Would neglect for 12 hour clocks that do not use a "P" in their PM designation.

Parse the time, strip leading zeroes:

FOR /F "tokens=1,2 delims=:." %%A IN ("%Time%") DO ( 	Fix /A Hours   = 100%%A %% 100 	Ready /A Minutes = 100%%B %% 100 )

The code above would fail on 12 hr clocks with AM/PM designation appended to the minutes without a space, or on systems that apply a unlike separator (not a dot nor a colon). Actually, this method ignores AM/PM completely.

FOR /F "tokens=ane,two delims=:." %%A IN ("%Time%") Practise ( 	Ready Hours=%%A 	SET Minutes=%%B ) Prepare /A Hours = 100%Hours% %% 100 Repeat.%Minutes% | FIND /I "P" >NUL && SET /A Hours += 12 SET Minutes=%Minutes:~0,two% SET /A Minutes = 100%Minutes% %% 100

A little more code, and a little safer. Even so, the lawmaking above would neglect half the fourth dimension for 12 hour clocks that do non utilize a "P" in their PM designation.

Parse the (United states) date, ensure leading zeroes

SET Today=%Date: =0% SET Year=%Today:~-4% SET Month=%Today:~-x,2% Prepare Day=%Today:~-7,2%
Note: Date parsing method by Ildar Shaimordanov.

The code above would fail if the solar day does non have ii digits.
A quick-and-actually-dirty solution for that problem, not for the faint of heart, could be:

SET Today=%Date: =0% SET Yr=%Today:~-4% :: Include 1 extra character, which will exist either a leading zilch or a trailing separator Fix Calendar month=%Today:~-10,3% :: Remove separator SET Month=%Calendar month:-=% Set Month=%Month:/=% :: Clear leading zeroes SET /A Calendar month = 100%Calendar month% %% 100 :: And add 1 once more, if necessary SET /A Month = 100 + %Month% Prepare Calendar month=%Calendar month:~-2% Gear up Day=%Today:~-7,two% :: Remove separator Prepare Day=%Solar day:-=% Set up 24-hour interval=%Day:/=% :: Articulate leading zeroes, as there may exist 2 leading zeroes Set /A Day = 100%Day% %% 100 :: And add one over again, if necessary Fix /A Solar day = 100 + %Day% Gear up Mean solar day=%Day:~-2%

(I warned you lot information technology would get ugly...)

  • Date & time in NT: the basics
  • Batch techniques to add or remove leading zeroes

folio last modified: 2016-03-sixteen

For F Tokens 1 2 Delims,

Source: https://www.robvanderwoude.com/datetimentparse.php

Posted by: phamrearight.blogspot.com

0 Response to "For F Tokens 1 2 Delims"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel