Date: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(18 intermediate revisions by the same user not shown)
Line 4: Line 4:
=Internal=
=Internal=
* [[Linux#Commands|Linux Commands]]
* [[Linux#Commands|Linux Commands]]
* [[gdate]]


=Overview=
=Overview=


{{{
<syntaxhighlight lang='bash'>
date +'%m/%d/%y %H:%M:%S'
</syntaxhighlight>


      date +'%m/%d/%y %H:%M:%S'
Output:


}}}
03/07/11 16:07:17


Output
Double quotes also work:


{{{
<syntaxhighlight lang='bash'>
   
date +"%m/%d/%y %H:%M:%S"
      03/07/11 16:07:17
</syntaxhighlight>


}}}
This also works:


!!!AM/PM
<syntaxhighlight lang='bash'>
date "+%m/%d/%y %H:%M:%S"
</syntaxhighlight>


{{{
=Output Patterns=


      date +'%m/%d/%y %I:%M:%S %p'
%s seconds since epoch (?)


}}}
==Time Zone==


!!!Year
+%z
 
-0700
 
=AM/PM=
 
<pre>
date +'%m/%d/%y %I:%M:%S %p'
</pre>
 
=Year=


Short year (14) %y
Short year (14) %y
Line 35: Line 50:
Long year (2014) %Y
Long year (2014) %Y


!!!File Name Format
=Month=
 
%m
 
=Hour=
 
24-hour hour: %H


{{{
=Displaying Date in a Different Timezone=
      date +'%y.%m.%d-%H.%M.%S'
}}}


Configure the desired timezone by setting up the TZ environment variable:


!!!Setting date & time
<syntaxhighlight lang='bash'>
TZ="America/Los_Angeles"
</syntaxhighlight>


{{{
To list available timezones, either list files in /usr/share/zoneinfo or run:
<syntaxhighlight lang='bash'>
timedatectl list-timezones
</syntaxhighlight>


    date 08311650
=File Name Format=
}}}
 
date +'%y.%m.%d-%H.%M.%S'
 
or
 
date +'%Y.%m.%d'
 
=Setting Date and Time=
 
<pre>
date 08311650
</pre>


sets the date for 08/31 current year, time 16:50.
sets the date for 08/31 current year, time 16:50.


=Getting the Time in Seconds=
date '+%s'
=Elapsed Time in Seconds=
<syntaxhighlight lang='bash'>
local t0 t1 elapsed
t0=$(date '+%s')
sleep 2
t1=$(date '+%s')
elapsed=$(("${t1}"-"${t0}"))
echo "elapsed seconds: ${elapsed}"
</syntaxhighlight>


!!!Elapsed Time in Seconds
=ISO 8601 Format=


Also see [expr]
date "+%Y-%m-%dT%H:%M:%S%z"


__Referenced by:__\\
Also see: {{Internal|Gdate#ISO_8601_Date_Format|gdate ISO 8601 Format}}
[{INSERT com.ecyrd.jspwiki.plugin.ReferringPagesPlugin WHERE max=20, maxwidth=50}]

Latest revision as of 21:05, 15 March 2024

External

Internal

Overview

date +'%m/%d/%y %H:%M:%S'

Output:

03/07/11 16:07:17

Double quotes also work:

date +"%m/%d/%y %H:%M:%S"

This also works:

date "+%m/%d/%y %H:%M:%S"

Output Patterns

%s seconds since epoch (?)

Time Zone

+%z
-0700

AM/PM

date +'%m/%d/%y %I:%M:%S %p'

Year

Short year (14) %y

Long year (2014) %Y

Month

%m

Hour

24-hour hour: %H

Displaying Date in a Different Timezone

Configure the desired timezone by setting up the TZ environment variable:

TZ="America/Los_Angeles"

To list available timezones, either list files in /usr/share/zoneinfo or run:

timedatectl list-timezones

File Name Format

date +'%y.%m.%d-%H.%M.%S'

or

date +'%Y.%m.%d'

Setting Date and Time

date 08311650

sets the date for 08/31 current year, time 16:50.

Getting the Time in Seconds

date '+%s'

Elapsed Time in Seconds

local t0 t1 elapsed
t0=$(date '+%s')
sleep 2
t1=$(date '+%s')
elapsed=$(("${t1}"-"${t0}"))
echo "elapsed seconds: ${elapsed}"

ISO 8601 Format

date "+%Y-%m-%dT%H:%M:%S%z"

Also see:

gdate ISO 8601 Format