How do I capture the date & time right down to the second and store it in a variable?
Take for example if I wanted Tuesday Dec 8th 2015 1:00:20 pm the output should look like this:
130020-8-12-2015
So far I only have the date:
function backup()
{
local now="$(date +'%d-%m-%Y')"
echo $now
}
You should check out the manpage for date. Use command man date. You will find in the manpage:
%H hour (00..23)
%I hour (01..12)
%k hour ( 0..23)
%l hour ( 1..12)
%M minute (00..59)
%N nanoseconds (000000000..999999999)
%s seconds since 1970-01-01 00:00:00 UTC
%S second (00..60)
%T time; same as %H:%M:%S
%z +hhmm numeric timezone (e.g., -0400)
Amongst many other tokens.
So the following should do what you need
function backup()
{
local now="$(date +'%H%M%S-%d-%m-%Y')"
echo $now
}
date +'%H%M%S-%d-%m-%Y' would give you your desired format.
Related
EpochConverter turns a timestamp value like 1586775709496 into Monday, April 13, 2020 11:01:49.496 AM.
Unfortunately, the date tool on MacOs expects seconds, not milliseconds, and gives a wrong year:
> date -r 1586775709496
Thu Dec 2 15:24:56 CET 52252
This existing question only explains the obvious: you can divide by 1000 (cut of the trailing 3 digits) and the built-in date tool will work.
But: that is not what I am looking for. I am looking for a "straightforward" way to turn such millisecond based timestamps into "human readable" including the milliseconds. Are there ways to achieve that?
timestamp=1586775709496
ms=$(( $timestamp % 1000 ))
echo "$(date -r $(( $timestamp / 1000 )) +"%a, %b %d, %Y %H:%M:%S").$ms"
Mon, Apr 13, 2020 12:01:49.496
you can edit the date format string to get exactly the result you need.
With gnu date I believe that would be:
$ a=1586775709496
$ LC_ALL=C date -u --date=#"$((a/1000)).$(printf "%03d" $((a%1000)))" +"%A, %B %2d, %Y %H:%M:%S.%3N %p"
Monday, April 13, 2020 11:01:49.496 PM
The %3N is something that GNU date supports and it prints only milliseconds.
I guess because the last 3 characters of input are just in the output, you could just input them where they should be, removing the need for %N extension:
$ a=1586775709496;
$ LC_ALL=C date -u --date=#"$((a/1000))" +"%A, %B %2d, %Y %H:%M:%S.$(printf "%03d" $((a%1000))) %p"
I have a script that reads files with linux timestamps and I'd like to convert them to human format while keeping the timezone offsets.
Script:
[..]
UPTIME=$(cut -d" " -f1 < /proc/uptime)
SECONDS=$(date +%s)
date -d"70-1-1 + $SECONDS sec - $UPTIME sec + $TIMESTAMP sec " +"%d/%m/%Y %T"
[..]
The problem is that I have a +2h timezone offset, so that my script shows dates early by 2hours
date "+%z %Z"
+0200 IST
How can I adjust the script to use the timezone offset?
Thanks,
You can use date with -u parameter which according to the manual it displays the output in UTC.
I want to grep a file with the following date format:
Thu Apr 24
At the moment I only have date +"%d %m %Y" and that's returning 24 04 2014.
How do I format to get "Thu Apr 24"?
So I need the day month and date?
man date would suggest date +"%a %b %d"
You can try
date +"%a %b %d"
where
%a locale's abbreviated weekday name (e.g., Sun)
%b locale's abbreviated month name (e.g., Jan)
%d day of month (e.g., 01)
I want to calculate the difference in time by passing in a string HH:MM value to set the time and calculating the difference between this and the current system time.
I'm aware of date --set="2013-07-10"+%T (or something like this). But I cannot figure out how to set the time part to be the string HH:MM value that I pass in and leave the data as the current date. Then, to calculate I was trying to use the below which changes it into a timestamp value; which I found from another user:
mydate=??
date1=$($mydate +"%s") #my set date/time here
date2=$(date +"%s")
diff=$(($date2-$date1))
echo "$(($diff / 60)) minutes and $(($diff % 60))"
You can use date -d hh:mm to create a new date with a specified time.
Example:
$ date -d 11:15
Wed Jul 10 11:15:00 BST 2013
Using this, you can write the following script:
time=11:15
now=$(date +%s)
other=$(date -d $time +%s)
diff=$((other-now))
echo "$((diff / 60)) minutes and $((diff % 60)) seconds"
In a bash script, if I have a number that represents a time, in the form hhmmss (or hmmss), what is the best way of subtracting 10 minutes?
ie, 90000 -> 85000
This is a bit tricky. Date can do general manipulations, i.e. you can do:
date --date '-10 min'
Specifying hour-min-seconds (using UTC because otherwise it seems to assume PM):
date --date '11:45:30 UTC -10 min'
To split your date string, the only way I can think of is substring expansion:
a=114530
date --date "${a:0:2}:${a:2:2}:${a:4:2} UTC -10 min"
And if you want to just get back hhmmss:
date +%H%M%S --date "${a:0:2}:${a:2:2}:${a:4:2} UTC -10 min"
why not just use epoch time and then take 600 off of it?
$ echo "`date +%s` - 600"| bc; date
1284050588
Thu Sep 9 11:53:08 CDT 2010
$ date -d '1970-01-01 UTC 1284050588 seconds' +"%Y-%m-%d %T %z"
2010-09-09 11:43:08 -0500
Since you have a 5 or 6 digit number, you have to pad it before doing string manipulation:
$ t=90100
$ while [ ${#t} -lt 6 ]; do t=0$t; done
$ echo $t
090100
$ date +%H%M%S --utc -d"today ${t:0:2}:${t:2:2}:${t:4:2} UTC - 10 minutes"
085100
Note both --utc and UTC are required to make sure the system's timezone doesn't affect the results.
For math within bash (i.e. $(( and ((), leading zeros will cause the number to be interpreted as octal. However, your data is more string-like (with a special format) than number-like, anyway. I've used a while loop above because it sounds like you're treating it as a number and thus might get 100 for 12:01 am.
My version of bash doesn't support -d or --date as used above. However, assuming a correctly 0-padded input, this does work
$ input_time=130503 # meaning "1:05:03 PM"
# next line calculates epoch seconds for today's date at stated time
$ epoch_seconds=$(date -jf '%H%M%S' $input_time '+%s')
# the 600 matches the OP's "subtract 10 minutes" spec. Note: Still relative to "today"
$ calculated_seconds=$(( epoch_seconds - 600 )) # bc would work here but $((...)) is builtin
# +%H%M%S formats the result same as input, but you can do what you like here
$ echo $(date -r $calculated_seconds '+%H%M%S')
# output is 125503: Note that the hour rolled back as expected.
For MacOS users you can do the following:
$(date -v -10M +"%H:%M:%S")
Date time without a specific format:
$(date -v -10M)
For non-macOS users:
Date time without a specific format:
date --date '-10 min'