You are not logged in.

#1 14 May 2017 13:16

flowe
New Member
Registered: 14 May 2017
Posts: 3

date and time format - single digits handling

Hi

I try to backup a databank named say "anything.dbx".

To attain a clear chronological listing of the backups in the
target folder, I have to add date and time to the file name,
such as "anything_date-time.dbx".

The basic coding in a batchfile is not the problem to me,
but there are several problems in the handling of date and time.

Standard Windows print format for date and time are xx.xx.xxxx
and xx:xx:xx:xx, e.g. 08.05.2017 and 12:06:35(:22), and these
should be transformed to a uniform "anything_170508-120635.dbx".

For one thing, colons are not accepted in file names. And the much
bigger problem is the intransparent handling of single digit counts
for hours etc., between 0 resp. 1 and 9 by dear old MS sad .

In batch coding, I'm never sure whether I deal with a string such
as "08" or with a plain numerical variable such as the value 8,
and/or the "0" is silently replaced with a space. 

If I pick the "05" in the above example with
set /a month=%date:~3,2% ,
I might get a string like "05", but in combining such fake strings
to a block of 6, I suddenly only have the plain single value 5 - the
vital cipher "0" gets lost, although the block of 6 digits doubtlessly
IS a string, but often curtailed to 5 or 4 digits. And not working
too is (after an IF function)
"set /a hour=0%time:~1,1%" .

So my question is:
how can I force or work around uniform string handling??

Many thanks for your help smile - flowe

Offline

#2 14 May 2017 16:13

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: date and time format - single digits handling

A common way of dealing with that issue is to prefix the result with an extra 0, so 10 becomes 010 and 9 becomes 09.
then you can just use the last two digits.

There's an example of renaming dates/times on this page:
https://ss64.com/nt/syntax-stampme.html

Offline

#3 14 May 2017 16:54

Aacini
Member
Registered: 05 Dec 2012
Posts: 149

Re: date and time format - single digits handling

Ok. This is my version of an answer.

When you describe date-time formats, you should be very clear. Your "date and time are xx.xx.xxxx and xx:xx:xx:xx" explanation is confusing and force us to compare the "e.g. 08.05.2017 and 12:06:35(:22)" examples given with the current date to conclude that the first part is the day. Why not you just posted "my date format is DD.MM.YYYY? Also, I think the time format given is wrong and should be HH:MM:SS.CC with the centiseconds part separated by point, not colon, although it may also be a comma. It is funny that after your long post we don't know what is the decimal separator character... These points are particularly irritant when compared vs. the lot of unuseful details you include in your question.

This question is as old as this problem, and I am pretty sure that if you would take the effort of google for a solution, you would find hundreds of related answers. However, you posted a new question here, so I post a new answer here as reply:

rem Convert current date in DD.MM.YYYY format and time in HH:MM:SS:CC format
rem to two variables: myDate=1YYMMDD and myTime=1HHMMSS
for /F "tokens=1-6 delims=.:" %%a in ("%date%:%time%") do (
   set /A "myDate=((100+%%c%%100)*100+1%%b%%100)*100+1%%a%%100"
   set /A "myTime=((100+10%%d%%100)*100+1%%e%%100)*100+1%%f%%100"
)

rem To use these variables, remove the first character
echo anything_%myDate:~1%-%myTime:~1%.dbx

Offline

#4 18 May 2017 14:08

flowe
New Member
Registered: 14 May 2017
Posts: 3

Re: date and time format - single digits handling

@Simon
Thanks for your suggestions. Within my coding capability the first of which – prefixing with a cipher zero and then picking the last two - didn't succeed, whatever I tried.  But the link to stampme.cmd solved the problem with ":: Get the date/time", replacing CMD date and time with an entirely different approach with the ready-made string "LocalDateTime" - with all the missing zeros in front of any numbers <10 already there.

The result of your 2 lines works out nearly as intended: YYYY-MM-DD@hh-mm-ss, and the final modification to "my sort of green ink" was easy: YYMMDD-hhmmss – "17" in place of 2017, for the sake of readability of 2 blocks of 6 digits, without any danger of mixup within the next 83 years wink .

Very many thanks again.

@Aacini
Unfortunately, your elaborate code produced a "bracket missing" error and I didn't try to sort it out, as the problem is solved. Regarding your superfluous comments re my superfluous comments only that much: the just mentioned date/time format as YYMMDD-hhmmss is straight out of ISO 8601 and perfectly acceptable for primarily technical purposes as opposed to readability to the general public.

Regards - flowe

Offline

#5 20 May 2017 04:48

Aacini
Member
Registered: 05 Dec 2012
Posts: 149

Re: date and time format - single digits handling

@flowe

My code works correctly. This is the prove:

C:\Users\Antonio\Documents\tests> ver

Microsoft Windows [Versión 6.2.9200]

C:\Users\Antonio\Documents\tests> type test.bat
@echo off
setlocal

rem Convert current date in DD/MM/YYYY format and time in HH:MM:SS.CC format
rem to two variables: myDate=1YYMMDD and myTime=1HHMMSS
for /F "tokens=1-6 delims=/:." %%a in ("%date%:%time%") do (
   set /A "myDate=((100+%%c%%100)*100+1%%b%%100)*100+1%%a%%100"
   set /A "myTime=((100+10%%d%%100)*100+1%%e%%100)*100+1%%f%%100"
)

rem To use these variables, remove the first character
echo anything_%myDate:~1%-%myTime:~1%.dbx

C:\Users\Antonio\Documents\tests> echo %date%
19/05/2017

C:\Users\Antonio\Documents\tests> echo %time%
22:15:00.72

C:\Users\Antonio\Documents\tests> test.bat
anything_170519-221504.dbx

I invite you to compare my "elaborate" code, comprised of just one FOR /F command and two SET /A commands, vs. any other Batch code used for the same purpose and based on %date% and %time% variables (as you requested in the question), that usually have much more lines.

Yes, the output format of the result is ISO 8601, but I asked you about "the time format given" in your "format for date and time are xx.xx.xxxx and xx:xx:xx:xx e.g. 08.05.2017 and 12:06:35(:22)" example, where I am pretty sure that the 22 centiseconds in the time (the ones placed after the 35 and enclosed in parentheses) are NOT separated by a ":" colon...

Antonio

Offline

#6 20 May 2017 20:11

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: date and time format - single digits handling

flowe wrote:

: the just mentioned date/time format as YYMMDD-hhmmss is straight out of ISO 8601 and perfectly acceptable for primarily technical purposes as opposed to readability to the general public.

I don't think this is quite right, every ISO 8601 reference I have seen has used a 4 digit year YYYY

Offline

#7 20 May 2017 23:34

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 205

Re: date and time format - single digits handling

Yeah, ISO 8601 clearly states that all digits of the year need to be displayed.
https://web.archive.org/web/20110614235 … format.htm

Offline

#8 21 May 2017 18:56

flowe
New Member
Registered: 14 May 2017
Posts: 3

Re: date and time format - single digits handling

@Simon, @Shadow Thief

You are right, ISO8601 defines the international standard date notation as YYYY-MM-DD.

According to ss64.com/dates.html, ISO8601 allows alternative formats "for use in applications with special requirements", the most notable of which is to skip the separators with date and time. YYMMDD is not explicitly listed and would not be acceptable in addressing a general public. In my case, internally identifying successive databank backup versions, YYMMDD-hhmmss to me seems more readable than YYYYMMDD-hhmmss. Sorry the issue cropped up at all. o.k. wink ?

Kind regards - flowe

Offline

#9 21 May 2017 20:26

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 205

Re: date and time format - single digits handling

Honestly, it simply depends on if any of your data goes back into the 90s (or might exist for the next 83 years).

Offline

#10 04 Sep 2018 16:31

Carl Ingalls
New Member
Registered: 04 Sep 2018
Posts: 4

Re: date and time format - single digits handling

How can my CMD code discover what format the system is using to present the date (in environment variable DATE)?  I'd like it to work no matter what format the date comes in. 

And thank you very much for such a great website.
Carl Ingalls

Offline

#11 04 Sep 2018 17:53

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: date and time format - single digits handling


cmd | *sh | ruby | chef

Offline

#12 04 Sep 2018 18:09

Carl Ingalls
New Member
Registered: 04 Sep 2018
Posts: 4

Re: date and time format - single digits handling

Thank you bluesxman.  The post you recommended is an excellent one.  I had already studied it, and quite a few others in the SS64 Forum. 

I'd like to stay within CMD as much as possible, and I'd also like to avoid WMIC.  I think I found a solution using REG QUERY, where I can read the date format directly from the Windows Registry. 

I might be in the wrong discussion thread.  I don't have a problem with handling single digits in the date, or with numbers that begin with zero.  Just in detecting the order of the various parts of the date.

Offline

#13 04 Sep 2018 23:04

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: date and time format - single digits handling

Back in the days of Windows XP there were many 'all CMD' solutions for getting the date/time independent of regional settings.

With Windows 7 and Windows 10 you have to use WMIC, VBScript, PowerShell or some other external program. A lot of people have looked at this problem and I think if there was an easy reliable way using only CMD it would have been found by now.

Offline

#14 04 Sep 2018 23:28

Carl Ingalls
New Member
Registered: 04 Sep 2018
Posts: 4

Re: date and time format - single digits handling

Thank you Simon.  I did find a way to read the date and time formats from the registry.  These apply to the output from the time and date commands.  They also apply to the environment variable %date%.  However, the environment variable %time% gets its formatting from somewhere else.  I get differently formatted results from the following two commands:
time /t
echo %time%

The first gives me the formatting as I have customized it.  The second always gives HH:mm:ss.ss (on my machine). 

As you said, a lot of people have been looking at this problem for a long time.  Some of the smartest are on your forum.  I don't think I'll find a better answer elsewhere. 

Thank you again.  ~ Carl

Offline

Board footer

Powered by