I am trying to convert month number to name, but it is giving output as current month instead of the date given in the variable.
KornShell (ksh) Code:
datep= 2013-10-22
echo $datep |printf "%(%B)T\n"
printf doesn't read from standard input, so it is assuming today's date as the default argument for the %T format; you need to provide the date as an argument instead.
printf "%(%B)T\n" "$datep"
Do it like this:
$ datep="2013-10-22"
$ date -d"$datep" "+%B"
October
As per man date,
-d, --date=STRING
display time described by STRING, not 'now'
So we get:
$ date -d"$datep"
Tue Oct 22 00:00:00 CEST 2013
Then you say you want the %B, that is, also from man date:
%B
locale's full month name (e.g., January)
So it is just a matter of using the format at the end of the string.
Other examples:
$ date -d"$datep" "+%Y" #year
2013
$ date -d"$datep" "+%F" #date
2013-10-22
$ date -d"$datep" "+%T" #time (if not given, gets the default)
00:00:00
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"
Getting the date in bash works something like this:
now=$(date)
echo "Current date: $now"
It returns:
Current date: Mon Nov 28 11:34:55 NZDT 2016
I need to store the date in a variable where the date is in a format like this:
20161128
How can I convert the date format into the format I need?
You use any format with +%
date +%Y%m%d
Output
20161127
I have a human readable time as
08-18-2016 09:18:25
I want it to be converted into epoch time using shell script.
I tried with date "+%s" but I am getting the error
date: invalid date `08-18-2016 09:32:42'
The canonical way to convert a datetime into epoch is to use:
date "+%s" # for this moment's date
date -d" some date" "+%s" # for a specific date
However, in this case the format is not valid:
$ date -d"08 18 2016 09:18:25" "+%s"
date: invalid date ‘08 18 2016 09:18:25’
You need, then, to massage the string a bit before passing it to date -d.
This converts the two first spaces into slashes:
$ sed 's# #/#;s# #/#' <<< "08 18 2016 09:18:25"
08/18/2016 09:18:25
So this works:
$ date -d"$(sed 's# #/#;s# #/#' <<< "08 18 2016 09:18:25")" "+%s"
1471504705
Or using variables:
$ nice_date=$(sed 's# #/#;s# #/#' <<< "08 18 2016 09:18:25")
$ date -d"$nice_date" "+%s"
1471504705
Thanks for the explanation fedorqui. But 1471511905 is the epoch time
for 08 18 2016 09:18:25, not 1471504705. – Mohit Rane
date -u … will print Coordinated Universal Time.
I have a date in format 'YYYYMMDDHHMMSS' and I need to convert it to Unix timestamp.
I tried to date -d '20140826225834' but I get 'invalid date' error. I asume that I would have to convert what I have ( 20140826225834 ) to accepted date and then convert it to timestamp?
Edit: I have sed this date from 2014-08-21_23.03.07 - maybe it would be easier to convert this date type
You should probably change the format of the date you get, so that date can handle it. I change it to a YYYY/MM/DD HH:MM:SS format with sed.
$ date -d"$(sed -r 's#(.{4})(.{2})(.{2})(.{2})(.{2})#\1/\2/\3 \4:\5:#' <<< "20140826225834")" "+%s"
1409086714
By pieces:
$ sed -r 's#(.{4})(.{2})(.{2})(.{2})(.{2})#\1/\2/\3 \4:\5:#' <<< "20140826225834"
2014/08/26 22:58:34
$ date -d"2014/08/26 22:58:34"
Tue Aug 26 22:58:34 CEST 2014
$ date -d"2014/08/26 22:58:34" "+%s"
1409086714
You could use PHP, since PHP's strtotime() can parse your input format:
#!/bin/bash
input="20140826225834"
output=$(php -r 'echo strtotime("'"$input"'");')
echo "$output" # 1409086714
Say I have the following format outputted for my date:
date --utc +%d.%m.%Y,\ %H:%M\ UTC
# Outputs: 12.06.2014, 09:03 UTC
How can I display the outputted date above, in another date call, in another format? I tried:
date --utc --date="12.06.2014, 09:03 UTC" +%d.%m.%Y,\ %H:%M\ UTC
but with no success (it says invalid date).
I am primarily trying to do this in order to be able to tell from an outputted date how many hours have passed (or days, or whatever time measuring unit).
Here is what the man date page says about format for the --date option:
The --date=STRING is a mostly free format human readable date string such as
"Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29 16:21:42" or even "next
Thursday". A date string may contain items indicating calendar date, time of day,
time zone, day of week, relative time, relative date, and numbers. An empty
string indicates the beginning of the day. The date string format is more
complex than is easily documented here but is fully described in the info
documentation.
Hence you can use, for example:
date --date "2014-06-12 09:03 UTC" --utc +%d.%m.%Y,\ %H:%M\ UTC
# Output: 12.06.2014, 09:03 UTC
to get what you desire.
You could get this second form easily from your first output with a sed line as follows:
sed 's/\([0-9]\{2\}\)\.\([0-9]\{2\}\)\.\([0-9]\{4\}\), \(.*\)/\3-\2-\1 \4/'
<<< '12.06.2014, 09:03 UTC'
# Output: 2014-06-12 09:03 UTC
Note that it would probably be faster to output date at ISO 8601 format in the first time for reuse, e.g. with:
date --utc +%F\ %H:%M\ UTC
# Output: 2014-06-12 10:12 UTC
I think you cannot specify input format, so you'll have to change it with another command like this:
date --utc --date="$(echo "12.06.2014, 09:03 UTC" | sed -r 's/(..).(..).(....), (..):(..) UTC/\3-\2-\1 \4:\5 UTC/')"
Also if you want to make arithmethic on this you could use +%s:
DATE1=$(date "+%s" --date="$(echo "12.06.2014, 09:03 UTC" | sed -r 's/(..).(..).(....), (..):(..) UTC/\3-\2-\1 \4:\5 UTC/')")
DATE2=$(date "+%s" --date="$(echo "17.06.2014, 08:30 UTC" | sed -r 's/(..).(..).(....), (..):(..) UTC/\3-\2-\1 \4:\5 UTC/')")
DIFF_IN_SECONDS=$(($DATE2-$DATE1))
DIFF_IN_RAW_DAYS=$(( ($DATE2-$DATE1)/86400 ))
DIFF_IN_DATES=$(( (($DATE2/86400) - ($DATE1/86400)) ))