How can I append time to this command prompt date command - windows

I am trying to run a .bat file with command prompt to add time to the date.
Currently, I have this code
MOVE...\folder\^"Mytest %DATE:/=-%.csv^"
This produces
..\folder\Mytest Thu 12-06-2012.csv
I want to get
..\folder\Mytest Thu 12-06-2012 21:45.csv
Tried all kinds of things but failed miserably. Help would be greatly appreciated.

This will work:
%date:/=-% %time:~0,5%.csv
The %time% uses the current time; the :~ means "a substring of", and the 0,5 says "starting at the first character (index 0) and continuing for 5 characters", so the entire thing means "give me the first 5 characters of the output of time".
Using this at a command prompt:
C:\>echo %date:/=-% %time:~0,5%
outputs
Thu 12-06-2012 18:19
The format you're using is going to cause problems with sorting, though. My advice would be to drop the day of the week portion, and change the date output to CCYY-MM-DD, which will be much more useful when you're trying to find a specific date. You can use this:
echo %date:~10,4%-%date:~4,2%-%date:~7,2% %time:~0,5%
which outputs
2012-12-06 18:33

Related

Vim formatted print date to current file

I am trying to print the current formatted as Mon, 1 Jan 2022 into my current file in vim. So far I've tried the following command, but the % sign gets put into the command as the current file.
:0r !date '+%a, %d %b %Y'
This ends up giving me an output similar to seen below.
/home/user/file.txta, /home/user/file.txtd /home/user/fileb /home/user/fileY
The issue is that the path to the current file is put in place for % as vim does, but that is causing issues for the current use case.
Any help is appreciated
The immediate solution is obviously to escape the %:
:0r !date '+\%a, \%d \%b \%Y'
But you might be interested by the built-in :help strftime(), which doesn't require escaping:
:0put=strftime('%a, %d %b %Y')

Bash shell: How to reformat string date from a variable value

I understand how to reformat a date using the date command, and I am fine with that. However, I have a wrinkle in that I am struggling with - the date I want to reformat is the output of another command, so I am storing it in the variable. I am struggling with the syntax of how to specify that I want to take the output of one command, and run it through date -d, and store it in another variable. Here is what I tried:
expdate=`get_expire_date.sh`
echo $expdate
Mon 23 Mar 2022 05:05:05 PM UTC
expdval=`date -d'($expdate)'`
echo $expdval
I get today's date, not the converted expire date from the script output. If I leave the parenthesis out, of course, it treats $expdate as the literal text to translate and gives an error, whereas if I leave the single quote marks off, it uses the spaces in the date string as a delimiter and only grabs the first token.
What am I doing wrong?
First, parameter expansion doesn't occur inside single quotes. You would need to change the single quotes
expdval=`date -d'($expdate)'`
to double quotes
expdval=`date -d"($expdate)"`
Second, the parentheses create an invalid input, which results (for reasons I don't really understand) in an output of midnight of the current day. (You'll get the same result with the trivial invalid date date -d "".)
Drop the parentheses, and you'll get the same date back (because the input format matches the default output format).
$ date -d "$expdate"
Wed Mar 23 13:05:05 EDT 2022
To actually manipulate it, you'll need an explicit output format:
$ date -d "$expdate" +%Y-%m-%d
2022-03-23
or some form of date "arithmetic":
$ date -d "$expdate + 2 days"
Fri Mar 25 13:05:05 EDT 2022
I found I had to use double-quotes instead, like this (and sorry for the old way of doing things, updating to new shell syntax):
expdval=$(date -d"$(get_expire_date.sh)")

Get today's date minus one Year in Unix (AIX)

I need to find today's date and then subtract a year and format that date into the YYYY-MM-dd format.
I am able to accomplish this with a script I wrote, but apparently it is only compatible with bash. I need it to be compatible with AIX.
lastYear=`date +'%Y-%m-%d' -d 'last year'`
searchDate="$lastYear 00.00.00";
echo "Retrieving data start from $searchDate"
myquery="myquery >= '$searchDate'"
The result when run on an AIX machine is that it only passes the "00:00:00" part of the $searchDate, the date does not prefix before the time as I hoped. What is the safest way to write this for the most compatibility across Linux/Unix variations?
Thank you!
Why make it so complicated?
#!/bin/ksh
typeset -i year=$( date +%Y )
(( year -= 1 ))
typeset rest=$( date +%m-%d )
echo "${year}-${rest}"
This should work in any shell. If you use sh replace the
$( ... )
with back tics
` ... `
But for bash and ksh I use $( ... ) -- just personal preference.
Checkout Updated Section Below
Original Answer
Try this. It uses the -v flag to display the result of adjusting the current date by negative one year -1y
searchDate=$(date -v-1y +'%Y-%m-%d')
echo "Retrieving data start from $searchDate"
myquery="myquery >= '$searchDate'"
Here is the output:
Retrieving data start from 2017-06-21
Note: I did try to run the lines you provided above in a script file with a bash shebang, but ran into an illegal time format error and the output was 00:00:00. The script I provided above runs cleanly on my unix system.
Hope this helps. Good luck!
Updated Section
Visit ShellCheck.net
It's a nice resource for testing code compatibility between sh, bash, dash, and ksh(AIX's default) shells.
Identifying the Actual Issue
When I entered your code, it clearly identified syntax that is considered legacy and suggested how to fix it, and gave this link with an example and a great explanation. Here's the output:
lastYear=`date +'%Y-%m-%d' -d 'last year'`
^__Use $(..) instead of legacy `..`.
So, it looks like you might be able to use the code you wrote, by making one small change:
lastYear=$(date +'%Y-%m-%d' -d 'last year')
Note: This question already has an accepted answer, but I wanted to share this resource, because it may help others trouble-shoot shell compatibility issues like this in the future.

How to convert date to timestamp in bash shell?

I need to write a script to check one of my log files to check its last updated time. If it's greater than 90 minutes, I need to get alerted. But now I am stuck in timestamp conversion.
I have the below line in my code, but it's not working. It's throwing an error like ./monitor_log.sh: line 13: Aug: command not found.
modifeidTimestamp=$(date --date="`$lastModified`" +"%s");
The command below is working fine, but I have to use a variable. Can anyone help?
date --date='Aug 25 06:07:30' +"%s"
You can do:
lastModified='Aug 25 06:07:30'
modifeidTimestamp=$(date --date="$lastModified" +"%s")
echo "$modifeidTimestamp"
1440497250
No need to use reverse tick around lastModified variable. Use wrap it in double quotes.

Issue with %date% and %time% on the command line

I got this command to generate a log file that contains the date in the name.
echo %time%;stuff done >> C:\myfile_%date%.log
Output:
The system cannot find the path specified.
If I do remove the system variable, all fine.
I have attempted to add "" and `` and '', no go so far.
Some help would be welcome :)
Thanks.
You need to generate a date without spaces in it. %date% outputs Mon 16/06/2014 which when passed as a filename causes problems with the creation. If you substring each of the date elements and build a custom filename with the below:
echo %time%;stuff done >> C:\myfile_%date:~10%%date:~4,2%%date:~7,2%%time:~0,2%%time:~3,2%.log
your command will work. The filename I get as a result is:
myfile_201416062105.log
which as a backwards timestamp will also make them easier to sort and find after the generation.

Resources