Time类

更新: 2017/06/23 更新了Data/Time在model模式下的便利方法
更新: 2018/10/12 修改了%Y相关描述防止误解
 年月日时分秒,时区  
 生成

 获取当前时间  Time.new()
 Time.now()
 生成  Time.mktime(2017, 5, 22, 0, 15, 15)
 年,月,日,时,分,秒,微秒
 可省略除年以外的参数
 经过时间  Time.at(12345)
 单位:秒
 起点:1970/01/01/0/0/0
   
   
 获取属性的值  t = Time.new()

 t.year  年
 t.month   月
 t.day  日
 t.hour  小时
 t.min  分
 t.sec  秒
 t.usec  秒以下的量
 微秒单位
 t.nsec  秒以下的量
 纳秒
 t.to.i  1970年1月1日0秒
 开始经过的秒数
 t.wday  本周第几天
 周日为0
 t.mday   本月第几日
 
 t.yday  一年的第几天
 第一天为1
 t.zone  时区
 t.utc_offset  与标准时的时差
 单位为秒
   
 比较  t1 = Time.new()
 t2 = Time.mktime(2018, 1, 1)
 输出  t.to_s()
 t.strftime(format)

 format  字符串
 %A  星期
 Sunday...
 %a  星期简称
 Sun, Mon, ... 
 %B  月
 January, ...
 %b  月简称
 Jan, Feb, ... 
 %c

Wed Jun 21 02:11:47 2017

 %d  日(01~31)
 %H  时  
 24小时制
 %I  时  
 12小时制
 %j  一年的第几天
 001~366
 %M  分 00~59
 %m  月 01~12
 %p  上下午 AM,PM
 %S   秒 00~60
 %U  周  星期天为起点
 %W  周 星期一为起点
 %w  星期几 0~6
 周日为0
 %X  时间
 %x  日期
 %Y  阳历年 2017
 %y  阳历年后两位 00~99
 %Z  时区 JST
 和电脑本地时区一致
 %z  时区 +900
 %%  % 
   
   
   
 转换

 t.uct()   这个会改变本身
 t.getutc()
 转换为标准时
 t.localtime()
 t.getlocal()
 转换为本地时区时
 t.to_date()  转为日期 
 d.to_time()  转为时间
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
 time库  require "time"
 邮件日期格式  t.rfc2822()
 Request For Comments 的RFC2822而来

Mon, 22 May 2017 02:39:13 +0900

 国际标准表示  t.iso8601() 
2017-05-22T02:39:13+09:00
 文字转日期

 Time.parse("2017/1/1")
 2017-01-01 00:00:00 +0900
 对应大部分情况 
 Time.strptime(str, format)  自定义配对的格式
   
   
   
   
   
   
   
   
   
   
 
Date类
 年月日  
 require "date"  
 生成

 Date.today()  获取今日
 Date.new(2015, 1, 1)
 月末一天 -1
 倒数第二天 -2
 指定日期
   
   
   
 运算

 减法  获取天数
 结果是有理数Rational(...)
 加法
 只加数字
 获取过后的日期
 >> n  到n月后的同一天
 超过范围就最后
 << n  到n月前的同一天
 输出  t.to_s()
 t.strftime("%Y/%m/%d") 
  
 转换

 Time.parse("2017/1/1")
 2017-01-01 00:00:00 +0900
 对应大部分情况 
 Time.strptime(str, format)  自定义配对的格式
 t.to_date()  转为日期 
 d.to_time()  转为时间
   
   
   
   
 Data/Time相关的有用的方法
 yesterday  昨天
 tomorrow  明天
 prev_xxxx  前年/月/周(year,month,week)
 next_xxxx  下年/月/周(year,month,week)
 beginning_of_xxxx  年/季/月/周的开始一天(year, quarter, month, day)
 end_of_xxxx  年/季/月/周的最后一条(year, quarter, month, day)
   
 n.xxx.ago
 Numeric
 n个年/月/日/时/分/秒以前
years, months, days, hours, minutes, seconds
也可以用单数
 n.xxx.from_now
 Numeric
 n个年/月/日/时/分/秒以后
years, months, days, hours, minutes, seconds
也可以用单数

Ruby Time类和Date类的更多相关文章

  1. Math类、Random类、System类、BigInteger类、BigDecimal类、Date类、SimpleDateFormat、Calendar类

    Math类* A:Math类概述 * Math 类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函数. * B:成员方法 * public static int abs(int a)  ...

  2. ruby Time类与Date类

    Time类用于表示时间.时间除了表示年月日时分秒的信息外,还包含了表示地域时差的时区(time zone)信息.例如我们可以计算中国当前时间是国际协调时间的几点 Date类只用于表示年月日.因此,相对 ...

  3. 14-03 java BigInteger类,BigDecimal类,Date类,DateFormat类,Calendar类

    BigInteger类 发 package cn.itcast_01; import java.math.BigInteger; /* * BigInteger:可以让超过Integer范围内的数据进 ...

  4. 正则表达式、Calendar类、SimpleDateFormat类、Date类、BigDecimal类、BigInteger类、System类、Random类、Math类(Java基础知识十四)

    1.正则表达式的概述和简单使用 * A:正则表达式(一个字符串,是规则)     * 是指一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串.其实就是一种规则.有自己特殊的应用. * B: ...

  5. 处理时间的类 —— System类、Date类 、SimpleDateFormat类 与 Calendar类

    在我们以往的编程中,就有过通过运行前和运行后时间差来判断时间复杂度的例子,再扯得远一点,我们在C语言中制造随机数的操作,也要用到有关时间的函数.而且,在我们未来的编程中,也会时不时要用到能够读取当前时 ...

  6. 时间日期相关类:Date类,DateFormat类&SimpleDateFormat类,Calendar类

    日期相关类 1.Date类:日期时间类 A.构造方法 Date(): 根据当前系统时间创建日期对象 ​ Date(Long time):根据传入的毫秒值时间创建日期对象 B.成员方法 Long get ...

  7. Object类、Date类、Calendar类、System类、StringBuilder类和基本类型包装类

    一.Object类--toString方法 1.普通类重写toString方法,不然打印出来是存在栈内存的对象引用名称的堆内存中该对象的地址值: 2.equals方法: String比较equals是 ...

  8. C++实现日期类(Date类)

    #include<iostream>using namespace std;class Date{public:    Date(int year = 1900, int month = ...

  9. String类与Date类的转换

    public class DateTest { public static void main(String[] args) throws ParseException { Date date = n ...

随机推荐

  1. webmagic使用手册

    https://my.oschina.net/flashsword/blog/180623 重点 SeleniumDownloader 对于一些Javascript动态加载的网页,仅仅使用http模拟 ...

  2. linux 开机启动脚本或者服务

    https://blog.csdn.net/zhuchunyan_aijia/article/details/53811368

  3. Multiply Strings(字符串乘法模拟,包含了加法模拟)

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  4. CodeForces 582A【multiset使用样例】

    题意: 给一些无序的数字,求解一个矩阵,使得矩阵的每一个元素都是行和列标志数的gcd,输出行标志数. 首先对数字进行排序.复杂度n*log(n^2). 这题的证明有官方的英文题解==在这直接贴英文题解 ...

  5. SSM!这就是你要的条条框框!

    第一次写博 1.导jar包 2,.表和实体类 实体类:com.bao.entity[Student] private int stuNo; private String stuName; privat ...

  6. linux命名详解及其软件安装实例

    始于cd,ls命令 好啦,步入正题,我使用的linux连接工具为xshell,mRemoteNG,对两款工具不做介绍啦,你可以百度一下,实在不会入左上方群. 进入之后,便是上面的界面黑乎乎一片,对于初 ...

  7. linux上安装启动elasticsearch-5.5.1完整步骤

    linux上安装启动elasticsearch-5.5.1完整步骤 学习了:https://blog.csdn.net/hingcheung/article/details/77144574 http ...

  8. Windows7系统下优化固态硬盘

    一.AHCI硬盘模式可提高硬盘性能,确定你的固态硬盘是运行在AHCI模式下,打开“HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Servicesmsahci” ...

  9. LUA协程复用

    -----协程复用根函数 local function routine(fun, args) while (fun) do fun, args = coroutine.yield(fun(table. ...

  10. [RxJS] Implement RxJS `concatMap` by Waiting for Inner Subscriptions to Complete

    Unlike mergeMap and switchMap, concatMap focuses on when "inner" subscriptions "compl ...