{"id":1099,"date":"2012-05-30T09:08:56","date_gmt":"2012-05-30T09:08:56","guid":{"rendered":"http:\/\/blog.designed79.co.uk\/?p=1099"},"modified":"2012-05-30T09:08:56","modified_gmt":"2012-05-30T09:08:56","slug":"how-to-format-date-for-display-or-to-use-in-a-shell-script","status":"publish","type":"post","link":"https:\/\/blog.designed79.co.uk\/?p=1099","title":{"rendered":"How to format date for display or to use in a shell script"},"content":{"rendered":"<div>\n<p>Q. How do I format date to display on screen on for my scripts as per my requirements?<\/p>\n<\/div>\n<div>\n<p>A. You need to use standard date command to format date or time for output or to use in a shell script.<\/p>\n<p>Syntax to specify format<br \/>\n<strong>date\u00a0+FORMAT<\/strong><\/p>\n<h3>Task: Display date in mm-dd-yy format<\/h3>\n<p>Type the command as follows:<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">$ date +&quot;%m-%d-%y&quot;<\/div><\/div>\n<p>Output:<\/p>\n<pre>02-27-07<\/pre>\n<p>Turn on 4 digit year display:<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">$ date +&quot;%m-%d-%Y&quot;<\/div><\/div>\n<p>Just display date as mm\/dd\/yy format:<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">$ date +&quot;%D&quot;<\/div><\/div>\n<h3>Task: Display time only<\/h3>\n<p>Type the command as follows:<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">$ date +&quot;%T&quot;<\/div><\/div>\n<p>Output:<\/p>\n<pre>19:55:04<\/pre>\n<p>Display locale\u2019s 12-hour clock time<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">$ date +&quot;%r&quot;<\/div><\/div>\n<p>Output:<\/p>\n<pre>07:56:05 PM<\/pre>\n<p>Display time in HH:MM format:<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">$ date +&quot;%H-%M&quot;<\/div><\/div>\n<h3>How do I save time\/date format to a variable?<\/h3>\n<p>Simply type command as follows at a shell prompt:<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">$ NOW=$(date +&quot;%m-%d-%Y&quot;)<\/div><\/div>\n<p>To display a variable use\u00a0<a href=\"http:\/\/www.cyberciti.biz\/faq\/how-to-write-output-to-terminal\/\">echo \/ printf command<\/a>:<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">$ echo $NOW<\/div><\/div>\n<p>Sample shell script:<\/p>\n<pre>#!\/bin\/bash\r\nNOW=$(date +\"%m-%d-%Y\")\r\nFILE=\"backup.$NOW.tar.gz\"\r\n# rest of script<\/pre>\n<h3>Complete list of FORMAT control characters supported by date command<\/h3>\n<p>FORMAT controls the output.It can be the combination of any one of the following:<\/p>\n<p>&nbsp;<\/p>\n<dl compact=\"compact\">\n<dt>%%<\/dt>\n<dd>\na literal %<\/dd>\n<dt>%a<\/dt>\n<dd>\nlocale&#8217;s abbreviated weekday name (e.g., Sun)<\/dd>\n<dt>%A<\/dt>\n<dd>&nbsp;<\/p>\n<p>locale&#8217;s full weekday name (e.g., Sunday)<\/p>\n<\/dd>\n<dt>%b<\/dt>\n<dd>\nlocale&#8217;s abbreviated month name (e.g., Jan)<\/dd>\n<dt>%B<\/dt>\n<dd>\nlocale&#8217;s full month name (e.g., January)<\/dd>\n<dt>%c<\/dt>\n<dd>\nlocale&#8217;s date and time (e.g., Thu Mar 3 23:05:25 2005)<\/dd>\n<dt>%C<\/dt>\n<dd>\ncentury; like %Y, except omit last two digits (e.g., 21)<\/dd>\n<dt>%d<\/dt>\n<dd>\nday of month (e.g, 01)<\/dd>\n<dt>%D<\/dt>\n<dd>&nbsp;<\/p>\n<p>date; same as %m\/%d\/%y<\/p>\n<\/dd>\n<dt>%e<\/dt>\n<dd>\nday of month, space padded; same as %_d<\/dd>\n<dt>%F<\/dt>\n<dd>\nfull date; same as %Y-%m-%d<\/dd>\n<dt>%g<\/dt>\n<dd>\nlast two digits of year of ISO week number (see %G)<\/dd>\n<dt>%G<\/dt>\n<dd>\nyear of ISO week number (see %V); normally useful only with %V<\/dd>\n<dt>%h<\/dt>\n<dd>\nsame as %b<\/dd>\n<dt>%H<\/dt>\n<dd>&nbsp;<\/p>\n<p>hour (00..23)<\/p>\n<\/dd>\n<dt>%I<\/dt>\n<dd>\nhour (01..12)<\/dd>\n<dt>%j<\/dt>\n<dd>\nday of year (001..366)<\/dd>\n<dt>%k<\/dt>\n<dd>\nhour ( 0..23)<\/dd>\n<dt>%l<\/dt>\n<dd>\nhour ( 1..12)<\/dd>\n<dt>%m<\/dt>\n<dd>\nmonth (01..12)<\/dd>\n<dt>%M<\/dt>\n<dd>&nbsp;<\/p>\n<p>minute (00..59)<\/p>\n<\/dd>\n<dt>%n<\/dt>\n<dd>\na newline<\/dd>\n<dt>%N<\/dt>\n<dd>\nnanoseconds (000000000..999999999)<\/dd>\n<dt>%p<\/dt>\n<dd>\nlocale&#8217;s equivalent of either AM or PM; blank if not known<\/dd>\n<dt>%P<\/dt>\n<dd>\nlike %p, but lower case<\/dd>\n<dt>%r<\/dt>\n<dd>\nlocale&#8217;s 12-hour clock time (e.g., 11:11:04 PM)<\/dd>\n<dt>%R<\/dt>\n<dd>&nbsp;<\/p>\n<p>24-hour hour and minute; same as %H:%M<\/p>\n<\/dd>\n<dt>%s<\/dt>\n<dd>\nseconds since 1970-01-01 00:00:00 UTC<\/dd>\n<dt>%S<\/dt>\n<dd>\nsecond (00..60)<\/dd>\n<dt>%t<\/dt>\n<dd>\na tab<\/dd>\n<dt>%T<\/dt>\n<dd>\ntime; same as %H:%M:%S<\/dd>\n<dt>%u<\/dt>\n<dd>\nday of week (1..7); 1 is Monday<\/dd>\n<dt>%U<\/dt>\n<dd>&nbsp;<\/p>\n<p>week number of year, with Sunday as first day of week (00..53)<\/p>\n<\/dd>\n<dt>%V<\/dt>\n<dd>\nISO week number, with Monday as first day of week (01..53)<\/dd>\n<dt>%w<\/dt>\n<dd>\nday of week (0..6); 0 is Sunday<\/dd>\n<dt>%W<\/dt>\n<dd>\nweek number of year, with Monday as first day of week (00..53)<\/dd>\n<dt>%x<\/dt>\n<dd>\nlocale&#8217;s date representation (e.g., 12\/31\/99)<\/dd>\n<dt>%X<\/dt>\n<dd>\nlocale&#8217;s time representation (e.g., 23:13:48)<\/dd>\n<dt>%y<\/dt>\n<dd>&nbsp;<\/p>\n<p>last two digits of year (00..99)<\/p>\n<\/dd>\n<dt>%Y<\/dt>\n<dd>\nyear<\/dd>\n<dt>%z<\/dt>\n<dd>\n+hhmm numeric timezone (e.g.,\u00a0<strong>-0400<\/strong>)<\/dd>\n<dt>%:z<\/dt>\n<dd>\n+hh:mm numeric timezone (e.g.,\u00a0<strong>-04<\/strong>:00)<\/dd>\n<dt>%::z<\/dt>\n<dd>\n+hh:mm:ss numeric time zone (e.g.,\u00a0<strong>-04<\/strong>:00:00)&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<\/dd>\n<dt>%:::z<\/dt>\n<dd>\nnumeric time zone with : to necessary precision (e.g.,\u00a0<strong>-04<\/strong>, +05:30)<\/dd>\n<dt>%Z<\/dt>\n<dd>\nalphabetic time zone abbreviation (e.g., EDT)<\/dd>\n<\/dl>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Q. How do I format date to display on screen on for my scripts as per my requirements? A. You need to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1099","post","type-post","status-publish","format-standard","hentry","category-info-on-tech"],"_links":{"self":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1099"}],"version-history":[{"count":0,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1099\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}