Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Yesterday's date in yyyymmdd format -- help

4,024 views
Skip to first unread message

Norm Hickel

unread,
Oct 25, 2002, 10:10:56 PM10/25/02
to
Does anyone have a method for determining yesterday's date in YYYYMMDD
format??? We have web server log files that get generated each day with
yesterday's date and I need to be able to grab them with a script. It would
be nice if I could do it with some type of script that can figure out what
yesterday's date was... accounting for month and year aspects...

Thanks
Norm


Garry Deane

unread,
Oct 25, 2002, 10:57:26 PM10/25/02
to
On Fri, 25 Oct 2002 21:10:56 -0500, "Norm Hickel" <nehi...@yahoo.com>
wrote:

There's plenty of utilitities around that do date math and make the
task very straightforward but if you want a straight batch approach
then try this.

@echo off
setlocal
call :get_date
:: Strip leading zeros from possible octals and decrement the day
set /a mm=1%mm%-100, dd=1%dd%-101
if %dd% NEQ 0 goto :add_zeros
:: Today is the 1st of the month - decrement the month
:: and set leap year check (ignoring centuries)
set /a mm-=1,ly=yy%%4
:: If today is 1 Jan, set date to 31st Dec
if %mm% EQU 0 (set /a dd=31, mm=12, yy-=1) else (
rem Calculate days in last month (by Frank Westlake)
set /a "dd=5546>>mm&1,dd+=30"
rem Special case for February
if %mm% EQU 2 if %ly% EQU 0 (set dd=29) else (set dd=28)
)
:add_zeros
if %dd% LSS 10 set dd=0%dd%
if %mm% LSS 10 set mm=0%mm%
echo yesterday was %yy%%mm%%dd%
goto :eof

:: ------------------------------------------------------------------
:Get_Date
:: ------------------------------------------------------------------
:: Generic date parser
:: Sets %dd% (01-31), %mm% (01-12) & %yy% (4 digit)

if "%date%A" LSS "A" (set toks=1-3) else (set toks=2-4)
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo:^|date') do (
for /f "tokens=%toks% delims=.-/ " %%d in ('date/t') do (
set %%a=%%d
set %%b=%%e
set %%c=%%f
set toks=
)
)
if %yy% LSS 100 set yy=20%yy%
goto :eof

Garry

Ted Davis

unread,
Oct 26, 2002, 11:01:57 AM10/26/02
to
On Fri, 25 Oct 2002 21:10:56 -0500, "Norm Hickel" <nehi...@yahoo.com>
wrote:

>Does anyone have a method for determining yesterday's date in YYYYMMDD

This can be done easily using a language that has a strftime()
function (such as gawk), but it would be easier in batch language just
to get the second newest file in the directory, or better, the oldest
file because you moved all except the current one and the previous one
out of the directory.

for /f "tokens=*" %%a in ('dir /o:-d /b *.log') do set logname=%%a


T.E.D. (tda...@gearbox.maem.umr.edu - e-mail must contain "T.E.D." or my .sig in the body)

Dr John Stockton

unread,
Oct 26, 2002, 11:49:27 AM10/26/02
to
JRS: In article <aOmu9.81$N01....@news.uswest.net>, seen in
news:alt.msdos.batch.nt, Norm Hickel <nehi...@yahoo.com> posted at Fri,
25 Oct 2002 21:10:56 :-

>Does anyone have a method for determining yesterday's date in YYYYMMDD
>format???

With NOWMINUS, via sig line 3;

NOWMINUS D1 F1 J0 R

puts that format on standard output.

I understand that, in NT,

for /f %t in ('NOWMINUS D1 F1 J0 R') do @set XX=%t

would set environment variable XX.

And NOWMINUS D1 F1 J0 Pxx gives SET XX=20021025

which can be redirected to file and executed.

It can make further adjustments, lest you should for example wish to get
yesterday's date any time between 06:30 today & 06:30 tomorrow.

--
© John Stockton, Surrey, UK. j...@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.

Helge Wunderlich

unread,
Oct 27, 2002, 8:12:54 AM10/27/02
to
On Fri, 25 Oct 2002 21:10:56 -0500, "Norm Hickel" <nehi...@yahoo.com>
wrote:

>Does anyone have a method for determining yesterday's date in YYYYMMDD
>format???

Create a file called YESTERDAY.VBS and put these lines into it:

d = DateAdd("d", -1, now)
wscript.echo DatePart("YYYY", d) & _
Right("0" & DatePart("m", d), 2) & _
Right("0" & DatePart("d", d), 2)


Then, put this into your batch file (single line):

for /F %%n in ('cscript.exe //nologo YESTERDAY.VBS') do set
yesterday=%%n

Now you have a variable containing the date in the correct format.

--
Helge Wunderlich
Please remove obvious part of address to send mail

Norm Hickel

unread,
Oct 26, 2002, 9:33:05 PM10/26/02
to
This works GREAT!!!

Thanks
Norm


"Garry Deane" <garrydeane_at_yahoo.com.au> wrote in message
news:3dba0403...@192.168.0.2...

anilsury...@gmail.com

unread,
Jul 2, 2013, 3:06:35 AM7/2/13
to
Hi There,
This works Great !!!. This is what exactly i'm looking for.

foxidrive

unread,
Jul 2, 2013, 3:32:31 AM7/2/13
to
The post you replied to is from 2002. :) Here is a powershell/batch solution.

@echo off
for /f "delims=" %%a in ('powershell get-date((get-date^).addDays(-1^)^) -uformat "%%Y%%m%%d"') do set d8=%%a
echo %d8%
pause



--
foxi

ten.n...@virgin.net

unread,
Jul 2, 2013, 10:27:36 AM7/2/13
to
On Tue, 02 Jul 2013 08:32:31 +0100, foxidrive <n...@this.address.invalid>
wrote:

> The post you replied to is from 2002. :) Here is a powershell/batch
> solution.
>
> @echo off
> for /f "delims=" %%a in ('powershell get-date((get-date^).addDays(-1^)^)
> -uformat "%%Y%%m%%d"') do set d8=%%a
> echo %d8%
> pause
>
>
>

Foxi, I would use this:

::----- START -----
@Echo Off & SetLocal
For /F "Tokens=*" %%# In ('PowerShell -Command "&{Get-Date -f
"yyyyMMdd"}"'
) Do Set "d8=%%#"
Echo/%d8%
>Nul Timeout /T 5
::------ END ------

Is there a reason to choose one over the other (other than the timeout
command which would not be available by default pre-Vista)?

foxidrive

unread,
Jul 2, 2013, 10:52:05 AM7/2/13
to
I snaffled the code from someone else - but the point is that it calculates the day prior to today.


--
foxi

ten.n...@virgin.net

unread,
Jul 2, 2013, 11:17:47 AM7/2/13
to
On Tue, 02 Jul 2013 15:27:36 +0100, <ten.n...@virgin.net> wrote:

> Foxi, I would use this:
>
> ::----- START -----
> @Echo Off & SetLocal
> For /F "Tokens=*" %%# In ('PowerShell -Command "&{Get-Date -f
> "yyyyMMdd"}"'
> ) Do Set "d8=%%#"
> Echo/%d8%
> >Nul Timeout /T 5
> ::------ END ------
>
> Is there a reason to choose one over the other (other than the timeout
> command which would not be available by default pre-Vista)?
>

Scratch that, mine gives todays date whereas your's gives what I've just read was asked for, yesterdays date! :(

::----- START -----
@Echo Off & SetLocal
For /F "Tokens=*" %%# In (
'PowerShell -Command "&{((Get-Date).AddDays(-1)).ToString('yyyyMMdd')}"'
) Do Set "d8=%%#"
Echo=%d8%

Stanley Daniel de Liver

unread,
Jul 3, 2013, 11:57:49 AM7/3/13
to
Well, OK, though none of the above are pure Batch, things have moved on.
(yes Dr S?)

of course it might be of interest as to why the OP wanted "yesterday's
date" - there may be other ways available, (e.g. if we're "talking" about
old files)

--
It's a money /life balance.

Dr J R Stockton

unread,
Jul 3, 2013, 11:10:13 AM7/3/13
to
In alt.msdos.batch.nt message <08fd750e-43d0-419c-86cf-e1f329ca0a56@goog
legroups.com>, Tue, 2 Jul 2013 00:06:35, anilsury...@gmail.com
posted:
More simply, calculate Y*10000 + m*100 + d.

--
(c) John Stockton, nr London, UK. E-mail, see Home Page. Turnpike v6.05.
Website <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.

mokomoji

unread,
Jul 8, 2013, 9:38:45 AM7/8/13
to
2002년 10월 26일 토요일 오전 11시 22분 3초 UTC+9, Norm Hickel 님의 말:
> Does anyone have a method for determining yesterday's date in YYYYMMDD format??? We have web server log files that get generated each day with yesterday's date and I need to be able to grab them with a script. It would be nice if I could do it with some type of script that can figure out what yesterday's date was... accounting for month and year aspects...ThanksNorm

windows date
http://blog.naver.com/mokomoji/130159916842
windows xp date
http://blog.naver.com/mokomoji/130117861406
windows nt server
forfiles command use

Stanley Daniel de Liver

unread,
Jul 8, 2013, 10:46:33 AM7/8/13
to
Sure, it can be done by checking day-1 for month-1 etc, but it gets
awkward with leap days.

Philip Robyn

unread,
Jul 8, 2013, 8:21:54 PM7/8/13
to
>
> Sure, it can be done by checking day-1 for month-1 etc, but it gets
>
> awkward with leap days.
>

I had originally read this thread a couple of days ago and the part about there not being a 'pure batch' solution stuck in the back of my mind. So this afternoon I was working on a date-related project of my own and I came across something that I've had around for the past eleven years or so. I can't claim credit for the heart of this batch file, and now it's been so long that I can't remember where I got it from. So if any of the old timers recognize this, credit goes to whomever.

=====begin C:\cmd\test\datemath.cmd ====================
01. @echo off
02. ::
03. :: Syntax: %~n0 [ yyyymmdd OR today ] [+ OR -]nnnnn [.]
04. ::
05. :: where 'nnnnn' is the number of days to add or subtract
06. ::
07. :: Examples: %~n0 20010501 +234 .
08. :: %~n0 today -12345
09. ::
10. :: Note that there is NO SPACE between the plus or minus sign and the
11. :: number of days.
12. ::
13. :: The result is returned in environment variable %~n0 in yyyymmdd format.
14. ::
15. :: A third (optional) argument of '.' will display the result on screen.
16. ::
17. echo .?./?.-?.help.. | findstr /i ".%1." > nul
18. if %errorlevel% EQU 0 (
19. call XHELP "%~f0"
20. goto :EOF
21. )
22.
23. set mydate=%1
24. if /i "%mydate%" NEQ "TODAY" goto :YMD
25.
26. set mydate=%date:~4%
27. set mydate=%mydate:/=%
28. set mydate=%mydate:~4,4%%mydate:~0,4%
29.
30. :YMD
31.
32. set M=%mydate:~4,2%
33. set D=%mydate:~6,2%
34. set Y=%mydate:~0,4%
35. set TD=%D%X
36. set TM=%M%X
37. if %TD:~1,1%==X ( set D=10%D% ) else ( set D=1%D% )
38. if %TM:~1,1%==X ( set M=10%M% ) else ( set M=1%M% )
39. set /a D-=100,M-=100
40.
41. set operation=%2
42.
43. :: Generate a Magic number from %D% %M% %Y%
44. set /a X=(1461*(Y+4800+(M-14)/12))/4+(367*(M-2-12*^
45. ((M-14)/12)))/12-(3*((Y+4900+(M-14)/12)/100))/4+D-32075
46.
47. :: Do date arithmetic here, eg 'set /a X+=30'
48.
49. set /a X = X %operation%
50.
51. :: Convert Magic number back to a date
52. set /a L=X+68569
53. set /a N=(4*L)/146097
54. set /a L=L-(146097*N+3)/4
55. set /a K=(4000*(L+1))/1461001
56. set /a L=L-(1461*K)/4+31
57. set /a J=(80*L)/2447
58. set /a D=L-(2447*J)/80
59. set /a L=J/11
60. set /a M=J+2-(12*L)
61. set /a Y=100*(N-49)+K+L
62.
63. set /a D+=100,M+=100
64.
65. if [%3]==[.] echo %Y% %M:~1% %D:~1%
66. endlocal&set %~n0=%Y%%M:~1%%D:~1%&goto :EOF
67.
=====end C:\cmd\test\datemath.cmd ====================

Here's XHELP as called by datemath:

=====begin C:\cmd\test\xhelp.cmd ====================
01. @echo off
02. ::
03. :: Displays the contents of the 'flowerbox' in a CMD or BAT
04. :: file and the directory information for the file.
05. ::
06. :: '%~n0' will be displayed as the name of the referenced file
07. ::
08. echo .?./?.-?.help.. | findstr /i ".%1." > nul
09. if %errorlevel% EQU 0 (
10. call XHELP "%~f0"
11. goto :EOF
12. )
13. setlocal enabledelayedexpansion
14. set whichfile=%~1
15. for /f "tokens=*" %%a in (
16. 'dir %whichfile% ^| find "/"'
17. ) do set direntry=%%a
18. set fname=%~n1
19. echo :
20. echo : %direntry:~0,38% %whichfile%
21. echo :
22. for /f "tokens=* delims=" %%* in (
23. 'findstr /b /c:"::" %whichfile%') do call :display %%*
24. endlocal& goto :EOF
25. :display
26. set line=%*
27. set line=%line:~1%
28. set line=!line:^%~n0=%fname%!
29. echo %line%
30. goto :EOF
=====end C:\cmd\test\xhelp.cmd ====================

Phil Robyn
ude tod yelekreb ta nyborp


Todd Vargo

unread,
Jul 8, 2013, 11:44:42 PM7/8/13
to
On 7/8/2013 8:21 PM, Philip Robyn wrote:
>>
>> Sure, it can be done by checking day-1 for month-1 etc, but it gets
>>
>> awkward with leap days.
>>
>
> I had originally read this thread a couple of days ago and the part about there not being a 'pure batch' solution stuck in the back of my mind. So this afternoon I was working on a date-related project of my own and I came across something that I've had around for the past eleven years or so. I can't claim credit for the heart of this batch file, and now it's been so long that I can't remember where I got it from. So if any of the old timers recognize this, credit goes to whomever.
>
> =====begin C:\cmd\test\datemath.cmd ====================

Does this help you remember?

https://groups.google.com/forum/#!topic/alt.msdos.batch.nt/ElDnskuOy9I

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

Philip Robyn

unread,
Jul 9, 2013, 12:18:01 AM7/9/13
to
Way to go, Todd! Turns out I almost duplicated my post of how many years ago??
(We must be at the same place now in the 11-year sunspot cycle.) Thanks for the reminder about Richie's excellent stuff.

Phil Robyn

mokomoji

unread,
Jul 10, 2013, 12:50:40 PM7/10/13
to
2002년 10월 26일 토요일 오전 11시 22분 3초 UTC+9, Norm Hickel 님의 말:
> Does anyone have a method for determining yesterday's date in YYYYMMDD format??? We have web server log files that get generated each day with yesterday's date and I need to be able to grab them with a script. It would be nice if I could do it with some type of script that can figure out what yesterday's date was... accounting for month and year aspects...ThanksNorm

2mon 29day -1mons? = 1mon 29day ok...
7mon 31day -1mons? =??? 6mon 31day ??????????

1mons = 28,29,30,31

Dr J R Stockton

unread,
Jul 10, 2013, 9:15:09 AM7/10/13
to
In alt.msdos.batch.nt message <rOBAt.5578$7x5....@newsfe02.iad>, Wed, 3
Jul 2013 00:52:05, foxidrive <n...@this.address.invalid> posted:

>
>I snaffled the code from someone else - but the point is that it calculates the day prior to today.
>

One should consider the circumstances. When should the date of
yesterday change? Only a pedant would disregard the possibility of
changes at other than 24:00.

Some transport authorities consider a date to run from 03:00 to 27:00
local time.

Assiduous people with a deadline to meet may work in the evening,
sometimes past midnight.

Then what about world-wide organisations? For those working in New
Zealand, it is almost always yesterday in Hawaii.

foxidrive

unread,
Jul 10, 2013, 8:59:12 PM7/10/13
to
On 10/07/2013 23:15, Dr J R Stockton wrote:
> In alt.msdos.batch.nt message <rOBAt.5578$7x5....@newsfe02.iad>, Wed, 3
> Jul 2013 00:52:05, foxidrive <n...@this.address.invalid> posted:
>
>>
>> I snaffled the code from someone else - but the point is that it calculates the day prior to today.
>>
>
> One should consider the circumstances. When should the date of
> yesterday change?

Without information to the contrary then 2400 is the switchover point in local time.

> Only a pedant would disregard the possibility of
> changes at other than 24:00.
>
> Some transport authorities consider a date to run from 03:00 to 27:00
> local time.
>
> Assiduous people with a deadline to meet may work in the evening,
> sometimes past midnight.
>
> Then what about world-wide organisations? For those working in New
> Zealand, it is almost always yesterday in Hawaii.

Anyone asking for a script where their date requirements are out of the ordinary would be expected to say
what they need - they would be quite aware of their circumstances.

Regarding the time, did you see that caesium clocks are going to be replaced with more accurate ones?
1 second in 100,000 years doesn't cut the mustard. :)


--
foxi

Dr J R Stockton

unread,
Jul 13, 2013, 11:15:21 AM7/13/13
to
In alt.msdos.batch.nt message <CrnDt.37244$ny.1...@fx14.iad>, Thu, 11
Jul 2013 10:59:12, foxidrive <n...@this.address.invalid> posted:

>On 10/07/2013 23:15, Dr J R Stockton wrote:
>> In alt.msdos.batch.nt message <rOBAt.5578$7x5....@newsfe02.iad>, Wed, 3
>> Jul 2013 00:52:05, foxidrive <n...@this.address.invalid> posted:
>>
>>>
>>> I snaffled the code from someone else - but the point is that it
>>>calculates the day prior to today.
>>>
>>
>> One should consider the circumstances. When should the date of
>> yesterday change?
>
>Without information to the contrary then 2400 is the switchover point
>in local time.

People often do not think enough before specifying or acting. For
example, an E-mail system that I use drops all connections overnight.
This happens at midnight, when I may well be still working. For me,
06:00 would be far better; for general use, 03:00 would be fine;
preferably, though, it would be user-configurable.


>> Only a pedant would disregard the possibility of
>> changes at other than 24:00.
>>
>> Some transport authorities consider a date to run from 03:00 to 27:00
>> local time.
>>
>> Assiduous people with a deadline to meet may work in the evening,
>> sometimes past midnight.
>>
>> Then what about world-wide organisations? For those working in New
>> Zealand, it is almost always yesterday in Hawaii.
>
>Anyone asking for a script where their date requirements are out of the
>ordinary would be expected to say
>what they need - they would be quite aware of their circumstances.

There is a word for people like you : "optimist".

Remember that code, including scripts, is commonly written for, and not
by, the user(s).


>Regarding the time, did you see that caesium clocks are going to be
>replaced with more accurate ones?

Where do you live, that you spell caesium correctly?

No. I saw that there has been some success with an apparently more
accurate system. One should only use "going to" for this after a formal
decision has been taken by the appropriate *international* body. And
between now and then, an even better method might be found.

See
<http://www.nature.com/news/precise-atomic-clock-may-redefine-time-
1.13363>.

FYI, I just checked, I have in my possession at least three second-hand
cardboard boxes addressed to (at the time) a senior member of the
section responsible for working with such things in the UK. I think I
recently had another addressed to the then head of that section; it may
be downstairs.
0 new messages