How to get date part from the current date in batch file - windows

My File contains Date as Thu 03/26/2015
I need to get output as 03/26/2015 I don't need day part in my date

Use command
date /t
Output will gate date only

Related

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)")

how to extract date only from the creation/modified date of a file?

Let's say the file name is : filetest.txt
and is present in the directory: inventory/data
How can I get the date at which this file was last modified/created?
Example : let's say if the file is created on 3rd June 2007 then it should return only value 3.
That means, how to extract the date only from that timestamp of file?
You can use the date command to extract the modification time of a file by passing it as reference.
date -r filename prints the last modified timestamp of the file.
Sat Jul 3 16:03:53 IST 2021
date +%d -r filename print just the day you asked for, but with a leading zero.
03
date +%-d -r filename prints the day, without a leading zero.
3

Concatenating text and command output in Bash script to output to file

I'm writing a bash script in FreeBSD that appends commands to a log file. Before I execute the commands that will append the log data, I want to print a line in the file which shows the current date above the data, like this:
---Tue Aug 20 17:26:37 EDT 2019---
I know that I can use the date command to output the timestamp, but I'm not sure how to include the "---" before and after the timestamp to add to the file. What's the simplest way to do this?
You can pass a format string to date:
date '+---%a %b %e %H:%M:%S %Z %Y---'

How can I output the date of the last "day"?

I want to print the day of the last Monday for example
using the date command in a terminal
Provided I do not know what day/ date is today but I can use date to find out the date and then use it to find out what date the first previous monday was.
If you are using gnu date, you can try the following command:
date -d "last monday"

Add the specific hour to a given date in shell script

In my shell, when a date is given with YYYYmmddHHMM format, I would like to add an hour to the given date.
My current code is as below:
DATE=201502252310
NEXT_DATE=$(date +%Y%m%d%H%M -ud "$DATE +1 hour")
echo $NEXT_DATE
When the given date is 201502121010, the result is 2015021210100100.
In fact, I'd like to get 201502121110.
When the given date is 201502122310, the result I'd like to get is 201502130010.
But, it shows date: invalid date '201502122310 +1 hour'.
It seems my code is something wrong.
How can I do to add the specific hour to the given date correctly???

Resources