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

Changing date output format to yyyy.mm.dd.ddd?

1,438 views
Skip to first unread message

Fairfax

unread,
Nov 6, 2012, 6:06:15 AM11/6/12
to
Good Morning!

I have a batch file that works very well in creating a backup copy:

copy "F:\TIMER-WORK\Time Worked Calculator\APP- TWCalc\twc.ses"
"F:\TIMER-WORK\Time Worked Calculator\APP- TWCalc\zBKP- twc.ses -
%DATE%., %TIME:~0,2%h%TIME:~3,2%m%TIME:~6,2%s.ses"

Because my regional settings at home work, I get the correct output of
part of the date, in yyyy.mm.dd.(day missing).

At work (also with WinXP), I edit the regional settings but they don't
stick.

How can I change the %DATE% above so that it works to give me
yyyy.mm.dd.ddd?

From this page here,
http://blueonionsoftware.com/blog.aspx?p=40656a9d-021b-4061-b296-36ad5211f4b2,
I tried this:

%DATE:~-4%.%DATE:~4,2%.%DATE:~7,2%.%DATE:~0,3%

but I got gobbledook in the output file's name for the date.
Supposedly the above breaks the date down into yyyy.mm.dd.ddd but I
get this type of mangled output:

zBKP- twc.ses - 1.06..1..0.201, 6h05m22s.ses

instead of this, which %DATE% gives which I have to then manually edit
each time:

zBKP- twc.ses - 2012.11.06., 6h05m27s.ses

TIA!

Cheers.

foxidrive

unread,
Nov 6, 2012, 6:18:04 AM11/6/12
to
On 06/11/2012 22:06, Fairfax wrote:
> Good Morning!
>
> I have a batch file that works very well in creating a backup copy:
>
> copy "F:\TIMER-WORK\Time Worked Calculator\APP- TWCalc\twc.ses"
> "F:\TIMER-WORK\Time Worked Calculator\APP- TWCalc\zBKP- twc.ses -
> %DATE%., %TIME:~0,2%h%TIME:~3,2%m%TIME:~6,2%s.ses"
>
> Because my regional settings at home work, I get the correct output of
> part of the date, in yyyy.mm.dd.(day missing).
>
> At work (also with WinXP), I edit the regional settings but they don't
> stick.
>
> How can I change the %DATE% above so that it works to give me
> yyyy.mm.dd.ddd?

Here is a non-region dependent way of getting a time stamp using Wmic.

:: timestamp YYYYMMDD_HHMMSS and YYYY-MM-DD_HH-MM-SS
@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
echo %dt:~0,8%_%dt:~8,6%
pause

@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
echo %dt:~0,4%-%dt:~4,2%-%dt:~6,2%_%dt:~8,2%-%dt:~10,2%-%dt:~12,2%
pause


--
foxi

foxidrive

unread,
Nov 6, 2012, 7:03:33 AM11/6/12
to
On 06/11/2012 22:18, foxidrive wrote:
> On 06/11/2012 22:06, Fairfax wrote:
>> Good Morning!
>>
>> I have a batch file that works very well in creating a backup copy:
>>
>> copy "F:\TIMER-WORK\Time Worked Calculator\APP- TWCalc\twc.ses"
>> "F:\TIMER-WORK\Time Worked Calculator\APP- TWCalc\zBKP- twc.ses -
>> %DATE%., %TIME:~0,2%h%TIME:~3,2%m%TIME:~6,2%s.ses"
>>
>> Because my regional settings at home work, I get the correct output of
>> part of the date, in yyyy.mm.dd.(day missing).
>>
>> At work (also with WinXP), I edit the regional settings but they don't
>> stick.
>>
>> How can I change the %DATE% above so that it works to give me
>> yyyy.mm.dd.ddd?
>
> Here is a non-region dependent way of getting a time stamp using Wmic.

I note now that you want the day too. Try this:


:: date time using WMIC
:: XP Pro and higher
@echo off
for /f "delims=" %%a in ('Wmic Path Win32_LocalTime Get /value ^|Find "="') do (
for /f "tokens=1,* delims==" %%b in ('cmd /c echo %%a') do set "%%b=00%%c")

set DayOfWeek=%DayOfWeek:~2%
set Quarter=%Quarter:~2%
set WeekInMonth=%WeekInMonth:~2%
set Day=%Day:~-2%
set Hour=%Hour:~-2%
set Minute=%Minute:~-2%
set Month=%Month:~-2%
set Second=%Second:~-2%
set Yr=%Year:~4%
set Year=%Year:~2%

if %dayofweek%==1 set ddd=Mon
if %dayofweek%==2 set ddd=Tue
if %dayofweek%==3 set ddd=Wed
if %dayofweek%==4 set ddd=Thu
if %dayofweek%==5 set ddd=Fri
if %dayofweek%==6 set ddd=Sat
if %dayofweek%==7 set ddd=Sun

set stamp=%year%-%month%-%day%-%ddd%_%hour%-%minute%-%second%

echo %%stamp%% is set to %stamp% (yyyy-mm-dd-ddd_hh-mm-ss)

pause





--
foxi

Tom Lavedas

unread,
Nov 6, 2012, 9:08:07 AM11/6/12
to
On Tuesday, November 6, 2012 7:04:04 AM UTC-5, foxidrive wrote:
> On 06/11/2012 22:18, foxidrive wrote:
> On 06/11/2012 22:06, Fairfax wrote:

I believe I've posted this several times before, but here we go again.

I prefer using VBS for date manipulations, since it tends to take fewer steps. For example, the following little hybrid batch/VBS approach generalizes the use of snippets of VBS code ...

@echo off
if [%2]==[] %0 Result=%1
echo wsh.Echo eval("%~2") > "%temp%\_tmp.vbs"
for /F "delims=" %%a in (
'cscript //nologo "%temp%\_tmp.vbs"') do set %1=%%a
del %temp%\_tmp.vbs

It could be used to return the day of the week text thus ...

call runvbs dow="WeekdayName(weekday(date),true)"
echo Day of the week is %dow%

Of course, similar lines could be used to do the rest of the job, but your original solution using WMIC was every bit as good. ;-)
_______________________________
Tom Lavedas

foxidrive

unread,
Nov 6, 2012, 9:46:21 AM11/6/12
to
On 07/11/2012 01:08, Tom Lavedas wrote:

> I prefer using VBS for date manipulations, since it tends to take fewer steps. For example, the following little hybrid batch/VBS approach generalizes the use of snippets of VBS code ...
>
> @echo off
> if [%2]==[] %0 Result=%1
> echo wsh.Echo eval("%~2") > "%temp%\_tmp.vbs"
> for /F "delims=" %%a in (
> 'cscript //nologo "%temp%\_tmp.vbs"') do set %1=%%a
> del %temp%\_tmp.vbs
>
> It could be used to return the day of the week text thus ...
>
> call runvbs dow="WeekdayName(weekday(date),true)"
> echo Day of the week is %dow%


That's a neat little tool, Tom, I can see the uses it can be handy for. Unfortunately VBS doesn't flow
off the tongue for me - I always meant to get a little more acquainted with it but I still mostly hack
around with other peoples solutions.

Here's the VBS solution for date/time stamps based on Todd's work.
If the OP is using XP Home then he won't have Wmic by default - so I'll add it here.



:: date time using WSH
:: datetime.bat V4
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: This uses Windows Scripting Host to set variables
:: to the current date/time/day/day_number
:: for Win9x/ME/NT/W2K/XP etc
:: Thanks go to Todd Vargo for his scripting
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
set TmpFile="%temp%.\tmp.vbs"
echo> %TmpFile% n=Now
echo>>%TmpFile% With WScript
echo>>%TmpFile% .Echo "set year=" + CStr(Year(n))
echo>>%TmpFile% .Echo "set yr=" + Right(Year(n),2)
echo>>%TmpFile% .Echo "set month="+ Right(100+Month(n),2)
echo>>%TmpFile% .Echo "set day=" + Right(100+Day(n),2)
echo>>%TmpFile% .Echo "set hour=" + Right(100+Hour(n),2)
echo>>%TmpFile% .Echo "set min=" + Right(100+Minute(n),2)
echo>>%TmpFile% .Echo "set sec=" + Right(100+Second(n),2)
echo>>%TmpFile% .Echo "set dow=" + WeekDayName(Weekday(n),1)
echo>>%TmpFile% .Echo "set dow2=" + WeekDayName(Weekday(n))
echo>>%TmpFile% .Echo "set iso=" + CStr(1 + Int(n-2) mod 7)
echo>>%TmpFile% .Echo "set iso2=" + CStr(Weekday(n,2))
echo>>%TmpFile% End With
cscript //nologo "%temp%.\tmp.vbs" > "%temp%.\tmp.bat"
call "%temp%.\tmp.bat"
del "%temp%.\tmp.bat"
del %TmpFile%
set TmpFile=
set stamp=%year%-%month%-%day%_%hour%.%min%.%sec%


echo The year (YYyy) is "%year%"
echo The year (yy) is "%yr%"
echo The month is "%month%"
echo The day (%dow%) is "%day%"
echo The full weekday name is "%dow2%"
echo.
echo ISO 8601 Day-Of-Week number is "%iso%"
echo.
echo The hour is "%hour%"
echo The minute is "%min%"
echo The second is "%sec%"
echo.

echo The date and time stamp is "%stamp%"
echo.
echo time (hhmmss) (%hour%%min%%sec%)
echo.
echo date A (yyyymmdd) (%year%%month%%day%)
echo date B (mmddyyyy) (%month%%day%%year%)
echo date C (ddmmyyyy) (%day%%month%%year%)
echo.
echo date D [yymmdd] [%yr%%month%%day%]
echo date E [mmddyy] [%month%%day%%yr%]
echo date F [ddmmyy] [%day%%month%%yr%]
:: datetime.bat
::::::::::::::::::::::::::::::::::::::::::::::::::::::::



--
foxi

Tim Meddick

unread,
Nov 6, 2012, 11:31:57 AM11/6/12
to
Forgive me; but I always try to get a handle on the bigger picture :- It
could be as simple as you are setting the date/time format in Regional
Settings, but then, perhaps, you are not refreshing the [cmd.exe] window.
That is; closing all instances of [cmd.exe] and re-opening another *after*
Regional Settings has been dismissed.

When you [re]set the short date:

"Control Panel" > "Regional and Language Options" >

"Regional Options" (Tab) > "Customize" (Button) >

"Date" (Tab) > "Short Date" (Area)

...if you choose : yyyy-MM-dd
...(with "Date Seperator" set to : "-" ) the %DATE% variable (in a Command
Prompt window) will look like : 2012-11-06

...Setting it to : yyyy-MMM-dd
...and the %DATE% variable will look like : 2012-Nov-06

*NB you do not have to stick to the options in the drop-down box of the
Date Format setting - it also accepts keyboard-typed-input.

You will need to change the "Date Seperator" setting (to; "-"), as other
characters (i.e.; "/") are not helpful when using the %DATE% variable for
naming files.

However, the "Time" format setting in Regional Options has *no* effect on
the format of the %TIME% variable, and will always remain: HH:MM:SS.UU or
16:01:37.66

==

Cheers, Tim Meddick, Peckham, London. :-)


P.S. The above settings (short) "Date" and "Time", will *both* always
effect the output of the Command Prompt's "Time" and "Date" commands :

C:\>time /t
04:18pm

C:\>date /t
2012-11-06

...whereas, it is only the %TIME% variable which is unaffected by any
modified settings.

"Fairfax" <Spa...@NoJunkMail.org> wrote in message
news:3hrh98tmltlt5qdek...@4ax.com...

Fairfax

unread,
Nov 7, 2012, 12:21:28 AM11/7/12
to
Hi, there.

I have never had any trouble changing date/time in Regional Settings
in any computer I've ever used, except at this new job contract. It
won't take. Since registry and other items are locked from user
editing, I'm assuming that that's what they've done here. Since I was
recently talking with an IT guy who said they'd had trouble with the
date entry in their databases and the endless problems that had
created (um, have none of these guys ever hear of masking?????!!!!),
I'm assuming that they locked this down.

And since my USB flash drive can't be used to launch apps from so
can't use my usual backup scripts using Autoit, I have to do a batch
file workaround.

Thanks.


On Tue, 6 Nov 2012 16:31:57 -0000, "Tim Meddick" <timme...@o2.co.uk>
wrote:

Fairfax

unread,
Nov 7, 2012, 12:24:08 AM11/7/12
to
Hey, thanks!

Darn, was hoping it would be more straightforward. I'm only a power
user and have never found batch file scripting easy, unlike other
scripting languages, I'll try to see how to incorporate all this
massive code into my copy bat string as posted above. <sigh> I might
have to live with my simple code above and keep manually correcting
date display in input, I foresee <lol>.

<sigh> Sometimes, wish I could just inject, or whatever, the
scripting gene as unlike other things, scripting doesn't come easy to
me in some languages (like batch and vbs!).

Cheers.

Fairfax

unread,
Nov 7, 2012, 12:25:36 AM11/7/12
to
On Wed, 07 Nov 2012 00:24:08 -0500, Fairfax <Spa...@NoJunkMail.org>
wrote:

[snip]

>have to live with my simple code above and keep manually correcting
>date display in input, I foresee <lol>.

[snip]

Sorry, that should read "... date display in OUTPUT, I foresee ...".
<g>

foxidrive

unread,
Nov 7, 2012, 12:34:58 AM11/7/12
to
On 07/11/2012 16:24, Fairfax wrote:

>>>> How can I change the %DATE% above so that it works to give me
>>>> yyyy.mm.dd.ddd?
>>>
>> :: date time using WMIC
>> :: XP Pro and higher
>> @echo off
>> for /f "delims=" %%a in ('Wmic Path Win32_LocalTime Get /value ^|Find "="') do (
>> for /f "tokens=1,* delims==" %%b in ('cmd /c echo %%a') do set "%%b=00%%c")
>>
>> set DayOfWeek=%DayOfWeek:~2%
>> set Quarter=%Quarter:~2%
>> set WeekInMonth=%WeekInMonth:~2%
>> set Day=%Day:~-2%
>> set Hour=%Hour:~-2%
>> set Minute=%Minute:~-2%
>> set Month=%Month:~-2%
>> set Second=%Second:~-2%
>> set Yr=%Year:~4%
>> set Year=%Year:~2%
>>
>> if %dayofweek%==1 set ddd=Mon
>> if %dayofweek%==2 set ddd=Tue
>> if %dayofweek%==3 set ddd=Wed
>> if %dayofweek%==4 set ddd=Thu
>> if %dayofweek%==5 set ddd=Fri
>> if %dayofweek%==6 set ddd=Sat
>> if %dayofweek%==7 set ddd=Sun
>>
>> set stamp=%year%-%month%-%day%-%ddd%_%hour%-%minute%-%second%
>>
>> echo %%stamp%% is set to %stamp% (yyyy-mm-dd-ddd_hh-mm-ss)

> Darn, was hoping it would be more straightforward. I'm only a power
> user and have never found batch file scripting easy, unlike other
> scripting languages, I'll try to see how to incorporate all this
> massive code into my copy bat string as posted above.

Firstly, as your machine is locked down the admin might also have locked wmic and WSH.
Can you run the batch file above and get a meaningful stamp in the last line?

If so then you can use this as the last line, and replace %date% with %stamp% in your code (which you can
add following this line).


set stamp=%year%.%month%.%day%.%ddd%





--
foxi

billious

unread,
Nov 7, 2012, 9:44:48 AM11/7/12
to
You seem to be very carefully avoiding telling us the critical matter -
what is the format of %DATE% on the machine in question?

Note that it IS possible that the format of %DATE% and %TIME% used could
contain separators such as "/", "-", "." "," ":" and possibly symbols
such as "am" AND the various elements may be leading-zero-suppressed.

If the dayname isn't displayed and you really want it, then it CAN be
derived with a GREAT DEAL of code if VBS, etc. is not available.

Tom Lavedas

unread,
Nov 7, 2012, 10:43:10 AM11/7/12
to
I thought that was foxi's point (first response) in resorting to WMIC to gain access to a date-time string independent or the locale formatting.
_____________________________
Tom Lavedas

billious

unread,
Nov 8, 2012, 1:27:23 AM11/8/12
to
True, but the same correspondent's last response includes

"
Firstly, as your machine is locked down the admin might also have locked
wmic and WSH.
Can you run the batch file above and get a meaningful stamp in the last
line?
"

So - if only we had the format of %date% and %time% on the machine in
question rather than what the result was after executing the faulty code
that OP is using, we may be able to derive a method that did not rely on
facilities that may not be available like WMIC and VBS - without
producing "massive code."

I'd normally favour a locale-independent solution, but is it really
warranted in the current application?

Fairfax

unread,
Nov 8, 2012, 6:21:27 AM11/8/12
to
I can run the batch file, I just don't get good results.

>If so then you can use this as the last line, and replace %date% with %stamp% in your code (which you can
>add following this line).
>
>
>set stamp=%year%.%month%.%day%.%ddd%

Kewl. Will give it a try. Thanks!

Fairfax

unread,
Nov 8, 2012, 6:24:51 AM11/8/12
to
Carefully avoiding <lol>, no. Just not going off into a different
tangent. The date format is simply mm-dd-yyyy which doesn't give you
ordered results when you view in detailed mode. That's why I prefer
yyyy.mm.dd.ddd and the periods are just my preference as the easiest
way to view dates. But, as I mentioned, they don't allow changing of
the regional settings. I'm guessing because of the many databases
they have and a comment about the major date formatting problems they
were having.

I mean ... seriously ... have the database developers never heard of
masking to force date entry to conform to a specific format???!!! I
sometimes wonder at the experts <g>.

>Note that it IS possible that the format of %DATE% and %TIME% used could
>contain separators such as "/", "-", "." "," ":" and possibly symbols
>such as "am" AND the various elements may be leading-zero-suppressed.
>
>If the dayname isn't displayed and you really want it, then it CAN be
>derived with a GREAT DEAL of code if VBS, etc. is not available.

I prefer batch, actually. VBS seems to be locked down in some places
wherease batch seems to run quite freely.

Thanks!

foxidrive

unread,
Nov 8, 2012, 6:37:07 AM11/8/12
to
On 08/11/2012 22:21, Fairfax wrote:

>>>> echo %%stamp%% is set to %stamp% (yyyy-mm-dd-ddd_hh-mm-ss)
>>
>>
>> Firstly, as your machine is locked down the admin might also have locked wmic and WSH.
>> Can you run the batch file above and get a meaningful stamp in the last line?
>
> I can run the batch file, I just don't get good results.

What result do you get when you run the batch that I snipped above?
You will get this format if WMIC is not locked down.

%stamp% is set to 2012-11-08-Thu_22-35-01 (yyyy-mm-dd-ddd_hh-mm-ss)

>> If so then you can use this as the last line, and replace %date% with %stamp% in your code (which you can
>> add following this line).
>>
>> set stamp=%year%.%month%.%day%.%ddd%
>
> Kewl. Will give it a try. Thanks!

Let us know how it goes - there is always powershell which might/might not be locked down.



--
foxi

billious

unread,
Nov 8, 2012, 9:37:28 AM11/8/12
to
Well, despite your scoffing, you still have failed to post the format in
which %date% appears on the machine in question.

What does

ECHO +%date%+

show?

I had to deal with a locked-down system where the mainframe ERP was set
to use the local dd/mm/yy convention but the microcomputer system was
locked to mm-dd-yy. The company lost a great deal of money where people
failed to convert or double-converted when switching between systems.
The managers couldn't care less about the losses - THEY didn't own the
company. AND they'd paid through the nose for the computer systems too -
but no matter because THEY didn't own the company. A five-minute task,
and probably millions lost in consequence - but not out of the managers'
pockets...

So, to return to the data you HAVE presented, and working backwards, it
would appear that your code

%DATE:~-4%.%DATE:~4,2%.%DATE:~7,2%.%DATE:~0,3%

produced the result

zBKP- twc.ses - 1.06..1..0.201, 6h05m22s.ses

which would mean that the last 4 characters of %date% are "1.06"
the FIFTH to SIXTH are ".1"
the EIGHTH to NINTH are ".0"
The first THREE are "201"
(batch counts from character #0, people from character #1)

Therefore the format of %date% must have been

201x.1x.0
but ending in "1.06"

Now I'd guess in the absence of the requested data but in the presence
of 40+ years' experience that the %date% being produced by the system in
question is

2012.11.06

so the magic string required is

%date%



Todd Vargo

unread,
Nov 8, 2012, 11:12:26 AM11/8/12
to
On 11/8/2012 6:24 AM, Fairfax wrote:
> On Wed, 07 Nov 2012 22:44:48 +0800, billious

>> You seem to be very carefully avoiding telling us the critical matter -
>> what is the format of %DATE% on the machine in question?
>
> Carefully avoiding <lol>, no. Just not going off into a different
> tangent. The date format is simply mm-dd-yyyy which doesn't give you
> ordered results when you view in detailed mode. That's why I prefer
> yyyy.mm.dd.ddd and the periods are just my preference as the easiest
> way to view dates. But, as I mentioned, they don't allow changing of
> the regional settings. I'm guessing because of the many databases
> they have and a comment about the major date formatting problems they
> were having.
>
> I mean ... seriously ... have the database developers never heard of
> masking to force date entry to conform to a specific format???!!! I
> sometimes wonder at the experts <g>.
>
>> Note that it IS possible that the format of %DATE% and %TIME% used could
>> contain separators such as "/", "-", "." "," ":" and possibly symbols
>> such as "am" AND the various elements may be leading-zero-suppressed.
>>
>> If the dayname isn't displayed and you really want it, then it CAN be
>> derived with a GREAT DEAL of code if VBS, etc. is not available.
>
> I prefer batch, actually. VBS seems to be locked down in some places
> wherease batch seems to run quite freely.

Regional settings have nothing to do with databases or the way files are
sorted by name. With batch, you don't need to modify regional settings
at the OS level, as you are currently doing. There are several ways to
extract the raw date/time from the system, you just have to be open to
exploring them.

So the date format is simply mm-dd-yyyy without the ddd? As billious
mentioned, ddd can be extracted from mm-dd-yyyy with extra batch code.
However, if the format is actually, "ddd mm-dd-yyyy" that would
eliminate the need for calculating ddd. Hence, you can see the reason
you have been asked to state the actual format of %date%.

However, since yyyy.mm.dd lists by name in the chronological order as
you stated, the ddd part really has no significance to listing the
files. So why include ddd at all?

Your original post only mentioned your home computer and at work, which
lead us to believe that other places were irrelevant. Are now indicating
that a broader range of unknown systems are involved too?

Is VBS or WMIC actually locked down at work? Or are you only repeating
the comment from foxi, because you are unfamiliar with using them?

What about the WMIC code posted by foxi? Did you try using it? Did it
work or fail? You stated, "I can run the batch file, I just don't get
good results." What results did you get? It is not possible to advise
when details are withheld.

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