I'm trying to write a cmd script that gets the current date and time and formats it into a way that sqlserver can input it as a datetime
.
So far, I have:
@echo off for /F "tokens=1-4 delims=/ " %%i in ('date /t') do ( set dow=%%i set mon=%%j set day=%%k set yr=%%l set mydate=%%j/%%k/%%l )
This prints out 10/22/2010
I have not been able to figure out how to get the time into a usable format. I tried working with time /t
, but it only gives the hours and minutes, and I need the seconds also.
You can use $T for time and $D for date, as well as several other expansions. See also: https://ss64.com/nt/prompt.html. https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/prompt.
REM [comment] Purpose: Provides a way to insert remarks (that will not be acted on) into a batch file. Discussion. During execution of a batch file, DOS will display (but not act on) comments which are entered on the line after the REM command. You cannot use separators in the comment except the space, tab, and comma.
echo off. When echo is turned off, the command prompt doesn't appear in the Command Prompt window. To display the command prompt again, type echo on. To prevent all commands in a batch file (including the echo off command) from displaying on the screen, on the first line of the batch file type: @echo off.
In computing, TIME is a command in DEC RT-11, DOS, IBM OS/2, Microsoft Windows and a number of other operating systems that is used to display and set the current system time. It is included in command-line interpreters (shells) such as COMMAND.COM , cmd.exe , 4DOS, 4OS2 and 4NT.
Use the %TIME%
pseudo-variable.
You can also use %DATE%
in above script.
Both have the same limitations, in that they depend on your locale. The code is not portable and you'll be up for surprises in other environments. But that's how date and time in cmd works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With