Date of Linux

Source: Internet
Author: User
Tags set time

Recent learning needs to be based on different date data, do the same operation, if the manual operation over and over, too cumbersome, some of the taste of the foolish. So just think about the time as a variable under the shell, and then repeat the other actions.

The requirement here is to get the N-day variable starting from one day (for example: 2014-06-01) and pass the variable to the Linux command or script file, and then repeat the operation.

Practice is the best teacher, there are a lot of cumbersome methods, but found to use date to get, very convenient, simple.

Here is the first step of the above requirements:

$startDay = "2014-06-01"

For i in ' seq 0 n '

Do

day= ' date-d ' $startDay $i days "+"%y%m%d "' #day就是我们要求的, and if it is later, it is forward; -D equivalent to--date

Echo $day #输出结果

Done

This will enable us to meet our needs.

Date Introduction

Date format:

Date [-u] [-D DATESTR] [-S datestr] [--UTC] [--universal] [--DATE=DATESTR] [--SET=DATESTR] [--help] [--version] [+format] [MMDDHHMM[[CC]YY][.SS]]

Date can be used to display or set the day and time of the system.

time facet:
%: print%
%n: Next line
%t: Jump Grid
H: Hours (00-23)
%I: Hours (01-12)
%k: Hours (0-23)
% L: Hours (1-12)
% M: minutes (00-59)
%p: Show local AM or PM
%r: Display time directly (12-hour format hh:mm:ss [ap]m)
%s: from January 1, 1970 00:00 : GMT UTC so far seconds
% s: seconds (00-60)
% T: Direct display time (24-hour system)
% X: equivalent to%h:%m:%s
%Z: Display time zone
date facet:
%a: Day of the Week (SUN-SAT)
% A: Days of the Week (Sunday-saturday)
%b: Month (JAN-DEC)
% B: Month (january-december)
%c: Show date and time
% directly D: Day (01-31)
% D: Show date directly (MM/DD/YY)
%h: Same as%b
%j: Day of the Year (001-366)
%m: Month (01-12)
% U: Week of the Year (00-53) (in the case of Sunday for the first day of the week)
%w: Day of the Week (0-6)
% W: Week of the Year (00-53) (in case of the first day of the week)
%x: Show date directly (MM/DD/YY)
%y: Last two digits of the year (00.99) %y: Full year (0000-9999)
If you do not start with a plus sign, it means to set the time, and the time format is MMDDHHMM[[CC]YY][.SS], where mm is the month, DD is day, HH is the hour, MM is minutes, CC is the first two digits of the year, YY is two digits after the year, and SS is the number of seconds

parameter:
-D datestr: Displays the time set in DATESTR (non-system time)
--help: Display Auxiliary message
-S DATESTR: Set system time to Datestr Time
-u: Show current GMT
--version: Show version number
Example:
Show Current date: #date ' +%t%n%d '
Show months and days: Date ' +% B%d '
Display date and set time (12:34:56): #date--date ' 12:34:56 '
Note: When you don't want a meaningless 0 o'clock (say, 1999/03/07), you can insert a symbol in the tag, such as Date ' +%-h :%-m:%-s ' will remove the meaningless 0 in seconds and minutes, as if the original 08:09:04 would change to 8:9:4. In addition, the system time can be set only if the authority (for example, Root) is acquired.
After you change the system time as root, remember to use CLOCK-W to write the system time to the CMOS so that the system time will continue to hold the latest correct value the next time you reboot.
Example: Modify Date Time
at the command line input:
Date  => Show Current time Fri 3 14:15:16 CST
Date–s = Modify the time by string
can only modify the date, do not modify the time, enter: Date-s 2007-08-03
Only modify the time, input: Date-s 14:15:00
also modify the date time, note to add double quotation marks, the date and time there is a space, enter:
#date-S "2007-08-03 14:15:00"

Add and Subtract
Date +%y%m%d//display now day and Month day
Date +%y%m%d--date= "+1 Day"//Display days later
Date +%y%m%d--date= "-1 days"//Displays the day before
Date +%y%m%d--date= "-1 month"//show dates on January
Date +%y%m%d--date= "+1 Month"//displays the dates for the next January
Date +%y%m%d--date= "-1 Year"//show dates for previous years
Date +%y%m%d--date= "+1 Year"//show dates for next

Can say + "%y%m%d" in the end, it is best to put double quotes, especially in the script, you can use-D instead of--date.

Off Topic:
1. View Hardware time
# Hwclock
Set Hardware time
# hwclock-set-date= "07/07/06 10:19" (Month/day/year: minutes: seconds)
2. Synchronization of hardware time and system time
According to the foregoing, reboot the system, hardware time will read the system time, to achieve synchronization, but do not restart, you need to use the Hwclock command to achieve synchronization.
The hardware clock is synchronized with the system clock:
# Hwclock--hctosys (HC stands for hardware time, SYS represents system time)
system clock and Hardware clock synchronization: (To synchronize the system's time to the hardware clock)
# HWCLOCK–SYSTOHC

Date of Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.