How to print date in MM/DD/YYYY formate in unix? - date

I'm new to unix and I'm trying to run print the date using:
echo "Purchase date: `date`"
but I keep getting the date in the format
Purchase date: Fri Oct 2 12:21:26 EDT 2020
but I'm trying to get it in the format of MM/DD/YYYY

A quick google search will show you lots of options but I believe you want something like: ~$ date +%m/%d/%Y
~$ 10/02/2020
%[a-z] represents formatting options.
%m - month (01..12)
%d - day of month
%Y - Full Year
'/' - Delimiter
You can see all of the date formatting options by using the help command:
~$ date --help

Related

Convert a dates from Tweet hydrator to standard date format in Google Sheets mm/dd/yyyy

I have a large dataset (close to 80,000) of tweets dated like this:
Wed Oct 05 01:20:53 +0000 2016
What script can I run to convert the dates in Google Sheets to the simple mm/dd/yyyy form?
In this case, it should be: 10/05/2016
Thanks!
If the format of the date is you mentioned is consistent, you can use the below formula (assuming the date is in cell A1)
=DATEVALUE(RIGHT(A1,4) & MID(A1,5,3) & MID(A1,9,2))
This will extract the Datevalue from the string and then you can format it to look in the mm/dd/yyyy format
Try
=arrayformula(if(A1:A="",,1*(regexextract(A1,"\d{2}")&"/"&regexextract(A1,"\D+ (\D+) ")&"/"&regexextract(A1,".* (\d+)"))))
or (with hours/minutes/seconds)
=arrayformula(if(A1:A="",,1*(regexextract(A1,"\d{2}")&"/"&regexextract(A1,"\D+ (\D+) ")&"/"&regexextract(A1,".* (\d+)"))+regexextract(A1,"\d{2}:\d{2}:\d{2}")))
and define the appropriate format
Another solution
=index(ifna(Text(1&RegexExtract(A:A,".*?\s(.*?)\s"),"MM")&"/"&RegexExtract(A:A,"\d{2}")&"/"&RegexExtract(A:A,".*\s(.*)")))
Or
=index(text(regexreplace(regexreplace(A:A,"\+0000\s",),"(.*)(\d+:\d+:\d+)\s(.*)","$1$3 $2"),"mm/dd/yyyy"))
Or
=index(text(regexreplace(A:A,"(.*\s)(\d+:.*)\+.*\s(.*)","$1$3 $2"),"mm/dd/yyyy"))

GNU date output specific time

I have a date command that outputs the last day of the previous month:
date -d"-1 day 1 $(date +%b)"
Outputs:
Sun Sep 30 00:00:00 BST 2018
How can I change the time part to be 23:59:59 on the same date?
Here is one solutions:
date -d "$(date +%b) -1.0 sec"
Notice the usage of a float and not an integer for the subtraction. This is to avoid timezone problems.
Thanks for the replies, sadly it gave me an error.
I got things working with:
EPOCH=$(date -d "-1 day 1 $(date +%b)" +%s)
END=`echo $EPOCH +86399 | bc -q`
Which gives me the EPOCH at 23:59:59 on the last day of the previous month.

How to enter a YYYYMM date in Jekyll?

How should dates be entered in Jekyll?
I would like to enter the date "August 2018" in a YAML file to be used in Jekyll. I find lots of information on how to format already-existing dates, but pretty much nothing on how to enter them.
The best I have managed to find is Date formatting, which implies that dates entered in ISO 8601 format should be valid. If I run with this, then Wikipedia explicitly states
"2004-05" is a valid ISO 8601 date, which indicates May (the fifth
month) 2004.
This implies that "August 2018" could be entered as 2018-08.
However, when I use my YAML file my_data.yml in my _data folder
date: 2018-08
then Jekyll doesn't recognize it as a date as
{{ site.data.my_data.date | time: '%B %Y' }}
outputs "2018-08" and not "August 2018".
TL;DR: Enter YYYYMM dates such as "August 2018" as Aug 2018.
Searching through the Jekyll repo, I found the date filters. The Ruby on Rails method .to_formatted_s (source) seem to be key to most of them. In the source to that method dates are written as Tue, 04 Dec 2007 00:00:00 +0000 from which I guessed that I should write Aug 2018. Doing so in my_data.yml, the code outputs the expected “August 2018”.

Extract day from non standard Unix date

I can see that UNIX easily handles a standard date format 'yyyy-mm-dd'. Date function can easily extract day form standard date format like this
date -d '2016-12-9 00:00:00' '+%d'
output: 09
But I can't figure out a way to get the same result using a non-standard date format. My date format is 'mm-dd-yyyy'.
GNU date which you are most likely using because of the -d switch, doesn't allow custom formatting of dates, unlike the FreeBSD version which supports with the -f input_fmt for converting custom date formats.
With GNU date and bit of manipulation with sed, you can achieve what you are trying to do.
Using sed converting your input date in mm-dd-yyyy to a format GNU date can understand.
customDate=$(echo "12-09-2016 00:00:00" | sed 's/\([0-9]\{2\}\)\-\([0-9]\{2\}\)\-\([0-9]\{4\}\) \(.*\)/\3-\1-\2 \4/')
Now customDate will have the date in the format 2016-12-09 00:00:00 format which you can pass to -d flag to get the required date. i.e.
date -d "$customDate" '+%d'
09
Or as a fancy "one-liner" they call it these days, without the use of the temporary variable.
date -d "$(echo "12-09-2016 00:00:00" | sed 's/\([0-9]\{2\}\)\-\([0-9]\{2\}\)\-\([0-9]\{4\}\) \(.*\)/\3-\1-\2 \4/')" '+%d'
09

Groovy date parse issue

date.parse() method of groovy detects date DD and year yyyy correctly but is unable to detect the month as mmm.. As in
println new Date().parse("DD-MMM-yyyy", '22-MAR-2011')
yields output as
Sat Jan 22 00:00:00 GMT+05:30 2011
Why is the month march as MAR picked up as Jan? What can I do to make it detect the month in mmm format?
The problem is actualy that you are using DD - that means day in year
Correct way:
println new Date().parse("dd-MMM-yyyy", '22-MAR-2011')
Quick tip when formatting dates try using the reverse and see what comes out:
println new Date().format("dd-MMM-yyyy")
Groovy uses SimpleDateFormat under the hood but that's not that important since most date libraries use the same format conventions.