When using email.date.to_s
I receive a date in this format
Wed, 3 Jun 2015 14:57:46 -0700
but I want it to be in this format
06/03/15
You can use strftime to format the string:
Time.now.strftime("%m/%d/%y")
#=> 09/16/15
Related
I have a String variable (datetime with GMT offset) in the following format.
How can I convert this to a MST in bash shell script?
Input GMT :- 08/Sep/2020:11:38:01 +0000
Output MST :- 08/Sep/2020:04:38:01 -0700
We can get the offset like this
offset=$(date +%-z)
I dont want to again convert into date from string and then use offset and minus offset to reach MST.Is there a way to convert it in a better way?
Convert 08/Sep/2020:11:38:01 +0000 with bash to 08 Sep 2020 11:38:01 +0000:
gmt="08/Sep/2020:11:38:01 +0000"
gmt="${gmt//\// }" # replace all / with spaces
gmt="${gmt/:/ }" # replace first : with space
Then use it with GNU date:
TZ="MST" date --date="$gmt" +'%d/%h/%Y:%H:%M:%S %z'
Output:
08/Sep/2020:04:38:01 -0700
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]}"
I have a date in string format "05/11/19" I used Date.parse, but when I use strftime("%d %b %Y"), it parsed the date as 19 Nov 2005. Is there another way to parse my string to date without messing up the day and year fields?
You can pass a format String when parsing:
require 'date'
datestring = '05/11/19' # 05 Nov 2019
date = Date.strptime(datestring, '%d/%m/%y')
puts date.strftime('%d %b %Y') # => 05 Nov 2019
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 want to convert a date like
Sat Dec 31 22:42:58 CET 2011
to a valid rss date which normaly looks like
Mon, 06 Sep 2009 16:45:00 +0000
Seconds should always be 00. Is there anyway to do so? Besides it would be could to find out the current as a valid rss but that's optional ;)
Update
With your new sample input, you don't even need to reformat. It's as simple as
$ time="Sat Dec 31 22:42:58 CET 2011"; date -Rd "$time"
Sat, 31 Dec 2011 13:42:58 -0800
The trick is to reformat your date into a string that date can understand and take as input. In this case it is 2011-12-31 13:37. I'm using awk to do this, but there are number of different utilities that will suffice.
#!/bin/bash
time="12-31-11 13:37"
date -Rd "$(awk -F'[- ]' '{printf("20%s-%s-%s %s\n", $3,$1,$2,$4)}' <<<"$time")"
Output
$ time="12-31-11 13:37"; date -Rd "$(awk -F'[- ]' '{printf("20%s-%s-%s %s\n", $3,$1,$2,$4)}' <<<"$time")"
Sat, 31 Dec 2011 13:37:00 -0800