bash datetime concatenated with space - bash

I am trying to print the current date time with a space that looks like this:
2015-10-13 00:00:00 ( YYYY-dd-mm H:m:S)
I see that this works: dte1=`date +%Y_%m_%d_%H_%M_%s`. So does dte2=`date +%Y_%m_%d`
But I dont seem to get the one character space between the date and the timestamp. I tried concatenation , but that only results in 2015-10-1300:00:00 i.e., without the space.
Please help

$ date +"%Y-%m-%d %H:%M:%S"
2015-10-13 14:48:17
date +"%FORMAT %FORMAT" # just an example with space
Anything other than %FORMAT remains the same.

You just need to quote the argument:
dte1=$(date "+%Y-%m-%d %H:%M:%s")
echo $dte1
2015-10-13 20:41:1444761690

Related

Not able to get the expected date output after adding a second

What I'm trying to do below
echo -e "input start_time(YYYYMMDDHHMMSS)"
read word1
dt= `date "--date=${word1} -s + 1sec" +%Y%m%d%H%M%S`
echo ${dt}
If input : 20181201090909 then I got an output : 20181201090909030001. The expected output is quite incorrect. I was expecting it to be 20181201090910
You need to format differently the string you pass to date:
~$ date +%Y%m%d%H%M%S --date="20181201 09:09:09 + 1sec"
20181201090910
~$
You see a space between the date and the time, and the colon separating hour minutes seconds. See info date for more information.

Output format for dates in a range, with bash

I am trying to use bash to produce a list of dates and times between a starting point and an end point.
I would like the output to be in mm/dd/yyyy hh:mm format.
On the command line, the command:
date +"%m/%d/%Y %H:%M"
Produces the output that I am looking for.
When I use the line that is presently commented out, I get an error.
date: extra operand ‘%H:%M’
Try 'date --help' for more information.
I am not sure how to alter the script to produce the output that I am looking for.
DATE=date
#FORMAT="%m/%d/%Y %H:%M"
FORMAT="%m/%d/%Y"
start=`$DATE +$FORMAT -d "2013-05-06"`
end=`$DATE +$FORMAT -d "2013-09-16"`
now=$start
while [[ "$now" < "$end" ]] ; do
now=`$DATE +$FORMAT -d "$now + 1 day"`
echo "$now"
done
I have played around with adding an 00:00 after the start and end times, but that did not work.
Any ideas where I am getting the output format wrong?
Code from:
https://ocroquette.wordpress.com/2013/04/21/how-to-generate-a-list-of-dates-from-the-shell-bash/
When you use the FORMAT="%m/%d/%Y %H:%M" you need quotes because it contains a space, so:
now=`$DATE +"$FORMAT" -d "$now + 1 day"`
Also, I do not think that you can compare dates like that. You might need timestamp:
date +%s

Get next Sunday after a given date (bash)

Is there any built-in function to get next Sunday (next Monday, Tuesday, etc.) from a given date using bash shell script?
For example, what is the first Sunday after 1-Sep-2014? I expect 7-Sep-2014.
I have searched the answer in google, but only found this function :
date "+%Y%m%d" -d Sun
which is to get next Sunday after today.
I fear date can't do it directly. But you can use brace expansion to get the dates for all the following 7 days and select Sunday from them:
echo '1-Sep-2014\ +'{1..7}'\ days' | xargs -n1 date -d | grep Sun
You can use this little script:
#!/bin/bash
w=$(date -d"$1" +%w)
diff=$(( (7-$w)%7 ))
date -d"+$diff day $1" +%F
saved as sun.sh then:
kent$ ./sun.sh 01-Sep-2014
2014-09-07
kent$ ./sun.sh 31-Aug-2014
2014-08-31
kent$ ./sun.sh 3-Sep-2014
2014-09-07
kent$ ./sun.sh 23-Sep-2014
2014-09-28
Note that, argument validation was not done, I just showed how to calculate the date.
It is possible to make date do all the math without using shell code for math functions or to iterate though output looking for answers. The date function can do basic math like "x + 1 days" or "x - 1 month + 2 days".
It can also output formatted strings with arbitrary values.
The trick here is to use one date invocation to output the string used to calculate your final answer. We'll input your input date and ask for what day of the week that is, but at the same time output some math to subtract that number of days. By adding 7 days first we'll end up an the next week, then by subtracting the day-of-the-week number from that we'll have the next Sunday:
INPUTDATE="20140901"
date +"%Y%m%d" -d "$(date +"%Y%m%d + 7 days - %w days" -d "$INPUTDATE")"
This will spit out your expected answer: 20140907. Of course you can change the output format using the format string on the outer date invocation.

Change datetime to timestamp in bash script

I have a json in file myTime
{
"beginTime": "2014-Mar-19 02:15:00",
"endTime": "2014-Mar-29 02:00:00"
}
I want to get beginTime and change it to timestamp.
I get beginTime by the following code:
beginTime=($(jq -r '.beginTime' myTime))
I replace Mar by 03 :
beginTime=($(echo "$beginTime" | sed -r 's/[Mar]+/03/g'))
I change it to time stamp :
date -d "$beginTime" "+%s"
I got 1395162000 , it mean only change 2014-03-19 because $beginTime give 2014-03-19 , first element of array.
so I tried another code
date -d "${beginTime[#]}" "+%s"
now I got
date: extra operand `+%s'
but this code is ok
date -d "2014-03-19 02:15:00" "+%s"
Could anyone help me?
Do not use array variables - you need to capture date and time as a single string:
beginTime=$(jq -r '.beginTime' myTime | sed 's/Mar/03/') # "2014-03-19 02:15:00"
Note that I've combined your first two commands into one.
(As an aside: regex /[Mar]+/ happens to work in this case, but is not what you intended - use /Mar/ instead. /[Mar]+/ matches any span of at least length 1 composed of any of the following characters in any sequence: M, a, r.)

shell script convert any date to milliseconds

I want to get this time in milliseconds
01/Mar/2012:09:08:00
I thought doing the following would store the new date then I can convert the date into milliseconds
time=01/Mar/2012:09:08:00
newDate=date --set="$time";
What do I need to do to get this working?
You should convert '01/Mar/2012:09:08:00' to a valid date string '01 Mar 2012 09:08:00'
$ time=01/Mar/2012:09:08:00
$ time="${time//// }"
$ time="${time/:/ }"
$ newDate=`date -d "$time" +%s000`
$ echo $newDate
1330564080000
Use Timer() method to get time then put that time in formatnumber() method as like
FormatNumber(Timer(), 2)
I have the easiest way to convert date into milliseconds:
echo $(date +'%s')

Resources