Bash How to format a date - bash

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)

Related

Capture Time Right Down to the Second

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.

Ruby Time.strptime vs Date.strptime

When parsing "some dates" (in Ruby) with Time.strptime and Date.strptime have different behaviour.
For example if we try to convert "30 Feb" (a date that does not exist), we have:
Time.strptime('30 Feb 2015', '%d %b %Y') # will result in this date: 2015-03-02
Date.strptime('30 Feb 2015', '%d %b %Y') # ArgumentError: invalid date
At the same time trying to parse "32 Feb" results in error for both classes.
Time.strptime('32 Feb 2015', '%d %b %Y') # ArgumentError: invalid strptime format - `%d %b %Y'
Date.strptime('32 Feb 2015', '%d %b %Y') # ArgumentError: invalid date
What is the reason for the different behaviour? Why Time "attempts to adjust" an invalid date?
OK, I did a bit more digging and found a couple of "issues" reported on this topic: https://bugs.ruby-lang.org/issues/9549 and the main one: https://bugs.ruby-lang.org/issues/10588
It seams that Time behaves this way for a reason. As Akira Tanaka says:
Invalid date/time is difficult to determine.
It is almost impossible by application because it depends various factors:
Month, leap year, summer time, leap seconds, time zone definition change.
Sometimes application needs a Time object near given year/month/day/hour/minute/second.
So Time tries to compensate possible 'almost correct' times. That's why it successfully parses this:
Time.strptime('29 Mar 2015 3:30:00 +02000', '%d %b %Y %T %z') to
2015-03-29 04:30:00 +0300 (3:30 is invalid time on 29th March 2015 because of daylight saving time, from 3:00 the clock moves to 4:00)

converting month number to date using KornShell

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

Is there Shell script date Format (dd month year time) (29 Oct 2013 05:26:30)

I want the date format in shell script as (dd month year time)
Example:
29 Oct 2013 05:26:30
Please can anyone help me in solving this.
Like this:
$ date "+%d %b %Y %T"
29 Oct 2013 10:45:08
From man date:
%d day of month (e.g., 01)
%b locale's abbreviated month name (e.g., Jan)
%Y year
%T time; same as %H:%M:%S

localized output for `date` command

How can I localize output of date command in MacOSX bash environment?
for example, I want to localize following in ru_RU locale:
$ date +"%d %B %Y"
07 April 2011
Could you try this?
LC_ALL=ru_RU date +"%d %B %Y"
I don't have ru_RU installed, so I show it with de/us:
env LC_ALL=en_US.utf8 date +"%d %B %Y" -d +2months
> 07 June 2011
env LC_ALL=de_DE.utf8 date +"%d %B %Y" -d +2months
> 07 Juni 2011

Resources