您的位置:首页 > 运维架构 > Shell

How to format date for display or to use in a shell script

2012-06-01 14:29 881 查看


How to format date for display or to use in a shell script

http://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/

by VIVEK
GITE on FEBRUARY
27, 2007 · 27
COMMENTS· last updated at FEBRUARY
27, 2007





Q. How do I format date to display on screen on for my scripts as per my requirements?

A. You need to use standard date command to format date or time for output or to use in a shell script.

Syntax to specify format
date +FORMAT


Task: Display date in mm-dd-yy format

Type the command as follows:
$
date +"%m-%d-%y"


Output:
02-27-07


Turn on 4 digit year display:
$
date +"%m-%d-%Y"


Just display date as mm/dd/yy format:
$
date +"%D"


Task: Display time only

Type the command as follows:
$
date +"%T"


Output:
19:55:04


Display locale’s 12-hour clock time
$
date +"%r"


Output:
07:56:05 PM


Display time in HH:MM format:
$
date +"%H-%M"


How do I save time/date format to a variable?

Simply type command as follows at a shell prompt:
$
NOW=$(date +"%m-%d-%Y")


To display a variable use echo
/ printf command:
$
echo $NOW


Sample shell script:
#!/bin/bash
NOW=$(date +"%m-%d-%Y")
FILE="backup.$NOW.tar.gz"
# rest of script


Complete list of FORMAT control characters supported by date command

FORMAT controls the output.It can be the combination of any one of the following:

%%

a literal %

%a

locale's abbreviated weekday name (e.g., Sun)

%A

locale's full weekday name (e.g., Sunday)

%b

locale's abbreviated month name (e.g., Jan)

%B

locale's full month name (e.g., January)

%c

locale's date and time (e.g., Thu Mar 3 23:05:25 2005)

%C

century; like %Y, except omit last two digits (e.g., 21)

%d

day of month (e.g, 01)

%D

date; same as %m/%d/%y

%e

day of month, space padded; same as %_d

%F

full date; same as %Y-%m-%d

%g

last two digits of year of ISO week number (see %G)

%G

year of ISO week number (see %V); normally useful only with %V

%h

same as %b

%H

hour (00..23)

%I

hour (01..12)

%j

day of year (001..366)

%k

hour ( 0..23)

%l

hour ( 1..12)

%m

month (01..12)

%M

minute (00..59)

%n

a newline

%N

nanoseconds (000000000..999999999)

%p

locale's equivalent of either AM or PM; blank if not known

%P

like %p, but lower case

%r

locale's 12-hour clock time (e.g., 11:11:04 PM)

%R

24-hour hour and minute; same as %H:%M

%s

seconds since 1970-01-01 00:00:00 UTC

%S

second (00..60)

%t

a tab

%T

time; same as %H:%M:%S

%u

day of week (1..7); 1 is Monday

%U

week number of year, with Sunday as first day of week (00..53)

%V

ISO week number, with Monday as first day of week (01..53)

%w

day of week (0..6); 0 is Sunday

%W

week number of year, with Monday as first day of week (00..53)

%x

locale's date representation (e.g., 12/31/99)

%X

locale's time representation (e.g., 23:13:48)

%y

last two digits of year (00..99)

%Y

year

%z

+hhmm numeric timezone (e.g., -0400)

%:z

+hh:mm numeric timezone (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)


Getting Yesterdays or Tomorrows Day With Bash Shell Date Command

by VIVEK
GITE on JUNE
17, 2007 · 45
COMMENTS· LAST UPDATED JUNE
15, 2011





When
invoked without arguments, the date command displays the current date and time. Depending on the options specified, date will set the date and time or print it in a user defined way. I've seen many sysadmin writing perl scripts for calculating relative date
such as yesterdays or tomorrows day. You can use GNU date command, which is designed to handle relative date calculation such as:





1 Year

2 Days

2 Days ago

5 Years


GNU date syntax

The syntax is as follows:
 
date  --date="STRING"
date  --date="next Friday"
date  --date="2 days ago"
 


The --date=STRING is a human readable format such as "next Thursday" or "1 month ago". A date string may contain items indicating calendar date, time of day, time zone, day of week, relative time, relative date, and numbers.


Why Use Relative GNU/date Formats?

Ease of use

Write your own shell scripts

Automate task using cron (example run a job on last day of the month or Nth day of the month or 3rd Friday and so on)


Examples

First, to display today's date, enter:
$
date


Sample outputs:
Wed Jun 15 04:47:45 IST 2011


To display yesterday's date, enter:
$
date --date="1 days ago"


OR
$
date --date="1 day ago"


OR
$
date --date="yesterday"


OR
$
date --date="-1 day"


Sample outputs:
Tue Jun 14 04:54:40 IST 2011


You can use various string formats to produce the same output. Please note that the output of the date command is not always acceptable as a date string, not only because of the language problem, but also because there is no standard meaning for time zone items
like IST.


Find tomorrow's date

Type the following command
$
date --date="-1 days ago"


Or
$
date --date="next day"


Getting Date In the Future

To get tomorrow and day after tomorrow (tomorrow+N) use day word to get date in the future as
follows:
 
date --date='tomorrow'
date --date='1 day'
date --date='10 day'
date --date='10 week'
date --date='10 month'
date --date='10 year'
 


The date string 'tomorrow' is worth one day in the future which is equivalent to 'day' string i.e. first two commands are same.


Getting Date In the Past

To get yesterday and earlier day in the past use string day ago:
 
date --date='yesterday'
date --date='1 day ago'
date --date='10 day ago'
date --date='10 week ago'
date --date='10 month ago'
date --date='10 year ago'
 


The date string 'yesterday' is worth one day in the past which is equivalent to 'day ago' string i.e. first two commands are same.


Moving By Whole Years or Months

You can add year and months keywords to get more accurate date:
$
date --date='2 year ago' # Past

$ date --date='3 years' # Go into future

$ date --date='2 days' # Future

$ date --date='1 month ago' # Past

$ date --date='2 months' # Future


Moving Date Using More Precise Units

You can use fortnight for 14 day.

week for 7 days.

hour for 60 minutes

minute for 60 seconds

second for one second

You can also use this / now / today keywords
to stress the meaning.

Few examples using precise string units:
 
date --date='fortnight'
date --date='5 fortnight'
date --date='fortnight ago'
date --date='5 fortnight ago'
date --date='2 hour'
date --date='2 hour ago'
date --date='20 minute'
date --date='20 minute ago'
 


Moving Date Using the Day of Week Items

To print the date of this Friday, enter:
 
date --date='this Friday'
## OR ##
date --date='next Friday'
 


Days of the week may be spelled out in full: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday or Saturday. Days may be abbreviated to their first three letters, optionally followed by a period
 
date --date='this Fri'
## OR ##
date --date='next Fri.'
 


You can also move forward supplementary weeks as follow:
 
date --date='2 Fri'
## OR ##
date --date='second Fri.'
## OR ##
date --date='Second Friday'
####
####  last DAY or next DAY move one week before or after the day that DAY by itself
####
date --date='last Friday'
date --date='next Friday'
 


To print the date in the future ($now + 6 months + 15 days), enter:
$
date --date='6 months 15 day'


To print the date in the past [$now - (two months and 5 days ago) ], enter:
$
date --date='-2 months 5 day ago'


Display Date Using Epoch Time

To display date in epoch
time:
$
date --date='1970-01-01 00:00:01 UTC +5 hours' +%s


How Do I Use Relative Date Format To Set System Date & Time?

You can also use relative format to setup date and time. For example to set the system clock forward by 30 minutes, enter (you must be login as root to set the system date and time):
#
date --set='+30 minutes'


OR
#
date --set='1 day ago'


OR
#
date --set='5 day'


However, I recommend setting NTPD
client / server or OpenNTPD
server to synchronize the local clock of a computer system with remote NTP servers.


How Do I Assigned Yesterday To Shell Variable?

Use the following syntax (see assign
values to shell variables and command
substitution for more information)
 
yest=$(date --date="yesterday")
echo "$yest"
yest=$(date --date="yesterday" +"%d/%m/%Y")
echo "The backup was last verified on $yest"
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息