Home / Microsoft / Microsoft DOS / Day of the Week
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] 2  All - (Bottom) Print
Author Topic: Day of the Week  (Read 1167 times)
twosides
Topic Starter
Rookie



Posts: 11


« on: March 15, 2009, 09:07:06 AM »

Does anyone know how to successfully return the day of the week?

I have tried a quick search on this forum but not got what I want.

I am running a UK date machine.


Cheers.
IP logged
BatchRocks
Hopeful



Thanked: 3
Posts: 326




« Reply #1 on: March 15, 2009, 09:22:26 AM »

JUST the day of the week?

Code: [Select]
echo %date%
That gives the Day, Month, Date, and Year.
IP logged
Dias de verano
Guest
« Reply #2 on: March 15, 2009, 10:17:38 AM »

Do you mean Sunday, Monday, Tuesday etc?

Code: [Select]
@echo off

echo Wscript.echo eval(WScript.Arguments(0))>evaluate.vbs

For /f "delims==" %%N in ( ' cscript //nologo evaluate.vbs "weekday(date)" ' ) do set daynumber=%%N
For /f "delims==" %%D in ( ' cscript //nologo evaluate.vbs "weekdayname(weekday(date))" ' ) do set dayofweek=%%D
echo Today is Day %daynumber% %dayofweek%

For /f "delims==" %%N in ( ' cscript //nologo evaluate.vbs "weekday("1/1/2009")" ' ) do set daynumber=%%N
For /f "delims==" %%D in ( ' cscript //nologo evaluate.vbs "weekdayname(weekday("1/1/2009"))" ' ) do set dayofweek=%%D
echo Jan 1st 2009 was day %daynumber% %dayofweek%

del evaluate.vbs

Code: [Select]
Today is day 1 Sunday
Jan 1st 2009 was day 7 Saturday
IP logged
BatchFileCommand
Hopeful



Thanked: 1
Posts: 339




« Reply #3 on: March 15, 2009, 11:15:02 AM »

Not familiar with UK time, but here's just plain batch.

Code: [Select]
@echo off
cls
echo %date:~0,4%
pause>nul
exit

IP logged

οτη άβγαλτος μεταφ βαθμολογία
Dias de verano
Guest
« Reply #4 on: March 15, 2009, 12:41:04 PM »

echo %date:~0,4%

Code: [Select]
C:\>echo %date:~0,4%
15/0
IP logged
BatchFileCommand
Hopeful



Thanked: 1
Posts: 339




« Reply #5 on: March 15, 2009, 12:52:57 PM »

Well then you're date variable has a different result.


Code: [Select]
C:/>echo %date:~0,4%
Sun


Twosides, in a batch file do:

Code: [Select]
@echo off
cls
echo %date%
pause>nul
exit

and tell us the result.
IP logged

οτη άβγαλτος μεταφ βαθμολογία
Dias de verano
Guest
« Reply #6 on: March 15, 2009, 01:06:13 PM »

I don't know about anywhere else, but where I come from (England) the phrase "day of the week" means the name of the day: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday or Sunday. I gather that Twosides comes from the UK too. In the UK, the standard %date% format is dd/mm/yyyy, so that today's %date% is 15/03/2009, unlike (say) the USA where the day of the week is shown in short form at the beginning of the %date% variable thus: Sun 03/15/2009. The variation is due to Windows "Regional Settings". The fact is that outside the USA we do things differently. So maybe Twosides could post and say what he or she wanted? Otherwise we're going to have people slicing the US date string from now till Doomsday.



IP logged
twosides
Topic Starter
Rookie



Posts: 11


« Reply #7 on: March 15, 2009, 05:34:13 PM »

Many thanks for the replies.
But Dias de verano was the only option that worked.
Entering 'date' gives a result of dd/mm/yyyy in the UK.

Dias de verano's code uses a vbs script, is there a way of doing this with just the straight batch code?

Cheers

IP logged
Reno
Hopeful



Thanked: 32
Posts: 323




« Reply #8 on: March 15, 2009, 10:59:07 PM »

YES, there is a way to do it in straight batch code.

but the code is complicated, and someone will come and yell "Download a Real Programming Languange"

and there is a shorter solution using debug + batch, but someone will come and say "I Hate Debug"
IP logged
ghostdog74
Mentor



Thanked: 26
Posts: 1,511


« Reply #9 on: March 16, 2009, 01:27:53 AM »

and there is a shorter solution using debug + batch, but someone will come and say "I Hate Debug"
Its true you can use Debug, but its totally NOT PRACTICAL in the real world where time is precious. i certainly wouldn't want to waste my time looking up assembly opcodes/mneumonics and making something up that other people couldn't understand, when languages like vbscript and other programming languages can do the job better.
IP logged

Dias de verano
Guest
« Reply #10 on: March 16, 2009, 01:44:32 AM »

is there a way of doing this with just the straight batch code?

More lines, but no VBScript...

I used an algorithm I found on a coding website. As you can see, it only uses integer divisions, which are the only kind that batch arithmetic provides. Personally I don't think it's "complicated" at all. The "meat" of it is just 4 set /a statements.

Quote
To calculate the day on which a particular date falls, the following algorithm may be used (the divisions are integer divisions, in which the remainders are discarded):

    a = (14 - month) / 12
    y = year - a
    m = month + 12 * a - 2
    d = (day + y + y / 4 - y / 100 + y / 400 + (31 * m / 12) % 7

    The value of d is 0 for a Sunday, 1 for a Monday, 2 for a Tuesday, etc.

Code: [Select]
@echo off

REM Get day, month, year from %date%
REM Assumes %date% format is dd/mm/yyyy
REM E.g. on 16 March 2009 %date% expands to 16/03/2009

  Set day=%date:~0,2%
Set month=%date:~3,2%
 Set year=%date:~6,4%

REM If day and/or month have a leading
REM zero, make the number hexadecimal to
REM avoid set /a making 08 & 09 into
REM nonsense octal values and stopping
REM with error message

if   "%day:~0,1%"=="0"   set day=0x%day%
if "%month:~0,1%"=="0" set month=0x%month%

REM Implement algorithm

set /a a=(14-%month%)/12
set /a y=%year%-%a%
set /a m=%month%+12*%a%-2
set /a DayOfWeekNumber=(%day% +%y% + %y% / 4 - %y% / 100 + %y% / 400 + (31 * %m% / 12)) %% 7

REM Get day name from number

if %DayOfWeekNumber% EQU 0 set DayOfWeekName=Sunday
if %DayOfWeekNumber% EQU 1 set DayOfWeekName=Monday
if %DayOfWeekNumber% EQU 2 set DayOfWeekName=Tuesday
if %DayOfWeekNumber% EQU 3 set DayOfWeekName=Wednesday
if %DayOfWeekNumber% EQU 4 set DayOfWeekName=Thursday
if %DayOfWeekNumber% EQU 5 set DayOfWeekName=Friday
if %DayOfWeekNumber% EQU 6 set DayOfWeekName=Saturday

REM Day of week number and name are in 2 variables

echo Day of week (number) is %DayOfWeekNumber%
echo Day of week (name)   is %DayOfWeekName%
« Last Edit: March 16, 2009, 03:08:14 AM by Dias de verano » IP logged
Reno
Hopeful



Thanked: 32
Posts: 323




« Reply #11 on: March 16, 2009, 04:03:28 AM »

not bad. i admire this one for the coding logic. you have successfully implement it in straight-forward fashion this time.

the first solution you post at 1:50 failed on year<1900 and year>2100, but this one is good.

don't forget to give credit to whoever he is that invent this algorithm, and thanks to dias for the batch conversion.
IP logged
Dias de verano
Guest
« Reply #12 on: March 16, 2009, 04:37:19 AM »


the first solution you post at 1:50 failed on year<1900 and year>2100


Yes I know, that's why I decided to start from first principles.

As far as I can tell, the algorithm is a simplification of Zeller's Congruence (Julius Christian Johannes Zeller 1822 -1899)

I found it here

http://www.tondering.dk/claus/cal/node3.html#SECTION00360000000000000000


IP logged
Dias de verano
Guest
« Reply #13 on: March 16, 2009, 12:46:47 PM »

Shorter method of getting day name from number

Code: [Select]
REM Get day name from number
setlocal enabledelayedexpansion
set num=0
for %%D in (Sun Mon Tues Wednes Thurs Fri Satur) do (
if !num! EQU %DayOfWeekNumber% set DayOfWeekName=%%Dday
set /a num+=1
)
IP logged
twosides
Topic Starter
Rookie



Posts: 11


« Reply #14 on: March 16, 2009, 04:51:41 PM »

Thanks for the replies.
I am trying them now.

Many thanks
IP logged
Pages: [1] 2  All - (Top) Print 
Home / Microsoft / Microsoft DOS / Day of the Week « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.103 seconds with 20 queries.