Computer Hope

Microsoft => Microsoft DOS => Topic started by: Dias de verano on December 24, 2008, 11:52:02 AM

Title: VBS Date & Time Accessible from a batch
Post by: Dias de verano on December 24, 2008, 11:52:02 AM
Some weeks ago I hacked together a crude batch file which created a VB script & then read back the results to make various VBS date stuff accessible in a batch. I was gratified to see people make use of it and since I am on Xmas break from work I decided to tidy it up a bit and add some features as you can see below

Code: [Select]
@echo off
set vbsfile=newdate.vbs
echo        Newdate = (Date())>%vbsfile%
echo       DateYear = DatePart("YYYY", Newdate)>>%vbsfile%
echo      DateMonth = DatePart("M"   , Newdate)>>%vbsfile%
echo        DateDay = DatePart("D"   , Newdate)>>%vbsfile%
echo     WeekOfYear = DatePart("WW"  , Newdate)>>%vbsfile%
echo      DayOfYear = DatePart("Y"   , Newdate)>>%vbsfile%
echo  WeekDayNumber = DatePart("W"   , Newdate)>>%vbsfile%
echo TodayNameShort = WeekdayName(WeekDayNumber,True)>>%vbsfile%
echo  TodayNameFull = WeekdayName(WeekDayNumber,False)>>%vbsfile%
echo       TimeHour = Hour(Time)>>%vbsfile%
echo     TimeMinute = Minute(Time)>>%vbsfile%
echo     TimeSecond = Second(Time)>>%vbsfile%
echo If TimeHour^<12 Then>>%vbsfile%
echo   AMorPM="AM">>%vbsfile%
echo Else>>%vbsfile%
echo   AMorPM="PM">>%vbsfile%
echo End If>>%vbsfile%
echo Wscript.Echo DateYear^&" "^&DateMonth^&" "^&DateDay^&" "^&Week^
OfYear^&" "^&DayOfYear^&" "^&WeekDayNumber^&" "^&Today^
NameShort^&" "^&TodayNameFull^&" "^&TimeHour^&" "^&Time^
Minute^&" "^&TimeSecond^&" "^&AMorPM>>%vbsfile%

echo Date And Time Stuff Demo

for /f "tokens=1-12 delims= " %%A in ('cscript //nologo %vbsfile%') do (
echo A Date Year                 : %%A
echo B Date Month                : %%B
echo C Date Day                  : %%C
echo D Week Of Year              : %%D
echo E Day Of Year               : %%E
echo F Day Of Week [Number]      : %%F
echo G Day Of Week [Short Name]  : %%G
echo H Day Of Week [Full Name]   : %%H
echo I Time Hour                 : %%I
echo J Time Minute               : %%J
echo K Time Second               : %%K
echo L Is AM or PM               : %%L
)




Output

Code: [Select]
Date And Time Stuff Demo
A Date Year                 : 2008
B Date Month                : 12
C Date Day                  : 24
D Week Of Year              : 52
E Day Of Year               : 359
F Day Of Week [Number]      : 4
G Day Of Week [Short Name]  : Wed
H Day Of Week [Full Name]   : Wednesday
I Time Hour                 : 18
J Time Minute               : 41
K Time Second               : 10
L Is AM or PM               : PM

Notes:

The time (hour) will be between 0 and 23. (i.e. what Americans call "military time") So AM/PM may be redundant
Minutes & seconds will be from 0 to 59. Values under 10 are single digits.

Comments / suggestions welcome!

Happy holidays to everyone. Except for this guy, who is "Off to Guantanamo!"

(http://i124.photobucket.com/albums/p29/badoit/santa_sq.jpg)

Title: Re: VBS Date & Time Accessible from a batch
Post by: Dusty on December 24, 2008, 06:04:01 PM
Thank you for another gem.

Quote
Happy holidays to everyone. Except for this guy, who is "Off to Guantanamo!"

Do you supply your own orange overall? ;D

Have a great time.



Title: Re: VBS Date & Time Accessible from a batch
Post by: DAan on January 04, 2009, 12:18:15 AM
Thank you for this.
results can be open automaticaly when you run the bat file?
Title: Re: VBS Date & Time Accessible from a batch
Post by: Dias de verano on January 04, 2009, 03:04:06 AM
Thank you for this.
results can be open automaticaly when you run the bat file?

What do you mean?
Title: Re: VBS Date & Time Accessible from a batch
Post by: Helpmeh on January 05, 2009, 03:18:15 PM
DAan probably is trying to ask if the BAT file displays the date/time when it is finished running.
Title: Re: VBS Date & Time Accessible from a batch
Post by: Dias de verano on January 05, 2009, 03:35:21 PM
DAan probably is trying to ask if the BAT file displays the date/time when it is finished running.

Well, if so, then all I can say is that it should be obvious to the intended users (either from looking at the "output" box above, or by actually saving and running the batch file) what it does. Also people who have some experience of writing & using batch files will be able to see how to modify it for their own purposes.
Title: Re: VBS Date & Time Accessible from a batch
Post by: Helpmeh on January 06, 2009, 05:23:05 PM
DAan probably is trying to ask if the BAT file displays the date/time when it is finished running.

Well, if so, then all I can say is that it should be obvious to the intended users (either from looking at the "output" box above, or by actually saving and running the batch file) what it does. Also people who have some experience of writing & using batch files will be able to see how to modify it for their own purposes.


True....
Title: Re: VBS Date & Time Accessible from a batch
Post by: Hedonist on January 27, 2009, 01:01:53 AM
Dias.  Is it possible to enter a date and have your script produce the results from that date.  If 23-11-2008 was entered can your code be modified to display month, day, year, week#, day# etc using that date as input?

I have searched T.Salmi, alt.msdos and others without success.

Cheers
Title: Re: VBS Date & Time Accessible from a batch
Post by: Dias de verano on January 27, 2009, 01:07:46 AM
Yes. It is 8:08 AM here and I am off to work, I will answer after work this afternoon.

You should be looking at VBS date functions really.
Title: Re: VBS Date & Time Accessible from a batch
Post by: Dias de verano on January 27, 2009, 12:10:04 PM
Dias.  Is it possible to enter a date and have your script produce the results from that date.  If 23-11-2008 was entered can your code be modified to display month, day, year, week#, day# etc using that date as input?

I have searched T.Salmi, alt.msdos and others without success.

Cheers

Qdate.bat
Code: [Select]

@echo off


REM Create VBS script
set vbsfile=DateInfo.vbs
echo           Newdate = WScript.Arguments(0)>%vbsfile%
echo if IsDate(Newdate) then>>%vbsfile%
echo           Newdate = CDate(Newdate)>>%vbsfile%
echo          DateYear = DatePart("YYYY", Newdate)>>%vbsfile%
echo         DateMonth = DatePart("M"   , Newdate)>>%vbsfile%
echo           DateDay = DatePart("D"   , Newdate)>>%vbsfile%
echo        WeekOfYear = DatePart("WW"  , Newdate)>>%vbsfile%
echo         DayOfYear = DatePart("Y"   , Newdate)>>%vbsfile%
echo     WeekDayNumber = DatePart("W"   , Newdate)>>%vbsfile%
echo    TodayNameShort = WeekdayName(WeekDayNumber,True)>>%vbsfile%
echo     TodayNameFull = WeekdayName(WeekDayNumber,False)>>%vbsfile%
echo    MonthNameShort = MonthName(DateMonth,True)>>%vbsfile%
echo     MonthNameLong = MonthName(DateMonth,False)>>%vbsfile%
echo    Wscript.Echo DateYear^&" "^&DateMonth^&" "^&DateDay^&" "^&Week^
OfYear^&" "^&DayOfYear^&" "^&WeekDayNumber^&" "^&Today^
NameShort^&" "^&TodayNameFull^&" "^&MonthNameShort^&" "^&MonthNameLong>>%vbsfile%
echo else>>%vbsfile%
echo     Wscript.Echo "ERROR">>%vbsfile%
echo end if >>%vbsfile%

REM below is example batch code to call the vbs

REM Example of use
REM 1) get a date string into a variable
set /p Querydate=Enter Date:

REM 2) pass the string to the VBS script and
REM    parse the output, which will either be 10
REM    items of data, or else the string "ERROR"
REM    if the string is not a valid date.
REM
REM    Store output of vbs script in variables
for /f "tokens=1-10 delims= " %%A in ('cscript //nologo %vbsfile% %querydate%') do (
set Year=%%A
set Month=%%B
set Day=%%C
set WeekNumber=%%D
set DayNumber=%%E
set DayOfWeekNum=%%F
set DayNameShort=%%G
set DayNameLong=%%H
set MonthNameShort=%%I
set MonthNameLong=%%J
)

REM 3) Do something with the data
if "%Year%"=="ERROR" (
echo Error: invalid date
) else (
Echo Data for Date: %Querydate%
echo.
echo Date Year                 : %Year%
echo Date Month                : %Month%
echo Date Day                  : %Day%
echo Week Of Year              : %WeekNumber%
echo Day Of Year               : %DayNumber%
echo Day Of Week [Number]      : %DayOfWeekNum%
echo Day Of Week [Short Name]  : %DayNameShort%
echo Day Of Week [Full Name]   : %DayNameLong%
echo Month [Short Name]        : %MonthNameShort%
echo Month [Long Name]         : %MonthNameLong%
echo.
Echo %DayNameLong%, %MonthNameLong% %Day%, %Year% is/was/will be in week %WeekNumber%
echo.
)


Alternatively you could just save the vbs script somewhere on your PATH and call it from batches


Code: [Select]

          Newdate = WScript.Arguments(0)
if IsDate(Newdate) then
          Newdate = CDate(Newdate)
         DateYear = DatePart("YYYY", Newdate)
        DateMonth = DatePart("M"   , Newdate)
          DateDay = DatePart("D"   , Newdate)
       WeekOfYear = DatePart("WW"  , Newdate)
        DayOfYear = DatePart("Y"   , Newdate)
    WeekDayNumber = DatePart("W"   , Newdate)
   TodayNameShort = WeekdayName(WeekDayNumber,True)
    TodayNameFull = WeekdayName(WeekDayNumber,False)
   MonthNameShort = MonthName(DateMonth,True)
    MonthNameLong = MonthName(DateMonth,False)
   Wscript.Echo DateYear&" "&DateMonth&" "&DateDay&" "&WeekOfYear&" "&DayOfYear&" "&WeekDayNumber&" "&TodayNameShort&" "&TodayNameFull&" "&MonthNameShort&" "&MonthNameLong
else
    Wscript.Echo "ERROR"
end if


Code: [Select]

S:\Test\Batch\querydate>qdate.bat
Enter Date:23-11-2008
Data for Date: 23-11-2008
Date Year                 : 2008
Date Month                : 11
Date Day                  : 23
Week Of Year              : 48
Day Of Year               : 328
Day Of Week [Number]      : 1
Day Of Week [Short Name]  : Sun
Day Of Week [Full Name]   : Sunday
Month [Short Name]        : Nov
Month [Long Name]         : November

Sunday, November 23, 2008 is/was/will be in week 48

S:\Test\Batch\querydate>qdate.bat
Enter Date:34-9-1788
Data for Date: 34-9-1788
Error: invalid date