short date in bash PS1 prompt - bash

You can use \d in the your PS1 confuration to display a long date ie. Tues 18 May, but how can I get it to display it in a format like 18.05.2012 for example?

Try including \D{%d.%m.%Y}. You can use any time format supported by strftime(3).

Try this:
PS1="\$(date +%d.%m.%Y) > "
export PS1

Use \D{format} where format is a strftime format code.
For example:
$ export PS1='\D{%d.%m.%Y}$ '
08.02.2012$

Rather than telling the shell to execute the date command each time, you would rather use the built-in format. Hence you can also use (though a little variation from what you have asked)
\D{%F %T}
to give you date and time.
date in format : YYYY-MM-DD
and time in format hh:mm:ss.

you can try this that display time:
$ PS1="\n\t \u#\h:\w# "
08:18:57 user#localhost:/home/user#

I'm not sure how to apply this to PS1 specifically (maybe someone can edit this question to replace this part with the best working bit from other questions).
BUT you can get short date formatted according to your current locale with:
date +%x

Related

How to generate date in this format (2022-04-29T06:07:28.158Z) in linux

Looking for solution to generate date in the below format:
2022-04-29T06:07:28.158Z
Have to use in bash script.
It looks like you want to have the milliseconds included (rounded to three places), so add %3N to pmf's suggestion from the comments:
date -u +%FT%T.%3NZ
Example output:
2022-04-29T16:23:39.905Z

zsh on macOS date modify output of given date (without script)

I‘m aware of date +%u to get the day of the week for today.
I‘d like to get that integer for any arbitrary date i input - if possible in the format I choose (e.g. %YYmmdd)
ok, found it finally:
date -j -f %Y%m%d +%u 20200910
this is, because date on macOS doesn't take a switch for putting in custom date (fyi for those folks, how try to make -v work, like me^^)
in addition, -f affects only input format (it's literally the second word in the man page, but I managed to overlook more than once)
-j is needed to use -f without setting the date.
hope this will spare someone time in the future ;)
edit:
it seems to be important, to specify input format before output format (see comment from #chepner below)
(also be careful with quotes)
$ date +%u -d "2020-09-10"
4

How to convert a custom date format to an alternate format with the gnu 'date' command?

I have a string with a custom date format written in Japanese: 2013年1月8日 20時19分. With osx's date command, I can convert this to some other format with the following command:
timestamp="2013年1月8日 20時19分"
date -j -f "%Y年%m月%d日 %H時%M分" "$timestamp" +"%F %R"
While searching I found this question helpful, but it ultimately did not help when it came to gnu date. The command gdate -d "2013年1月8日 20時19分" +"%F %R" fails saying that it does not understand the date format. The -d flag allows some simple formats, but how I can apply a more radical custom format and convert the date? Am I stuck with parsing the string myself with string manipulation in shell?
Any help would be greatly appreciated.
You probably will have to tinker with some environment variables (ex: TZ, LC_ALL, etc).
See this page showing you most of the common environnement variables, and their meanings
To try some: you can force the value to change just for the duration of the following command by putting them on the same line, before the command itself:
TZ=.... LC_LANG=..... date -d .......
will invoke date -d .... with the 2 environment variables TZ and LC_LANG set to a temporary value.
Some interresting pointers (I can't right now tell if there is a program that will take as input any locale's date and translate that to the relevant Epoch or Unix Timestamp... BUt there seems to be hope following that (looking quite standard) trail of online docs:
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/date.html
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_02
which talks, amongst many other, about:
LC_TIME
This variable shall determine the locale category for date and time formatting information. It affects the behavior of the time functions in strftime(). Additional semantics of this variable, if any, are implementation-defined.
Which points to: http://pubs.opengroup.org/onlinepubs/9699919799/functions/getdate.html
which says in the middle:
The match between the template and input specification performed by getdate() shall be case-insensitive.
The month and weekday names can consist of any combination of upper and lowercase letters. The process can request that the input date or time specification be in a specific language by setting the LC_TIME category (see setlocale ).
and points to: http://pubs.opengroup.org/onlinepubs/9699919799/functions/setlocale.html
... I wish you an happy reading ! Let us know what you find!
I finally figured this out with the aid of the coreutils mailing list. However, the example they give there uses perl. They specifically rely on the POSIX::strptime module, which does not come with a standard installation of perl. Therefore, I solved this with python, which has the time module. This module should be available in most installations of python2 and python3.
Here's how to use it programmatically:
Python solution:
$ timestamp='2013年1月8日 20時19分'
$ time_format='%Y年%m月%d日 %H時%M分'
$ gdate -u -R -d "$(python -c 'import sys; from time import strptime; t=strptime(sys.argv[-1],"'$time_format'"); print("%d-%d-%d %d:%d"%(t.tm_year,t.tm_mon,t.tm_mday,t.tm_hour,t.tm_min))' $timestamp)"
Tue, 08 Jan 2013 20:19:00 +0000
This works with both python2 and python3. You can substitute any timestamp and format as you like.
Perl solution
To document the answer given to me on coreutils, the perl solution is this (requires POSIX::strptime)
$ gdate -u -R -d "$(perl -MPOSIX::strptime -le 'my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = POSIX::strptime("$ARGV[0]","%Y年%m月%d日 %H時%M分");$year+=1900;$mon+=1;printf("%04d-%02d-%02d %0d:%02d\n",$year,$mon,$mday,$hour,$min);' "2013年1月8日 20時19分")"

shell script to get time ('%T'), how to reformat output (- instead of :)

I use this shell script inside applescript:
set Date to (do shell script "date '+%Y-%m-%d'-'%T'")
return Date
It returns this:
"2012-02-15-16:01:05" #[year, month, day and time]
Is there some way to reformat the time to 16:01:05 to 16-01-05. (Obviously I could do a regex on the output, but that will be messy.)
A typical solution using the standard date formatting params is
date '+%Y-%m-%d-%H-%M-%S'
Not sure why you have two separate strings in your original i.e. '+%Y-%m-%d'-'%T'
I hope this helps.
You can separate the %T to %H-%M-%S then it shows the time in 00-00-00 format

Cshell Script date issue

Is there a way to add date in the name of the file... we can add current date in this manner date '+%Y%m%d' but i want to add "filename_date_1-2-2011_thru_31-2-2011.txt" Is it possible to do that??????????
If you have a sufficiently advanced version of the date command and you know a Unix timestamp for the start and end dates, then you can use:
(MacOS X) date -r 1234567890 "+%d-%m-%Y" to obtain 13-02-2009.
(GNU) date -d 2/13/2009 "+%d-%m-%Y" to obtain 13-02-2009 again.
If you don't want the leading zeroes on the day of month, then you need to use '%e` instead of '%d' on Linux (but that puts a space in place of the zero). It is not clear that there's a format specifier for day-of-month without a leading zero on MacOS X; nor is it clear that there's a way to format month of year as a single-digit number for January to September on either platform.
You get the format into your C shell script using back-ticks around the date commands.
Consider reading Csh Programming Considered Harmful and heeding its advice.

Resources