net_user Posted May 11, 2011 Share Posted May 11, 2011 i have a batch file that echos the date into a txt fileecho %date% > c:\file.txti get this "Wed 05/11/2011"how do i make the code just output "05/11/2011" Link to comment Share on other sites More sharing options...
jaclaz Posted May 11, 2011 Share Posted May 11, 2011 i have a batch file that echos the date into a txt fileecho %date% > c:\file.txti get this "Wed 05/11/2011"how do i make the code just output "05/11/2011"See here:http://www.robvanderwoude.com/datetiment.phpDepending on how "universal" you want the solution to be, there are solutions anging from very simple to very complex.A simple one, only valid for your example/language/regional setting could be:FOR /F "tokens=2" %%A IN ("%Date%") DO ECHO %%AAnother one is:CALL :SUB %Date%....GOTO :EOF....:SUBECHO %2GOTO :EOFjaclaz Link to comment Share on other sites More sharing options...
net_user Posted May 11, 2011 Author Share Posted May 11, 2011 how would i make it out put in an example like thisMay 11, 2011 Link to comment Share on other sites More sharing options...
Yzöwl Posted May 11, 2011 Share Posted May 11, 2011 In answer to your first question>C:\file.txt ECHO=%DATE:~-10%For your second question, It can be done through a For loop with a counter and comparing the counter with the month name, but unless it is for a specific known locale etc. only I'd strongly suggest using something other than batch only functions for this stuff, (perhaps JScript). Link to comment Share on other sites More sharing options...
gunsmokingman Posted May 11, 2011 Share Posted May 11, 2011 Here is a Cmd Script that makes a VBS script to make the date formateEG Month Name, Day Name , Date, YearSave As MyDate.cmd@Echo OffCLSMode 55, 5Color F9Title Custom Date DemoSet VBS=MyDate.vbsEcho Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject") > %VBS%Echo Dim Cmd, Ts >> %VBS%Echo Cmd = "Date.cmd" >> %VBS%Echo Set Ts = Fso.CreateTextFile(Cmd) >> %VBS%Echo Ts.WriteLine "Set MyDate="^&MonthName(Month(Now)) ^& ", " ^& _ >> %VBS%Echo WeekdayName(Weekday(Now)) ^& _ >> %VBS%Echo ", " ^& Day(Now) ^& ", " ^& Year(Now) >> %VBS%Echo Ts.Close >> %VBS%%VBS%call Date.cmdDel %VBS%Del Date.cmdEcho.Echo MyDate=%MyDate%pause Link to comment Share on other sites More sharing options...
Geej Posted May 12, 2011 Share Posted May 12, 2011 (edited) Here is autoit code that return a console output (The autoit script need to compile as console application)ConsoleDate.au3Select Case @mon=01 $sMonth="Jan" Case @mon=02 $sMonth="Feb" Case @mon=03 $sMonth="Mar" Case @mon=04 $sMonth="Apr" Case @mon=05 $sMonth="May" Case @mon=06 $sMonth="Jun" Case @mon=07 $sMonth="Jul" Case @mon=08 $sMonth="Aug" Case @mon=09 $sMonth="Sept" Case @mon=10 $sMonth="Oct" Case @mon=11 $sMonth="Nov" Case @mon=12 $sMonth="Dec"EndSelect$sCurrentdate=$sMonth&" "&@MDAY&", "&@YEARConsoleWrite ($sCurrentdate); Will output to console as: May 12, 2011See return sample Edited May 12, 2011 by Geej Link to comment Share on other sites More sharing options...
Yzöwl Posted May 12, 2011 Share Posted May 12, 2011 Since you've had a hybrid VBScript batch and an AutoIt solution, I thought I'd provide the hybrid batch solution using JScript as I suggested earlier:DateStrMon.cmd@if (@X)==(@Y) @goto :Dummy @end/* Batch part@echo offfor /f "tokens=*" %%# in ('cscript //nologo //e:jscript "%~f0"') do echo=%%#pausegoto :eofJscript part */var currMonth, currDate, currYear;var dateObj = new Date();var monthsArr = new Array();currMonth = dateObj.getMonth();currDate = dateObj.getDate();currYear = dateObj.getFullYear();monthsArr[0] = "January";monthsArr[1] = "February";monthsArr[2] = "March";monthsArr[3] = "April";monthsArr[4] = "May";monthsArr[5] = "June";monthsArr[6] = "July";monthsArr[7] = "August";monthsArr[8] = "September";monthsArr[9] = "October";monthsArr[10] = "November";monthsArr[11] = "December";WScript.Echo(monthsArr[currMonth] + " " + currDate + ", " + currYear); Link to comment Share on other sites More sharing options...
jaclaz Posted May 15, 2011 Share Posted May 15, 2011 This "pure batch" should work.@ECHO OFFSETLOCAL ENABLEEXTENSIONSSETLOCAL ENABLEDELAYEDEXPANSION::Get the current date formatFOR /F "tokens=2,3,4 delims=(/)" %%A IN ('VER ^| DATE ^| FIND "("') DO (Set First=%%ASet Second=%%BSet Third=%%C)::Get the Current DateFOR /F "tokens=2,3,4 delims=:/" %%A IN ('VER ^| DATE ^| FIND /V "("') DO (Set /A First_value=%%ASet /A Second_value=%%BSet /A Third_value=%%C)::Add here Local settings::This is ItalianIF "%First%%Second%%Third%"=="ggmmaa" (SET Day=FirstSET Month=SecondSET Year=ThirdSET Lang=IT)::This is English-USIF "%First%%Second%%Third%"=="mmddyy" (SET Day=SecondSET Month=FirstSET Year=ThirdSET Lang=ENUS)::SET Variables::Change string values of variables according to your languageIF "%Lang%"=="ENUS" Set Months=January February March April May June July August September October November DecemberIF "%Lang%"=="IT" Set Months=Gennaio Febbraio Marzo Aprile Maggio Giugno Luglio Agosto Settembre Ottobre Novembre DicembreSet Counter=0For %%A IN (%Months%) DO (SET /A Counter+=1SET Month_!Counter!=%%A)SET Index=!%Month%_value!::Example output (change as you wish order of variable output and/or add separators)ECHO !%Day%_value! !Month_%Index%! !%Year%_value!jaclaz Link to comment Share on other sites More sharing options...
Yzöwl Posted May 15, 2011 Share Posted May 15, 2011 This "pure batch" should work.Scripts which are required to output localized Strings are often going to have to be altered for each environment. The example you have posted above is basically Bilingual, we could perhaps expand it by adding German, but where would it end? Link to comment Share on other sites More sharing options...
jaclaz Posted May 16, 2011 Share Posted May 16, 2011 (edited) This "pure batch" should work.Scripts which are required to output localized Strings are often going to have to be altered for each environment. The example you have posted above is basically Bilingual, we could perhaps expand it by adding German, but where would it end?Sure , I know where it starts, not where it ends.......till then if you have an Italian system it works (tested) should also on a En-Us one (untested).In other words it wasn't meant as the "ultimate batch solution to all date formatting problems" , but rather as a "quick and dirty batch solution that may work, and YMMV" .I presume that members using another localization can add their own "months" and "country code", since the few lines that need changes/additions are quite clearly laid down.jaclaz Edited May 16, 2011 by jaclaz Link to comment Share on other sites More sharing options...
allen2 Posted May 16, 2011 Share Posted May 16, 2011 You could have also used the unix tools date.exe and use this:date.exe +"%b %d, %Y" Link to comment Share on other sites More sharing options...
jaclaz Posted May 16, 2011 Share Posted May 16, 2011 (edited) And if we are allowed to use WMIC, we have "datepart.cmd":http://www.robvanderwoude.com/wmic.phpthat should be "language independant" (or at least it works allright in Italian system too ) and that should be easily adapted to show the date info any which way you like.At it's essence it resolves in:@ECHO OFFSETLOCAL ENABLEEXTENSIONSFOR /F "skip=1 tokens=1-3" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Month^,Year /Format:table 2^>NUL') DO ECHO Today is %%A/%%B/%%CBut of course you still need a "local language" array of strings for "months names". (or if you also want them, "day names")jaclaz Edited May 16, 2011 by jaclaz Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now