Linux date format (time format) in Shell __linux

Source: Internet
Author: User
Tags date1 linux date format local time month name postgresql redis set time date two weeks ago


Local Test Cases:






# there can be no spaces on either side of the equal sign, before the error has been out here
yesterday= ' date-d last-day +%y-%m-%d '
echo $yesterday

curday= ' Date +%y-%m-%d '
echo $curday

echo "Now time: ' Date ' +%y%m%d%T '"
echo "Now time: ' Date ' +%y%m%d%h%m%s '"

echo ' Date ' +%y%m%d-% h%m%s '

t3= ' date ' +%y-%m-%d%h:%m:%s '
echo $t 3 send= '

date ' +%y-%m-%d%h:%m:%s '
echo $send

T4 = ' Date ' +%y-%m-%d%h:%m:%s '
echo $t 4



Turn from: http://blog.csdn.net/classhao1/article/details/8182733






Get the date of the day



Date +%y-%m-%d



Output: 2011-07-28






date1=$ (Date--date= ' 1 days ago +%y%m%d ') #Date of the previous day



date1=$ (Date--date= ' 2 days ago +%y%m%d ') #Date of the first two days






Assign the current date to a date variable
date=$ (Date +%y%m%d)



Sometimes we need to use dates before or after today, when we can use the date-D parameter






Get Tomorrow's date



Date-d Next-day +%y%m%d



Get yesterday's date



Date-d Last-day +%y%m%d



Get the year and month of last month



Date-d Last-month +%y%m



Get the year and month of next month
Date-d Next-month +%y%m



Get next year's year
Date-d Next-year +%y






The following is a description of some date parameters and some examples



Name: Date
Use Rights: All users
Usage: Date [u] [D-DATESTR] [-S datestr] [--UTC] [--universal] [--DATE=DATESTR] [--SET=DATESTR] [--help] [--version] [+f ORMAT] [MMDDHHMM[[CC]YY][.SS]]
Description: Date can be used to display or set the system of dates and times, in the display, users can set the format to display, format set to a plus after a number of tags, where the list of available tags are as follows:
Time aspect:
%: Printed out
%%n: Next line
%t: Jump Lattice
%H: Hours (00..23)
%I: Hours (01..12)
%k: Hours (0..23)
%l: Hours (1..12)
%m: Min (00..59)
%p: Show local AM or PM
%r: Direct Display time (12-hour system, format hh:mm:ss [ap]m)
%s: Number of seconds from January 1, 1970 00:00:00 UTC to date%s: sec (00..61)
%T: Direct Display time (24-hour system)
%x: Equivalent to%h:%m:%s
%Z: Show Time zone
Date aspect:
%a: Day of the Week (Sun. Sat)
%A: Day of the Week (Sunday. Saturday)
%b: Month. DEC)
%B: Month (January. December)
%c: Displays the date and time directly
%d: Day (01..31)
%d: Direct display date (MM/DD/YY)
%h: With%b
%j: The first day of the Year (001..366)
%m: Month (01..12)
%u: The week of the Year (00..53) (with Sunday as the first day of the week)
%w: The first day of the Week (0..6)
%w: The first few weeks of the year (00..53) (in Monday case for day one of the week)
%x: Direct display date (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, you are setting the time, and the time format is MMDDHHMM[[CC]YY][.SS],
Where MM is the month,
DD is the day,
HH for the Hour,
MM for minutes,
CC is the first two digits of the year,
YY is the two digits after the year,
SS is second number
The meter:
-D datestr: Displays the time set in DATESTR (non-system time)
--HELP: Display auxiliary messages
-S DATESTR: Set the system time to the time set in Datestr
-U: Show the current Greenwich TIME
--version: Display version number
Example:
Show time after hop, then 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
Set the system current time (12:34:56): Date--s 12:34:56
Note: When you do not want to appear meaningless 0 o'clock (for example, 1999/03/07), you can insert-symbol in the tag, for example, date +%-h:%-m:%-s will take away the meaningless 0 in the minutes, as if the original 08:09:04 would become 8:9:4. In addition, only those who get permission (for example, root) can set the system time. When you change the system time as root, remember to write the system time to the CMOS in Clock-w, so that the system time will continue to hold the latest correct value the next time you reboot.
NTP time synchronization
The NTP service is installed by default on the Linux system, and the NTP synchronization is done manually as follows
Ntpdate ntp1.nl.net
Of course, you can also specify a different NTP server
-------------------------------------------------------------------
Extended Functionality
The date tool can do more work than just print out the current system date. You can use it to get the day of the week on which the given date is, and to get the relative date corresponding to the current date. Know what day of the week
Another extension of the GNU to date command is the-D option, which is useful when there are no calendar tables on your desk (UNIX users do not need a calendar table). With this powerful option, you can quickly find out what day of the week a particular date is, by providing a parameter that encloses the date as quotation marks:
$ date-d "Nov 22"
Wed Nov 00:00:00 EST 2006
$
In this example, you can see that this year's November 22 is Wednesday.
So, assuming that a major meeting is held on November 22, you can immediately learn that this day is Wednesday, and that day you will be at the office of the resident.
Get relative date
The D option can also tell you what day of the day is relative to the current date, a number of days or weeks from now, or before (past). You can accomplish this task by enclosing this relative offset in quotation marks as an argument to the-D option.
For example, you need to know the date two weeks from now. If you are at the Shell prompt, you can get an answer quickly:
$ Date-d ' 2 weeks '
There are some other important ways to use this command. Using the Next/last directive, you can get what day of the following week:
$ Date-d ' next Monday ' (date of next Monday)
$ date-d next-day +%y%m%d (tomorrow's date) or: Date-d tomorrow +%y%m%d
$ date-d last-day +%y%m%d (yesterday's date) or: date-d yesterday +%y%m%d
$ date-d last-month +%y%m (last month was a few months)
$ date-d next-month +%y%m (next month is a few months)
Using the AGO directive, you can get past dates:
$ Date-d ' day ago ' (30 days before date)
You can use negative numbers to get the opposite date:
$ Date-d ' Dec 14-2 weeks ' (relative: Dec 14 date two weeks ago)
$ Date-d ' -100 days ' (100 day ago date)
$ Date-d ' (50 days after date)
This technique is useful for setting reminders for yourself based on future dates, possibly in a script or Shell startup file, as follows:
Day= ' date-d ' 2 weeks ' + '%b%d '
if test "' echo $DAY '" = "Aug 16"; Then Echo ' Product launch is now two weeks away! '; Fi

##############################

Date format conversions in the Unix shell
$ t_t= "June 1 21:29"
$ date "+%g-%m-%d%h:%m:%s"-D "$t _t" # $t _t format is more casual
2008-07-01 21:29:00
$ date +%b/%d/%g-d "2008-07-01"
jul/01/2008

Number of days between dates in the Unix shell
$ Expr ' (' $ (date +%s-d ' 2008-07-02 ')-$ (date +%s-d "2008-05-30") ') '/86400
33
# #存在bcdate的话, can be used directly.

Shell in date plus minus specified interval unit
Increase by 36 Hours:
$ A= ' Date +%y-%m-%d '
$ b= ' date +%y-%m-%d-d ' $a +36 hours '
10 days ago:
$ date-d "$a -10 days"
Sun June 00:00:00 CST 2008

Displays the last date after a file change, such as Yyyy-mm-dd Hh24:mi:ss, in the specified format.
$ date "+%y-%m-%d%h:%m:%s"-R Test.bak
2008-07-01 21:28:55


Turn from: http://www.flatws.cn/article/program/shell/2011-05-07/24592.html





In Linux, use the shell to get the date yesterday, tomorrow, or many days ago:
In Linux, the man date-d parameters are more ambiguous, the following examples to further illustrate:
# D,--date=string display time described by STRING, not ' now '
[Root@gman root]# date-d next-day +%y%m%d #tomorrow date
20091024
[Root@gman root]# date-d last-day +%y%m%d # yesterday date
20091022
[Root@gman root]# date-d yesterday +%y%m%d # yesterday date
20091022
[Root@gman root]# date-d tomorrow +%y%m%d # tomorrow date
20091024
[Root@gman root]# date-d last-month +%y%m #Last month date
200909
[Root@gman root]# date-d next-month +%y%m #Next month date
200911
[Root@gman root]# date-d next-year +%y #Next date
2010
Date=$ (Date +%y%m%d--date ' 2 days ago ') #Get the date yesterday or more days ago

Name: Date
Use Rights: All users
Usage: Date [u] [D-DATESTR] [-S datestr] [--UTC] [--universal] [--DATE=DATESTR] [--SET=DATESTR] [--help] [--version] [+f ORMAT] [MMDDHHMM[[CC]YY][.SS]]
Description: Date can be used to display or set the system of dates and times, in the display, users can set the format to display, format set to a plus after a number of tags, where the list of available tags are as follows:
Time aspect:
%: Printed out
%%n: Next line
%t: Jump Lattice
%H: Hours (00..23)
%I: Hours (01..12)
%k: Hours (0..23)
%l: Hours (1..12)
%m: Min (00..59)
%p: Show local AM or PM
%r: Direct Display time (12-hour system, format hh:mm:ss [ap]m)
%s: Number of seconds from January 1, 1970 00:00:00 UTC to date%s: sec (00..61)
%T: Direct Display time (24-hour system)
%x: Equivalent to%h:%m:%s
%Z: Show Time zone
Date aspect:
%a: Day of the Week (Sun. Sat)
%A: Day of the Week (Sunday. Saturday)
%b: Month. DEC)
%B: Month (January. December)
%c: Displays the date and time directly
%d: Day (01..31)
%d: Direct display date (MM/DD/YY)
%h: With%b
%j: The first day of the Year (001..366)
%m: Month (01..12)
%u: The week of the Year (00..53) (with Sunday as the first day of the week)
%w: The first day of the Week (0..6)
%w: The first few weeks of the year (00..53) (in Monday case for day one of the week)
%x: Direct display date (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, you are setting the time, and the time format is MMDDHHMM[[CC]YY][.SS],
Where MM is the month,
DD is the day,
HH for the Hour,
MM for minutes,
CC is the first two digits of the year,
YY is the two digits after the year,
SS is second number
The meter:
-D datestr: Displays the time set in DATESTR (non-system time)
--HELP: Display auxiliary messages
-S DATESTR: Set the system time to the time set in Datestr
-U: Show the current Greenwich TIME
--version: Display version number
Example:
Show time after hop, then 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
Set the system current time (12:34:56): Date--s 12:34:56
Note: When you do not want to appear meaningless 0 o'clock (for example, 1999/03/07), you can insert-symbol in the tag, for example, date +%-h:%-m:%-s will take away the meaningless 0 in the minutes, as if the original 08:09:04 would become 8:9:4. In addition, only those who get permission (for example, root) can set the system time. When you change the system time as root, remember to write the system time to the CMOS in Clock-w, so that the system time will continue to hold the latest correct value the next time you reboot.
NTP time synchronization
The NTP service is installed by default on the Linux system, and the NTP synchronization is done manually as follows
Ntpdate ntp1.nl.net
Of course, you can also specify a different NTP server
-------------------------------------------------------------------
Extended Functionality
The date tool can do more work than just print out the current system date. You can use it to get the day of the week on which the given date is, and to get the relative date corresponding to the current date. Know what day of the week
Another extension of the GNU to date command is the-D option, which is useful when there are no calendar tables on your desk (UNIX users do not need a calendar table). With this powerful option, you can quickly find out what day of the week a particular date is, by providing a parameter that encloses the date as quotation marks:
$ date-d "Nov 22"
Wed Nov 00:00:00 EST 2006
$
In this example, you can see that this year's November 22 is Wednesday.
So, assuming that a major meeting is held on November 22, you can immediately learn that this day is Wednesday, and that day you will be at the office of the resident.
Get relative date
The D option can also tell you what day of the day is relative to the current date, a number of days or weeks from now, or before (past). You can accomplish this task by enclosing this relative offset in quotation marks as an argument to the-D option.
For example, you need to know the date two weeks from now. If you are at the Shell prompt, you can get an answer quickly:
$ Date-d ' 2 weeks '
There are some other important ways to use this command. Using the Next/last directive, you can get what day of the following week:
$ Date-d ' next Monday ' (date of next Monday)
$ date-d next-day +%y%m%d (tomorrow's date) or: Date-d tomorrow +%y%m%d
$ date-d last-day +%y%m%d (yesterday's date) or: date-d yesterday +%y%m%d
$ date-d last-month +%y%m (last month was a few months)
$ date-d next-month +%y%m (next month is a few months)
Using the AGO directive, you can get past dates:
$ Date-d ' day ago ' (30 days before date)
You can use negative numbers to get the opposite date:
$ Date-d ' Dec 14-2 weeks ' (relative: Dec 14 date two weeks ago)
$ Date-d ' -100 days ' (100 day ago date)
$ Date-d ' (50 days after date)
This technique is useful for setting reminders for yourself based on future dates, possibly in a script or Shell startup file, as follows:
Day= ' date-d ' 2 weeks ' + '%b%d '
if test "' echo $DAY '" = "Aug 16"; Then Echo ' Product launch is now two weeks away! '; Fi


##############################



Date format conversions in the Unix shell
$ t_t= "June 1 21:29"
$ date "+%g-%m-%d%h:%m:%s"-D "$t _t" # $t _t format is more casual
2008-07-01 21:29:00
$ date +%b/%d/%g-d "2008-07-01"
jul/01/2008



Number of days between dates in the Unix shell
$ Expr ' (' $ (date +%s-d ' 2008-07-02 ')-$ (date +%s-d "2008-05-30") ') '/86400
33
# #存在bcdate的话, can be used directly.



Shell in date plus minus specified interval unit
Increase by 36 Hours:
$ A= ' Date +%y-%m-%d '
$ b= ' date +%y-%m-%d-d ' $a +36 hours '
10 days ago:
$ date-d "$a -10 days"
Sun June 00:00:00 CST 2008



Displays the last date after a file change, such as Yyyy-mm-dd Hh24:mi:ss, in the specified format.
$ date "+%y-%m-%d%h:%m:%s"-R Test.bak
2008-07-01 21:28:55
This article comes from: Development Institute http://edu.codepub.com original Link: http://edu.codepub.com/2011/0206/29186.php


The date usage of the "Linux" Bash shell #man Date can see the date Help file
#date Get the current time
#date-D "-1 week" +%y%m%d acquired on the Sunday period (day,month,year,hour)
#date--date= " -24 hour" +%y%m%d ditto
date_now= ' date +%s ' shell script assigns variable values

%%% OUTPUT% symbol
%a the current domain's weekday abbreviation (Sun ... Sat)
%A the current domain for the full week (Sunday. Saturday)
%B the month abbreviation for the current field. DEC)
%B the full month name of the current domain (January. December)
Default time format for current field in%c (Sat Nov 12:02:33 EST 1989)
%c N Hundred years [00-99]
%d two-bit days (01..31)
%d Short time Format (MM/DD/YY)
%e Short Format Day (1..31)
%F file time format same as%y-%m-%d
%h Same as%b
%H 24-hour hours (00..23)
%I 12-hour hours (01..12)
%j Day of the Year (001..366)
%k Short Form 24 hour (0..23)
%l Short Form 12 Hour (1..12)
%m Two-digit month (01..12)
%m double digit minute (00..59)
%n Line Change
%N One-zero seconds (000000000..999999999)
%p Upper afternoon indication of the current field in uppercase (blank in many locales)
%P on the upper PM of the current field of lowercase (blank in many locales)
%r 12-hour Time representation (time: minutes: seconds, double digits) times, 12-hour (Hh:mm:ss [ap]m)
%R 24-hour time representation (time: minutes, double digits) times, 24-hour (hh:mm)
%s from base time 1970-01-01 00:00:00 to current time seconds (a GNU extension)
%s Dual-digit seconds second (00..60);
%t horizontal tab (tab)
%T 24-hour time representation (HH:MM:SS)
%u number of weeks represented (starting from Monday 1-7)
%u the first weeks of the year Sunday as the beginning (00..53)
%V the first few weeks of the year Monday as the beginning (01..53)
%w Day of the week of Sunday as the beginning (0..6)
%w the first few weeks of the year Monday as the beginning (00..53)
%x Local date format (MM/DD/YY)
%x local time format (%h:%m:%s)
%y Two-bit year (00..99)
%Y year (1970 ...)

Example: Write a shell script to calculate how many days from your birthday.
Read-p "Input your Birthday (YYYYMMDD):" Date1
m= ' Date--date= ' $date 1 "+%m" #Get the birthday month
d= ' Date--date= ' $date 1 "+%d" #Get the birthday of the day
Date_now= ' date +%s ' #Get the current time in seconds
y= ' Date +%y ' #Get the current time of the year
Birth= ' Date--date= ' $y $m$d ' +%s ' #Get the seconds value of this year's birthday date
Internal=$ (($birth-$date _now)) #calculate the interval between today's and birthday dates
If ["$internal"-lt "0"]; Then #judge whether today’s birthday has passed
Birth= ' Date--date= $ (($y + 1)) $m $d "+%s" #Get tomorrow's birthday date seconds
Internal=$ (($birth-$date _now)) #Calculating the interval from today to the next birthday
Fi
Echo "There is: $ ((einternal/60/60/24)) days." #output, second conversion to day




Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

Related Article

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.