linux
date -R# Sun, 22 Nov 2015 12:27:31 +0800 date "+%a, %d %b %Y %H:%M:%S %z"# Sun, 22 Nov 2015 12:27:31 +0800 date +"%a, %d %b %Y %H:%M:%S %z"# Sun, 22 Nov 2015 12:36:13 +0800 |
linux中date还有个-I的参数,可以很方便得到ISO-8601格式,也就是常用的yyyy-MM-dd形式的字符串。
date -I# 2015-11-22 |
mac os x
Mac OS中date命令与linux版本的参数不同,要得到rfc822格式,需要格式化日期对象。
格式化字符串前面需要有个+号,可以在双引号里面,也可以在外面,linux手册提示为date [OPTION]... [+FORMAT]。
date "+%a, %d %b %Y %H:%M:%S %z"# Sun, 22 Nov 2015 12:27:31 +0800 date +"%a, %d %b %Y %H:%M:%S %z"# Sun, 22 Nov 2015 12:27:31 +0800 |
perl
perl -v# This is perl 5, version 16, subversion 2 (v5.16.2) built for darwin-thread-multi-2level perl -e 'use POSIX qw(strftime); print strftime("%a, %d %b %Y %H:%M:%S %z", localtime(time())) . "\n";'# Sun, 22 Nov 2015 12:01:11 +0800 |
附linux date命令参数详细说明
datedate [options] [+format] [date]Print the current date and time. You may specify a display format. format can consist of literal text strings (blanks must be quoted) as well as field descriptors, whose values will appear as described in the following entries (the listing shows some logical groupings). A privileged user can change the system's date and time.Options+formatDisplay current date in a nonstandard format. For example:$date +"%A %j %n%k %p" Tuesday 248 15 PMThe default is %a %b %e %T %Z %Y (e.g., Tue Sep 5 14:59:37 EDT 2005).-d date, --date dateDisplay date, which should be in quotes and may be in the format d days or m months d days, to print a date in the future. Specify ago to print a date in the past. You may include formatting (see the following section).-f datefile, --file=datefileLike -d, but printed once for each line of datefile.-I [timespec] , --iso-8601[=timespec]Display in ISO-8601 format. If specified, timespec can have one of the following values: date (for date only), hours, minutes, or seconds to get the indicated precision.-r file, --reference=fileDisplay the time file was last modified.-R, --rfc-822Display the date in RFC 822 format.--helpPrint help message and exit.--versionPrint version information and exit.-s date, --set dateSet the date.-u, --universalSet the date to Greenwich Mean Time, not local time.FormatThe exact result of many of these codes is locale-specific and depends upon your language setting, particularly the LANG environment variable. See locale.%Literal %.- (hyphen)Do not pad fields (default: pad fields with zeros)._ (underscore)Pad fields with space (default: zeros).%aAbbreviated weekday.%bAbbreviated month name.%cCountry-specific date and time format.%dDay of month (01-31).%hSame as %b.%jJulian day of year (001-366).%kHour in 24-hour format, without leading zeros (0-23).%lHour in 12-hour format, without leading zeros (1-12).%mMonth of year (01-12).%nInsert a new line.%pString to indicate a.m. or p.m.%rTime in %I:%M:%S %p (12-hour) format.%sSeconds since "the Epoch," which is 1970-01-01 00:00:00 UTC (a nonstandard extension).%tInsert a tab.%wDay of week (Sunday = 0).%xCountry-specific date format based on locale.%yLast two digits of year (00-99).%zRFC 822-style numeric time zone.%AFull weekday.%BFull month name.%DDate in %m/%d/%y format.%HHour in 24-hour format (00-23).%IHour in 12-hour format (01-12).%MMinutes (00-59).%SSeconds (00-59).%TTime in %H:%M:%S format.%UWeek number in year (00-53); start week on Sunday.%VWeek number in year (01-52); start week on Monday.%WWeek number in year (00-53); start week on Monday.%XCountry-specific time format based on locale.%YFour-digit year (e.g., 2006).%ZTime-zone name.Strings for setting dateStrings for setting the date may be numeric or nonnumeric. Numeric strings consist of time, day, and year in the format MMDDhhmm[[CC] YY] [.ss] . Nonnumeric strings may include month strings, time zones, a.m., and p.m.timeA two-digit hour and two-digit minute (hhmm); hh uses 24-hour format.dayA two-digit month and two-digit day of month (MMDD); default is current day and month.yearThe year specified as either the full four-digit century and year or just the two-digit year; the default is the current year.ExamplesSet the date to July 1 (0701), 4 a.m. (0400), 2005 (05):date 0701040095The command:date +"Hello%t Date is %D %n%t Time is %T"produces a formatted date as follows:Hello Date is 05/09/05 Time is 17:53:39 |