I need to retrieve the month of a date (dd.month.year) and transform it into the digit YYYY-MM
the source month is in french, so i try :
# date --date="$(printf "01 %s" "January 2020")" +"%Y-%m"
2020-01
# LC_TIME=fr_FR date --date="$(printf "01 %s" "Janvier 2020")" +"%Y-%m"
date: invalid date '01 Janvier 2020'
# LANG=fr_FR date --date="$(printf "01 %s" "Janvier 2020")" +"%Y-%m"
date: invalid date '01 Janvier 2020'
it works when I use the month in English.
But with the month in French even by forcing the language or local environment parameters it does not recognize the month correctly.
even with date simply it still announces in us :
# LANG=fr_FR date
Sun May 17 23:45:40 CEST 2020
# LC_TIME=fr_FR date
Sun May 17 23:45:48 CEST 2020
# date
Sun 17 May 2020 11:45:53 PM CEST
any idea ?
info :
Operating System: Debian GNU/Linux 10 (buster)
Kernel: Linux 4.19.0-8-amd64
Architecture: x86-64
Although the parsing done by date is flexible and amazing at times, it doesn't handle localization (here and the array is static here). You have to build an translation array that would translate Janvier to January yourself, and then pass the result to date.
It will be easy to use an associative bash array for that:
arr=([Janvier]=January etc)
echo "${arr[Janvier]}"
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)) ))
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 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
This is a Ruby question (1.9.1)
I have the following date and time in a string:
29 Sep 2013 12:25:00.367
I first want to convert it from string to date and time and then
add 10 seconds to it and convert it back to the same string format as
above.
I wrote this code:
format = "%d %b %Y %H:%M:%S"
date_time = "29 Sep 2013 22:11:30.195"
parsed_time = DateTime.strptime(date_time, format)
puts " new date time is #{parsed_time}"
Which outputs:
new date time is 2013-09-29T22:11:30+00:00
I did not see "195". I tried format = "%d %b %Y %H:%M:%S.%3N" and this gives:
fileOpTest:34:in `strptime': invalid date (ArgumentError) from fileOpTest:34:in `<main>'
This can be done very easily using the Time class. You can add to a Time by adding seconds. Then use #strftime
t= Time.parse('29 Sep 2013 12:25:00.367')
=> 2013-09-29 12:25:00 -0400
t=t + 10
=> 2013-09-29 12:25:10 -0400
t.strftime("%d %b %Y %H:%M:%S.%3N")
=> "29 Sep 2013 12:25:10.367"