Chapter 2: Date Manipulation

        __Linux 101 Hacks

Hack 7. Set System Date and Time

To change the system date use:

 

# date {mmddhhmiyyyy.ss}

o mm – Month
o dd – Date
o hh –
24 hour format
o mi – Minutes
o yyyy – Year
o ss – seconds

 

 

For example, to set system date to Jan 31st 2009, 10:19 p.m, 53 seconds

 

 

# date 013122192009.53

 

 

 

 

You can also change system date using set argument as shown below.

 

# date 013122192009.53

# date +%Y%m%d -s "20090131"

# date -s "01/31/2009 22:19:53"

# date -s "31 JAN 2009 22:19:53"

# date set="31 JAN 2009 22:19:53"

 

 

 

To set the time only:

 

# date +%T -s "22:19:53"

# date +%T%p -s "10:19:53PM"

 

 

 

Hack 8. Set Hardware Date and Time

Before setting the hardware date and time, make sure the OS date and timeis set appropriately as shown in the hack#7.

Set the hardware date and time based on the system date as shown below:

 

# hwclock –systohc

# hwclock --systohc –utc

 

 

 

Use hwclock without any parameter, to view the current hardware date andtime:

    # maybe sudo hwclock

Check the clock file to verify whether the system is set for UTC:

 

# cat /etc/sysconfig/clock

ZONE
="America/Los_Angeles"
UTC
=false
ARC
=false

 

 

 

 

 

 

 

Hack 9. Display Current Date and Time in a SpecificFormat

Following are different ways of displaying the current date and time invarious formats:

 

代码
$ date
Thu Jan
1 08:19:23 PST 2009

$ date
--date="now"
Thu Jan
1 08:20:05 PST 2009

$ date
--date="today"
Thu Jan
1 08:20:12 PST 2009

$ date
--date='1970-01-01 00:00:01 UTC +5 hours' +%s
18001

$ date
'+Current Date: %m/%d/%y%nCurrent Time:%H:%M:%S'
Current Date:
01/01/09
Current Time:0
8:21:41

$ date
+"%d-%m-%Y"
01-01-2009

$ date
+"%d/%m/%Y"
01/01/2009

$ date
+"%A,%B %d %Y"
Thursday,January
01 2009

 

 

 

Following are the different format options you can pass to the datecommand:

 

代码
o %D date (mm/dd/yy)
o
%d day of month (01..31)
o
%m month (01..12)
o
%y last two digits of year (00..99)
o
%a locale’s abbreviated weekday name (Sun..Sat)
o
%A locale’s full weekday name, variable length
(Sunday..Saturday)
o
%b locale’s abbreviated month name (Jan..Dec)
o
%B locale’s full month name, variable length
(January..December)
o
%H hour (00..23)
o
%I hour (01..12)
o
%Y year (1970...)

 

 

 

Hack 10. Display Past Date and Time

Following are various ways to display a past date and time:

 

代码
$ date --date='3 seconds ago'
Thu Jan
1 08:27:00 PST 2009

$ date
--date="1 day ago"
Wed Dec
31 08:27:13 PST 2008

$ date
--date="1 days ago"
Wed Dec
31 08:27:18 PST 2008

$ date
--date="1 month ago"
Mon Dec
1 08:27:23 PST 2008

$ date
--date="1 year ago"
Tue Jan
1 08:27:28 PST 2008

$ date
--date="yesterday"
Wed Dec
31 08:27:34 PST 2008

$ date
--date="10 months 2 day ago"
Thu Feb
28 08:27:41 PST 2008

 

Hack 11. Display Future Date and Time

Following examples shows how to display a future date and time.

 

代码
$ date
Thu Jan
1 08:30:07 PST 2009

$ date
--date='3 seconds'
Thu Jan
1 08:30:12 PST 2009

$ date
--date='4 hours'
Thu Jan
1 12:30:17 PST 2009

$ date
--date='tomorrow'
Fri Jan
2 08:30:25 PST 2009

$ date
--date="1 day"
Fri Jan
2 08:30:31 PST 2009

$ date
--date="1 days"
Fri Jan
2 08:30:38 PST 2009

$ date
--date="2 days"
Sat Jan
3 08:30:43 PST 2009

$ date
--date='1 month'
Sun Feb
1 08:30:48 PST 2009

$ date
--date='1 week'
Thu Jan
8 08:30:53 PST 2009

$ date
--date="2 months"
Sun Mar
1 08:30:58 PST 2009

$ date
--date="2 years"
Sat Jan
1 08:31:03 PST 2011

$ date
--date="next day"
Fri Jan
2 08:31:10 PST 2009

$ date
--date="-1 days ago"
Fri Jan
2 08:31:15 PST 2009

$ date
--date="this Wednesday"
Wed Jan
7 00:00:00 PST 2009

 

 

 

 

 

posted on 2010-09-14 00:23  oyzway  阅读(422)  评论(0编辑  收藏  举报