🖥️date

➡️This is a command-line reference manual for commands and command combinations that you don’t use often enough to remember it. This cheatsheet explains the date command with important options and switches using examples.

▁ ▂ ▃ ▄ ꧁ 🔴☠ COMMANDLINE-KUNGFU WITH CHEATSHEETS ☠🔴꧂▅ ▃ ▂ ▁

#                ██████╗  █████╗ ████████╗███████╗
#                ██╔══██╗██╔══██╗╚══██╔══╝██╔════╝
#                ██║  ██║███████║   ██║   █████╗  
#                ██║  ██║██╔══██║   ██║   ██╔══╝  
#                ██████╔╝██║  ██║   ██║   ███████╗
#                ╚═════╝ ╚═╝  ╚═╝   ╚═╝   ╚══════╝
                                                 
                                                 
# Print date in format suitable for affixing to file names
date +"%Y%m%d_%H%M%S"

# Convert Unix timestamp to Date(Linux)
date -d @1440359821

# Convert Unix timestamp to Date(Mac)
date -r 1440359821

#==============================#
# CMD DATE
#==============================##==============================#

# german Date 
date +"%d-%m-%Y"

date +'%j'
# Day of the year

date -u
# Current UTC time

date -d @192179700
# Turn a Unix epoch time back into a human readable date. Feature of GNU date

date +'%j'
# Day of the year

date -s 1970-01-01
# If you set your production server his time to this date, you will get fired.

date +%j
# Print the day of the year. Can be useful with things like find.

date -d "now 30 years ago" +%s
# In GNU date, show the epoch time 30 years ago. Interestingly, @498765432 was at 17:57:12 GMT that day.

date -d"2009-12-15 +2600 days"||date -v2009y -v12m -v15d -v+2600d; play -n synth sin 2600 trim 0 1 vol .05 
# Today is day

TZ=UTC date
# Force a timezone for the date by setting the TZ variable. In this case, get UTC time.

TZ=UTC-1 date +"(%S + (%M * 60) + (%H * 3600))/86.4" | bc
# Calculate the current Swatch Beat time. /me ducks

time cat
# Instant stopwatch. Run to start timer and press Ctrl-D to stop it. "real" time is the elapsed time.

# 1) Example 1 -  displaying current date and time in Unix
# This is simplest use of unix date command. just type date in the command prompt and it will show you the current date and time in the timezone your machine is.

date
     Thu Jul 14 14:31:35 MPST 2011

# 2) Example 2 - displaying Julian date in Unix
# Date command examples in Unix and Linux
# most of java stock trading application running under Linux use Julian date format to store date information. Julian dates are made of 5 digits 2 of which represent year and 3 or last digit represent day of year. for example 14th July 20111 will be represented by 11195 in Julian format because 14th July is the 195th day of the year. we can get Julian date by using date command in unix as shown in the below example of unix date command:

# Converting current date into Julian format in unix
date +%j
     195

# Converting a specific date into Julian format in unix
date -d "2011/07/13" +%j
     194

# this way you can get yesterdays Julian date or tomorrows Julian date in Unix though I have heard that "-d" option of date is not supported in AIX so this may not work in AIX version of Unix.

# 3) Example 3 - displaying current date in various format in unix
# Unix date command is very powerful and can show date in various format. in this section of unix date tutorial we will convert date into some commonly used date format in unix:

# Showing date in YYYYMMDD format in Unix
date +%Y%m%d
     20110714

# here %Y shows year in four digit ( you can use %y (small letter) for displaying just 2 digit of year)  %m  shows two digit of month as 01..12 and %d represent day of month.

# Showing date in DD/MM/YYYY format in unix
date +%d\/%m\/%Y
     14/07/2011

# here again %d is for day %m is for month and %Y is for four digit year if you want to show 2 digit use small case y e.g. %y

# Showing date in DD-MM-YY format in unix
date +%d\-%m\-%Y
     14-07-2011

# quite similar to above approach just chaged the "/" to "-", you can change it to any character as per your wish and until unix accept it.

# displaying date in MM/DD/YYYY format in Unix
# what do you guys think would it be difficult no right ? just exchange place of "%m" and "%d" and you are ready as shown in below example of date command in unix

date +%m\/%d\/%Y
     07/14/2011

# displaying date in YYYY-MM-DD format is exactly similar to above approach just change the order of year and month.

# 4) How to find date 7 days before current date in Unix
# This is really nice of gnu version of unix date command you can easily get whats the date 4 days before or 5 days before by using date command in unix. here is example:

# date after 7 days in unix
date -d "-7 days"
     Thu Jul  7 14:54:36 MPST 2011

# by using this way you can find any date which is any days apart from a given current date in unix

# 5) How to get date 7 days after current day in Unix and Linux
# As shown above you can get the date after 7 days in unix by using gnu version of date command in unix, here is an example of getting date 7 days after current date:

date -d "7 days"
     Thu Jul 21 14:54:15 MPST 2011

# this way you can find any date which is 7 days past of a given current date in unix

# 6) Setting date time into unix
# setting date is easy though I dont recommend to do it if you have unix admins. Also changing date requires root access which you might not have with you anyway we can use unix date command to change the date, just provide new date in the specified format and your current date will be set to new date.

# format of setting date time into unix

# date mmddhhmi

 where mm = month
       dd = day of month
       hh = hour
       mi = minute
                     
# Example of changing date time in unix

date 07240222
     Sun Jul 24 02:22:00 MPST 2011

# above example of date command will set the current date to 24th July and time to 02:22, we dont provide year information and date uses current year by default.

# 7) How to find timezone by using unix date command - Example
# unix date command can display timezone related information also. it provides following option to display timezone
     %:z  +hh:mm numeric time zone (e.g., -04:00)
     %::z  +hh:mm:ss numeric time zone (e.g., -04:00:00)
     %:::z  numeric time zone with : to necessary precision (e.g., -04, +05:30)
     %Z   alphabetic time zone abbreviation (e.g., EDT)

date +%Z  (this will display timezone in common format e.g. EST, GMT)
     MPST

date +%z  (this option of unix date command will display timezone respective of GMT)
     +0800

# 8) How to show day of week in Unix using date command - Exmaple
# you can show current date, day of week name using unix date command  by using options "%a" and "%A". small case is used to display day in short format while capital one is used to display full name of day. see the below example of date command in unix for display day of week :

date +%a
     Thu

date +%A
     Thursday

# 9) Date command in Unix for showing current time in Various format
# time is another important information which we can get from unix date command. by default date command in unix display both date and time information but we can use various options provided by date command to display only time information. lets see some example of showing time using unix date command:

# 10) Date to command for showing time in format HH:MM:SS
# As shown above on showing date in different format in unix you can also display time on different format by applying same approach. you just need to remember following time related options of unix date command

                             %H   hour (00..23)
                             %M   minute (00..59)
                             %S   second (00..60)

# thats all for now guys I hope you will be more comfortable with date command in unix after going through and trying these examples. you can get more information about date command in unix by using "man date" or "date --help" which will also list all the options supported by unix date command.

# Tip: just remember that unix date command is mostly used inside bash script to get the date information , so its worth mentioning here how to execute date command from bash script, you just need to specify date command in back quotes . see the example bash script:

cat unixdate.sh
#!/bin/bash

DATE=`date`
echo "Current date: $DATE"

./unixdate.sh
# Current date: Sun Jul 24 02:30:50 MPST 2011

# So that was all about different examples of date command. We have seen how we can format date in Unix, how we can convert date into different timezone in Unix, how we can get current date and time inside bash script and several other examples of date command. Let me know if you have any other usage of date command which is not covered here.

date -d @1500000000 
# A Unix epoch of epic proportions is this week. Party time! I'll be in Rome and could meet up with local fans there.

date -d @1539217802 
# Using GNU date you can convert a Unix epoch time back into a human readable date by prefixing it with @

date -d @$((2**31-1)) 
# You heard it here first. Less than 20 years to go before 32-bit epochtime runs out. Make plans! 

date1=$(date +%Y-%m-%d -d "1 day ago") 
date2=$(date +%Y-%m-%d -d "2 days ago") 
date3=$(date +%Y-%m-%d -d "3 days ago")

date -d @728737200 
# Turn a Unix epoch time back into a human readable date. This is a not widely known feature of the GNU date command.

date +%Y%m%d_%H%M%S.%2N 
# In GNU date, you can use %xN to specify that you only want 'x' decimal places of the nanoseconds instead of the full 9 places.

# This Sunday, April 14th is Unix Epoch Day 18000! Happy Anniversary!
TZ=GMT echo $(( $( date +%s ) / 86400 )) 

date +%5Y-%m-%d 
# Members of The Long Now Foundation (@longnow) will be happy to know that in GNU date you can force displaying a 5 digit year by using %5Y in the output format.

# This Sunday, April 14th is Unix Epoch Day 18000! Happy Anniversary!
TZ=GMT echo $(( $( date +%s ) / 86400 )) 

##########################################
NOW=$(date +"%m-%d-%Y")
PAST=$(date --date="1 days ago" +"%m-%d-%Y")
pid=$$
#zufall=$RANDOM
#tmpdir=/var/tmp/primeconfig/primeconfig.$pid.$zufall.maildir
tmpdir=/var/tmp/primeconfig/$NOW
tmpdirpast=/tmp/primeconfig/$PAST
###################################

#!/bin/bash
# Script to generate a unique ID based on date
# First 14 characters are based on the current time stamp in format YYYYMMDDHHMMSS, followed by an underscore and a random set of four alphanumeric characters. I'm not sure how likely this method is to generate duplicate IDs, but would guess quite unlikely. For a quick test, generated 5K four character sets and saw zero duplicates, so I think the chances of duplicates generated within one second are very low, unless /dev/urandom on your system is not very random. Example output: 20101230193212_pOiF

date=$(date +%Y%m%d%H%M%S)
rand=$(cat /dev/urandom | tr -cd [:alnum:] | head -c 4)
ID=$date"_"$rand

######################################
# Do something every few seconds/minutes/hours in a shell script
# In this case "something" is a command that prints current date and time.

#Every 5 seconds:
while true; do
  if [[ $(date +%S) =~ (0$|5$) ]] ; then
    echo Current time is ... $(date)
    sleep 1
  fi
done

		Current time is ... Thu Jul 28 15:12:20 PDT 2011
		Current time is ... Thu Jul 28 15:12:25 PDT 2011
		Current time is ... Thu Jul 28 15:12:30 PDT 2011
		Current time is ... Thu Jul 28 15:12:35 PDT 2011
		Current time is ... Thu Jul 28 15:12:40 PDT 2011
		Current time is ... Thu Jul 28 15:12:45 PDT 2011
		Current time is ... Thu Jul 28 15:12:50 PDT 2011

#Every 10 seconds:
while true; do
  if [[ $(date +%S) =~ (0$) ]] ; then
    echo Current time is ... $(date)
    sleep 1
  fi
done

		Current time is ... Thu Jul 28 15:13:00 PDT 2011
		Current time is ... Thu Jul 28 15:13:10 PDT 2011
		Current time is ... Thu Jul 28 15:13:20 PDT 2011
		Current time is ... Thu Jul 28 15:13:30 PDT 2011
		Current time is ... Thu Jul 28 15:13:40 PDT 2011
		Current time is ... Thu Jul 28 15:13:50 PDT 2011

#Every 15 seconds:
while true; do
  if [[ $(date +%S) =~ (00|15|30|45) ]] ; then
    echo Current time is ... $(date)
    sleep 1
  fi
done

		Current time is ... Thu Jul 28 15:14:15 PDT 2011
		Current time is ... Thu Jul 28 15:14:30 PDT 2011
		Current time is ... Thu Jul 28 15:14:45 PDT 2011
		Current time is ... Thu Jul 28 15:15:00 PDT 2011

#Every 20 minutes:
while true; do
  if [[ $(date +%M) =~ (00|20|40) ]] ; then
    echo Current time is ... $(date)
    sleep 60
  fi
done

		Current time is ... Thu Jul 28 15:20:00 PDT 2011
		Current time is ... Thu Jul 28 15:40:00 PDT 2011
		Current time is ... Thu Jul 28 16:00:00 PDT 2011
		Current time is ... Thu Jul 28 16:20:00 PDT 2011
###########################################################################################################

# Create file text with ISO Date name. (MAC OS CATALINA / BASH) - Created, tested on MAC OS CATALINA Darwin Kernel Version 19.0.0: Thu Oct 17 16:17:15 PDT 2019; root:xnu-6153.41.3~29/RELEASE_X86_64 x86_64 bash.
touch `date +%Y-%m-%dT%H:%M:%S%z`.txt
    # Sample output
	    # 2019-10-13T16:40:12+0200.txt			
	    # 2019-10-13T16:44:01+0200.txt
	    # 2019-10-13T16:47:13+0200.txt

# Console clock - You will see it on the corner of your running terminal.
while sleep 1; do tput sc; tput cup 0 $(($(tput cols)-29)); date; tput rc; done &
    # Sample output
	    # Sun Oct 20 05:48:44 JST 2019

# Countdown Clock - I find the other timers are inaccurate. It takes some microseconds to perform the date function. Therefore, using date/time math to calculate the time for us results in millisecond accuracy. This is tailored to the BusyBox date function. May need to change things around for GNU date function.
let T=$(date +%s)+3*60;while [ $(date +%s) -le $T ]; do let i=$T-$(date +%s); echo -ne "\r$(date -d"0:0:$i" +%H:%M:%S)"; sleep 0.3; done
    # Sample output
	    # 00:03:00

# Poor man's ntpdate
date -s "$(curl -sD - www.example.com | grep '^Date:' | cut -d' ' -f3-6)Z"

# Print the DATE in ISO format.(MAC OS CATALINA / BASH) - Print date in iso format. Created and tested on MAC OS CATALINA. Darwin Kernel Version 19.0.0: Thu Oct 17 16:17:15 PDT 2019; root:xnu-6153.41.3~29/RELEASE_X86_64 x86_64 bash
date +%Y-%m-%dT%H:%M:%S%z
    # Sample output
	    # 2019-10-13T15:49:45+0200
	    # 2019-10-13T15:51:31+0200

# Create backup copy of file, adding suffix of the date of the file modification (NOT today its date) - If your `date` command has `-r` option, you don't need `stat`
cp file{,.$(date -r file "+%y%m%d")}

# Create backup copy of file, adding suffix of the date of the file modification (NOT today's date)
cp file{,.$(date -d @$(stat -c '%Y' file) "+%y%m%d")}

# Create backup copy of file, adding suffix of the date of the file modification (NOT today's date) - When I go to change a configuration file I always like to make a backup first. You can use "cp -p" to preserve the modification time, but it gets confusing to have file.prev, file.prev2, etc. So I like to add a YYMMDD suffix that shows when the file was last changed. "stat -c %Y" gives you the modification time in epoch seconds, then "date -d @" converts that to whatever format you specify in your "+format" string.
cp file file.$(date -d @$(stat -c '%Y' file) "+%y%m%d")
    # Sample output
	# ls -l
	    # -rw-r--r-- 1 root root 0 Dec 31  2018 file

cp -p file file.$(date -d @$(stat -c '%Y' file) "+%y%m%d")
	# ls -l
	    # -rw-r--r-- 1 root root 0 Dec 31  2018 file
	    # -rw-r--r-- 1 root root 0 Dec 31  2018 file.181231
 

# Thinking about starting #100daysofcode but also wondering when that'd be finished? The `date` command on Linux systems may help with that...
date -d "+100 days" +'%Y-%m-%d'
	#	 2020-08-05

#==============================##==============================#
# CMD date						       #
#==============================##==============================#
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

  █║▌│║█║▌★ KALI ★ PARROT ★ DEBIAN 🔴 PENTESTING ★ HACKING ★ █║▌│║█║▌

              ██╗ ██╗ ██████╗  ██████╗ ██╗  ██╗███████╗██████╗
             ████████╗██╔══██╗██╔═══██╗╚██╗██╔╝██╔════╝██╔══██╗
             ╚██╔═██╔╝██║  ██║██║   ██║ ╚███╔╝ █████╗  ██║  ██║
             ████████╗██║  ██║██║   ██║ ██╔██╗ ██╔══╝  ██║  ██║
             ╚██╔═██╔╝██████╔╝╚██████╔╝██╔╝ ██╗███████╗██████╔╝
              ╚═╝ ╚═╝ ╚═════╝  ╚═════╝ ╚═╝  ╚═╝╚══════╝╚═════╝

               █║▌│║█║▌ WITH COMMANDLINE-KUNGFU POWER █║▌│║█║▌

░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░