i want to check if Date is valid or not using Script shell.
I tried this:
date "+%d/%m/%Y" -d "09/10/2013" > /dev/null 2>&1
is_valid=$?
It works fine for me when it comes to dd/MM/YYYY format. I Tried
date "+%Y%m%d%H%M%S" -d "20191001041253" > /dev/null 2>&1
is_valid=$?
for YYYYmmddHHMMSS but it doesn't work.
Could someone please guide me to check if a date is valid or not with the YYYYmmddHHMMSS.
GNU date has a very relaxed syntax as to how to read input. The "+..." is only for specifying the output of it, you cannot in any way specify how should it read/parse the input.
You need to first convert your input into something GNU date can understand. I found that the format mentioned in the DATE STRING section in man page seems to be most reliable.
s=20191001041253
# 2019-10-01 04:12:53
s="${s::4}-${s:4:2}-${s:6:2} ${s:8:2}:${s:10:2}:${s:12:2}"
if date --date="$s" >/dev/null 2>/dev/null; then
echo "Its ok"
else
echo "its invalid"
fi
Note that bsd date has an -f options that allows specifying input format. I wish such option would be available with gnu date.
The 'date' utility accept date values in many formats (look at info date, 'Date Input formats' section). The bad news is that the format you need ( 'YYYYmmddHHMMSS') is not supported. The good news is that similar format (ISO compact format 'YYYYmmdd< HH:MM:SS') is supported.
date -d '20191001 05:06:07'
Tue Oct 1 05:06:07 IDT 2019
If you want to LIMIT the input date format to accept ONLY YYYYmmddHHMMSS, you actually have to write a small filter
input=20191001050607
# Convert to YYYYmmdd HH:MM:SS
t="${input:0:8} ${input:8:2}:${input:10:2}:${input:12:2}"
is_valid=
# Validate; Check for 14 digits + try to convert to date
[[ "$input" =~ ^[0-9]{14}$ ]] && date -d "$t" > /dev/null 2>&1 && is_valid=YES
echo "$is_valid"
Try changing it to ISO format:-
$ date "+%Y%m%d%H%M%S" -d "2019-10-01 04:12:53"
20191001041253
$ echo $?
0
Related
I have tried to do this in terminal:
touch -t `date +%yy%mm%dd%HH%MM`/path/to/file
but it gives the error touch: out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]. How can I get the right date currently?
Try running date +%yy%mm%dd%HH%MM by itself, and the problem should become apparent:
$ date +%yy%mm%dd%HH%MM
19y10m26d11H40M
The format code for a two-digit year is %y, so %yy gives a two-digit year followed by a stray "y". All the others work the same way. So just un-double all the format codes. Oh, and I recommend using $( ) instead of backticks:
$ date +%y%m%d%H%M
1910261141
$ touch -t $(date +%y%m%d%H%M) /path/to/file
But it's actually easier than that, because touch defaults to the current time. So all you actually need is touch /path/to/file.
I am using GNU bash, version 4.3. How can I represent timezone as hours and minute?
Here is the code.
./time.sh
printf -v t '%(%s)T' -1
export TZ=UTC
counter=1
while [ "$counter" -le 10 ]
do
((t=t+600))
printf '%(%y-%m-%dT%H:%M:%S%z)T\n' "$t"
((counter++))
done
echo All done
Here is the result.
#bash time.sh
18-01-17T07:29:50+0000
18-01-17T07:39:50+0000
18-01-17T07:49:50+0000
18-01-17T07:59:50+0000
18-01-17T08:09:50+0000
18-01-17T08:19:50+0000
18-01-17T08:29:50+0000
18-01-17T08:39:50+0000
18-01-17T08:49:50+0000
18-01-17T08:59:50+0000
All done
But I'd like to show the result format like '18-01-17T08:59:50+00:00'.
How can I do that?
Use date instead of printf:
date --iso-8601=seconds --date="#$t"
Note that this assumes GNU date; the --iso-8601 argument does not exist in the POSIX specification. POSIX doesn't have %:z either (nor even %z), so if this needs to be portable you'll need a different solution.
I have a csv file I am reading which contains various time stamps of the format "2016-06-13T18:30:31.868Z". I would like to know how ,using bash script, how to convert the string into "18:30:31" leaving only the time component.
Thanks.
You can do this just with bash parameter expansion
datetime="2016-06-13T18:30:31.868Z"
no_date=${datetime #*T} # remove "2016-06-13T"
timestamp=${no_date%.*} # remove ".868Z"
or with a regular expression:
if [[ $datetime =~ ([0-9][0-9]:[0-9][0-9]:[0-9][0-9]) ]]; then
timestamp=${BASH_REMATCH[1]}
fi
It would be better to manipulate date-time with a tool that understands it rather than any text-processing tool.
With GNU date, you can pass the time format and get the output in desired HH:MM:SS format with the %T identifier:
date -d '2016-06-13T18:30:31.868Z' '+%T'
To set a specific timezone, use TZ environment variable:
TZ=EST date -d '2016-06-13T18:30:31.868Z' '+%T'
In Bash
VAR="2016-06-13T18:30:31.868Z"
FRONT_REMOVED=${VAR%.*}
BACK_REMOVED=${FRONT_REMOVED#T*}
Back Removed should be what you want %.* deletes everything after the period
#T* deletes everything before the T
Use substring expansion:
$ a="2016-06-13T18:30:31.868Z"
$ echo "${a:11:8}"
18:30:31
I want to write a bash script that will run on a given but process data with next days date, My current approach is to get the unix time stamp and add a days worth of seconds to it, but I cant get it working, and haven't yet found what I'm looking for online.
Here's what I've tried, I feel like the problem is that its a string an not a number, but I dont know enough about bash to be sure, is this correct? and how do I resolve this?
today="$(date +'%s')"
tomorrow="$($today + 86400)"
echo "$today"
echo "$tomorrow"
If you have gnu-date then to get next day you can just do:
date -d '+1 day'
Some of the answers for this question depend on having GNU date installed. If you don't have GNU date, you can use the built-in date command with the -v option.
The command
$ date -v+1d
returns tomorrow's date.
You can use it with all the standard date formatting options, so
$ date -v+1d +%Y-%m-%d
returns tomorrow's date in the format YYYY-MM-DD.
$(...) is command substitution. You're trying to run $today + 86400 as a command.
$((...)) is arithmetic expansion. This is what you want to use.
tomorrow=$(( today + 86400 ))
Also see http://mywiki.wooledge.org/ArithmeticExpression for more on doing arithmetics in the shell.
I hope that this will solve your problem here.
date --date 'next day'
Set your timezone, then run date.
E.g.
TZ=UTC-24 date
Alternatively, I'd use perl:
perl -e 'print localtime(time+84600)."\n"'
echo $(date --date="next day" +%Y%m%d)
This will output
20170623
I like
input_date=$(date '+%Y-%m-%d')
tomorrow_date=$(date '+%Y-%m-%d' -d "$input_date + 1 day")
echo "$tomorrow_date"
It returns the date of the day after $input_date, in format YYYY-MM-DD
You can try below
#!/bin/bash
today=`date`
tomorrow=`date --date="next day"`
echo "$today"
echo "$tomorrow"
I have two files created at different times.
In my shell scripting program, I want to initialize a variable with the date of the file which was created earlier.
For eg. if file1 was created on 22 April and file2 was created on April 19. my variable should be initialized to 19th April.
How can I do this in bash shell?
Assuming existence of GNU stat (part of GNU coreutils):
if [[ $(stat -c%W <file1>) -lt $(stat -c%W <file2>) ]]
then
EARLIER="$(stat -c%w <file1>)"
else
EARLIER="$(stat -c%w <file1>)"
fi
Note the case of %W (integer) vs. %w (human-readable) is significant.
%W / %w is birth time, since you asked for "creation time". Usually %Y / %y (last modification) or %Z / %z (last change) are more meaningful.
If you need a different format for your date, you could feed the stat output to date, e.g.:
date -d "$(stat -c%w <earlier_file>)" +"%Y-%m-%d")
PS: While you are at the subject of time stamps, please consider RFC 3339 for your formatting, i.e. YYYY-MM-DD HH:MM:DD-TZ, which is non-ambiguous, portable, and sortable.
/bin/date can convert to timestamps, using the %s format, after which date comparison is a straight numeric compare, so the solution to your problem, modulo syntax errors, is:
DATE1=`date -j '22 April' +"%d %B"`
DATE2=`date -j '19 April' +"%d %B"`
if [[ $DATE2 < $DATE1 ]]; then
export VAR=`date -j $DATE1 +"%s"`
else
export VAR=`date -j $DATE2 +"%s"`
fi