xml地图|网站地图|网站标签 [设为首页] [加入收藏]

格式化当前日期时间,ruby中时间日期用法详解

来源:http://www.ccidsi.com 作者:集成介绍 人气:187 发布时间:2019-10-04
摘要:做web应用,和岁月打交道是不可免的。rails对ruby的光阴模块做了扩充。本文作于二零一二年1十二月22日,ruby版本为1.8.7, rails版本为3.0.3 ruby 格式化当前些天期时间 ruby 用Time类获取当前时

做web应用,和岁月打交道是不可免的。rails对ruby的光阴模块做了扩充。本文作于二零一二年1十二月22日, ruby版本为1.8.7, rails版本为3.0.3

ruby 格式化当前些天期时间

ruby 用Time类获取当前时光。

t = Time.new

puts t

能够观望输出的是(小编前日运作的时日):

Sat Jan 29 10:45:22 0800 2011

 

相似我们拿来用,都不会直接用如此“复杂”的年华格式。

上边通过提姆e的strftime方法来格式化管理,获得想到的日子格式。

1.例如你想猎取 “二零一一-01-29” 那样的日期格式

t = Time.new
date = t.strftime("%Y-%m-%d")
puts date    #2011-01-29

 

2.比如你想赢得“二〇一二年0七月三日 星期日 10:50 AM” 那样的格式

dayOfWeek = [ "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" ]
t = Time.new
puts t
date = t.strftime("%Y年%m月%d日 ") << dayOfWeek[t.strftime("%w").to_i] << t.strftime(" %H:%M %p")
puts date    #2011年01月29日 星期六 10:50 AM

 

参数:

%a - The abbreviated weekday name (``Sun'')
%A - The full weekday name (``Sunday'')
%b - The abbreviated month name (``Jan'')
%B - The full month name (``January'')
%c - The preferred local date and time representation
%d - Day of the month (01..31)
%H - Hour of the day, 24-hour clock (00..23)
%I - Hour of the day, 12-hour clock (01..12)
%j - Day of the year (001..366)
%m - Month of the year (01..12)
%M - Minute of the hour (00..59)
%p - Meridian indicator (``AM'' or ``PM'')
%S - Second of the minute (00..60)
%U - Week number of the current year, starting with the first Sunday as the first day of the first week (00..53)
%W - Week number of the current year, starting with the first Monday as the first day of the first week (00..53)
%w - Day of the week (Sunday is 0, 0..6)
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%y - Year without a century (00..99)
%Y - Year with century
%Z - Time zone name
%% - Literal ``%'' character

a: AM/PM (上午/下午)
A: 0~86399999 (一天的第A纳秒)
c/cc: 1~7 (七日的首后天, 周天为1)
ccc: Sun/Mon/Tue/Wed/Thu/Fri/Sat (星期几简写)
cccc: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday (星期几全拼)
d: 1~31 (月份的第几天, 带0)
D: 1~366 (年份的第几天,带0)
e: 1~7 (十十九日的第几天, 带0)
E~EEE: Sun/Mon/Tue/Wed/Thu/Fri/Sat (星期几简写)
EEEE: Sunday/蒙德ay/Tuesday/Wednesday/Thursday/Friday/Saturday (星期几全拼)
F: 1~5 (每月的第几周, 七日的第一天为周二)
g: Julian Day Number (number of days since 4713 BC January 1) 未知
G~GGG: BC/AD (Era Designator Abbreviated) 未知
GGGG: Before Christ/Anno Domini 未知
h: 1~12 (0 padded Hour (12hr)) 带0的时, 12小时制
H: 0~23 (0 padded Hour (24hr)) 带0的时, 24小时制
k: 1~24 (0 padded Hour (24hr) 带0的时, 24小时制
K: 0~11 (0 padded Hour (12hr)) 带0的时, 12小时制
L/LL: 1~12 (0 padded Month) 第几月
LLL: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec 月份简写
LLLL: January/February/March/April/May/June/July/奥古斯特/September/October/November/December 月份全称
m: 0~59 (0 padded Minute) 分钟
M/MM: 1~12 (0 padded Month) 第几月
MMM: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec
MMMM: January/February/March/April/May/June/July/August/September/October/November/December
q/qq: 1~4 (0 padded Quarter) 第几季度
qqq: Q1/Q2/Q3/Q4 季度简写
qqqq: 1st quarter/2nd quarter/3rd quarter/4th quarter 季度全拼
Q/QQ: 1~4 (0 padded Quarter) 同小写
QQQ: Q1/Q2/Q3/Q4 同小写
QQQQ: 1st quarter/2nd quarter/3rd quarter/4th quarter 同小写
s: 0~59 (0 padded Second) 秒数
S: (rounded Sub-Second) 未知
u: (0 padded Year) 未知
v~vvv: (General 丙胺博莱霉素T Timezone Abbreviation) 常规奇霉素T时区的编纂
vvvv: (General 卡那霉素T 提姆ezone Name) 常规维生霉素T时区的称呼
w: 1~53 (0 padded Week of Year, 1st day of week = Sunday, NB: 1st week of year starts from the last Sunday of last year) 一年的第几周, 一周的发端为周天,第七日从二〇一八年的最后贰个周末起算
W: 1~5 (0 padded Week of Month, 1st day of week = Sunday) 半年的第几周
y/yyyy: (Full Year) 完整的年度
yy/yyy: (2 Digits Year) 2个数字的年份
Y/YYYY: (Full Year, starting from the Sunday of the 1st week of year) 这几个年度未知干嘛用的
YY/YYY: (2 Digits Year, starting from the Sunday of the 1st week of year) 这么些年份未知干嘛用的
z~zzz: (Specific 金霉素T Timezone Abbreviation) 钦点威他霉素T时区的编排
zzzz: (Specific 地霉素T Timezone Name) Z: 0000 (奥迪Q5FC 822 Timezone) 钦点GMT时区的称号

基本的时日转移
>> now=Time.now
=> Sat Jan 29 21:47:07 0800 2011
#utc秒数相互调换
>> now.to_i
=> 1296308827
>> Time.at(1296308827)
=> Sat Jan 29 21:47:07 0800 2011
#超过天子的某些变量
>> now.sec
=> 7
>> now.min
=> 47
>> now.hour
=> 21
>> now.month
=> 1
>> now.year
=> 2011
#前段时间是星期几(注意!!!周天是回来 0 )
>> now.wday
=> 6
#今昔是前一个月第几天
>> now.day
=> 29
#近期是当年第几天
>> now.yday
=> 29
#时间参数的数组
>> now.to_a
=> [7, 47, 21, 29, 1, 2011, 6, 29, false, "CST"]

Date Field Symbol Table
Field Sym. No. Example Description
era G 1..3 AD Era - Replaced with the Era string for the current date. One to three letters for the abbreviated form, four letters for the long form, five for the narrow form.
4 Anno Domini
5 A
year y 1..n 1996 Year. Normally the length specifies the padding, but for two letters it also specifies the maximum length. Example:
Year y yy yyy yyyy yyyyy
AD 1 1 01 001 0001 00001
AD 12 12 12 012 0012 00012
AD 123 123 23 123 0123 00123
AD 1234 1234 34 1234 1234 01234
AD 12345 12345 45 12345 12345 12345
Y 1..n 1997 Year (of "Week of Year"), used in ISO year-week calendar. May differ from calendar year.
u 1..n 4601 Extended year. This is a single number designating the year of this calendar system, encompassing all supra-year fields. For example, for the Julian calendar system, year numbers are positive, with an era of BCE or CE. An extended year value for the Julian calendar system assigns positive values to CE years and negative values to BCE years, with 1 BCE being year 0.
quarter Q 1..2 02 Quarter - Use one or two for the numerical quarter, three for the abbreviation, or four for the full name.
3 Q2
4 2nd quarter
q 1..2 02 Stand-Alone Quarter - Use one or two for the numerical quarter, three for the abbreviation, or four for the full name.
3 Q2
4 2nd quarter
month M 1..2 09 Month - Use one or two for the numerical month, three for the abbreviation, or four for the full name, or five for the narrow name.
3 Sept
4 September
5 S
L 1..2 09 Stand-Alone Month - Use one or two for the numerical month, three for the abbreviation, or four for the full name, or 5 for the narrow name.
3 Sept
4 September
5 S
week w 1..2 27 Week of Year.
W 1 3 Week of Month
day d 1..2 1 Date - Day of the month
D 1..3 345 Day of year
F 1 2
Day of Week in Month. The example is for the 2nd Wed in July
g 1..n 2451334 Modified Julian day. This is different from the conventional Julian day number in two regards. First, it demarcates days at local zone midnight, rather than noon GMT. Second, it is a local number; that is, it depends on the local time zone. It can be thought of as a single number that encompasses all the date-related fields.
week
day E 1..3 Tues Day of week - Use one through three letters for the short day, or four for the full name, or five for the narrow name.
4 Tuesday
5 T
e 1..2 2 Local day of week. Same as E except adds a numeric value that will depend on the local starting day of the week, using one or two letters. For this example, Monday is the first day of the week.
3 Tues
4 Tuesday
5 T
c 1 2 Stand-Alone local day of week - Use one letter for the local numeric value (same as 'e'), three for the short day, or four for the full name, or five for the narrow name.
3 Tues
4 Tuesday
5 T
period a 1 AM AM or PM
hour h 1..2 11 Hour [1-12].
H 1..2 13 Hour [0-23].
K 1..2 0 Hour [0-11].
k 1..2 24 Hour [1-24].
minute m 1..2 59 Minute. Use one or two for zero padding.
second s 1..2 12 Second. Use one or two for zero padding.
S 1..n 3457 Fractional Second - rounds to the count of letters. (example is for 12.34567)
A 1..n 69540000 Milliseconds in day. This field behaves exactly like a composite of all time-related fields, not including the zone fields. As such, it also reflects discontinuities of those fields on DST transition days. On a day of DST onset, it will jump forward. On a day of DST cessation, it will jump backward. This reflects the fact that is must be combined with the offset field to obtain a unique local time value.
zone z 1..3 PDT Timezone - Use one to three letters for the short timezone or four for the full name. For more information, see Appendix J: Time Zone Display Names
4 Pacific Daylight Time
Z 1..3 -0800 Use one to three letters for RFC 822, four letters for GMT format.
4 GMT-08:00
v 1 PT Use one letter for short wall (generic) time, four for long wall time. For more information, see Appendix J: Time Zone Display Names
4 Pacific Time
All non-letter character represent themselves in a pattern, except for the single quote. It is used to 'escape' letters. Two single quotes in a row, whether inside or outside a quoted sequence, represent a 'real' single quote.

时间化输出
>> now.strftime("%Y-%m-%d %H:%M:%S")
=> "2011-01-29 21:47:07"

http://www.cocoachina.com/bbs/read.php?tid-122098.html

参数解释如下

  %a - 星期几的斯拉维尼亚语简写 (``Sun'')
  %A - 星期几的匈牙利(Magyarország)语全称 (``Sunday'')
  %b - 月份的乌Crane语简写 (``Jan'')
  %B - 月份的阿尔巴尼亚语全称 (``January'')
  %c - 私下认可的首推本地时间输出格式
  %d - 上月第几天 (01..31)
  %H - 24时辰制的时辰 (00..23)
  %I - 12小时制的钟点 (01..12)
  %j - 二〇一七年的第几天 (001..366)
  %m - 月份 (01..12)
  %M - 分钟 (00..59)
  %p - 深夜要么早晨 (``AM''  or  ``PM'')
  %S - 秒数 (00..60)
  %U - 从礼拜日算五日最早的当年第几周 (00..53)
  %W - 从星期四算一日最初的当年第几周 (00..53)
  %w - 今后是星期几 (星期天是0 , 0..6)
  %x - 暗许的日子输出格式 ("01/49/11")
  %X - 私下认可的时日输出格式 ("21:47:07")
  %y - 年份的后两位 (00..99)
  %Y - 年份
  %Z - 时区名
  %% - 输出%字符

本文由68399皇家赌场发布于集成介绍,转载请注明出处:格式化当前日期时间,ruby中时间日期用法详解

关键词: 68399皇家赌场 iOS Ruby

上一篇:以及对应版本Ipython安装配置,0安装教程

下一篇:没有了

最火资讯