ruby Time类与Date类
Time类用于表示时间。时间除了表示年月日时分秒的信息外,还包含了表示地域时差的时区(time zone)信息。例如我们可以计算中国当前时间是国际协调时间的几点
Date类只用于表示年月日。因此,相对于Time类以秒为单位计算时间,Date类则是以天为单位进行计算的。Date类还可以求下个月的同一天,本月末等日期
Time
puts Time.new #=> 2017-07-26 22:41:42 +0800
puts Time.now #=> 2017-07-26 22:41:42 +0800 t = Time.now
puts t
puts t.year #=> 2017年
puts t.month #=> 7月
puts t.day #=> 26日
puts t.hour #=> 22时
puts t.min #=> 44分
puts t.sec #=> 7秒
puts t.usec #=> 524674毫秒
puts t.to_i #=> 1501080502从1970年1月1日到当前时间的秒数
puts t.wday #=> 3一周当中的第几天
puts t.mday #=> 26一个月当中的第几天
puts t.yday #=> 207一年当中的第几天
puts t.zone #=> 中国标准时间 时区
puts t.utc #=> 2017-07-26 15:10:07 UTC 转换为UTC时间
puts t.localtime #=> 2017-07-26 23:11:54 +0800 转换为本地时间
#根据指定时间获取事件对象
t = Time.mktime(2013, 3, 28, 2, 32, 12)
puts t #=> 2013-03-28 02:32:12 +0800 #Time类计算
t1 = Time.now
sleep 10
t2 = Time.now
puts t1<t2 #=> true
puts t2-t1 #=> 10.000197
puts t2, t2+24*60*60 #时间格式
t = Time.now
puts t.strftime('%Y-%m-%d %H:%M:%S %z') #=> 2018-05-10 02:14:08 +0800
puts t.to_s #=> 等价于'%Y-%m-%d %H:%M:%S %z' require 'time'
#转换为电子邮件头部的Date,需要导入time类
t = Time.now
puts t.rfc2822 #=> Wed, 26 Jul 2017 23:06:59 +0800 #转换为ISO国际标准时间的时间格式的字符串
t = Time.now
puts t.iso8601 #=> 2017-07-26T23:08:54+08:00 #字符串时间转换
puts Time.parse('Wed, 26 Jul 2017 23:06:59 +0800') #=> 2017-07-26 23:06:59 +0800
puts Time.parse('2013/3/21 12:45:56') #=> 2013-03-21 12:45:56 +0800Date
require 'date' d = Date.today
puts d #=> 2017-07-26 当前日期
puts d.year #=> 2017 年
puts d.month #=>7 月
puts d.day #=>26 日
puts d.wday #=> 3 一周的第几天
puts d.mday #=> 26 一个月中的第几天
puts d.yday #=> 207 一年中的第几天 #指定日期生成Date对象
d = Date.new(2013, 3, 30)
puts d #=> 2013-03-30 #日期运算
d1 = Date.new(2013, 1, 1)
d2 = Date.new(2013, 1, 4)
puts d2 - d1 #=> 3/test.text (3天的意思) d = Date.today
#+,-按天数计算
puts d #=> 2017-07-26
puts d + 1 #=> 2017-07-27
puts d + 100 #=> 2017-11-03
puts d - 1 #=> 2017-07-25
puts d - 100 #=> 2017-04-17 d = Date.today
#>>,<<按月数计算
puts d #=> 2017-07-26
puts d >> 1 #=> 2017-08-26
puts d >> 100 #=> 2025-11-26
puts d << 1 #=> 2017-06-26
puts d << 100 #=> 2009-03-26 #日期格式
t = Date.today
puts t.strftime('%Y/%m/%d %H:%M:%S') #=> 2017/07/26 00:00:00
puts t.strftime('%a %b %d %H:%M:%S %Z %Y') #=> Wed Jul 26 00:00:00 +00:00 2017
puts t #=> 2017-07-26 #字符串转换为日期
puts Date.parse('Wed Jul 26 00:00:00 +00:00 2017') #=> 2017-07-26# 时间日期相互转换
Time.new.to_date
Date.today.to_time
ruby Time类与Date类的更多相关文章
- Math类、Random类、System类、BigInteger类、BigDecimal类、Date类、SimpleDateFormat、Calendar类
Math类* A:Math类概述 * Math 类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函数. * B:成员方法 * public static int abs(int a) ...
- Ruby Time类和Date类
Time类 更新: 2017/06/23 更新了Data/Time在model模式下的便利方法 更新: 2018/10/12 修改了%Y相关描述防止误解 年月日时分秒,时区 生成 获取当前时 ...
- 14-03 java BigInteger类,BigDecimal类,Date类,DateFormat类,Calendar类
BigInteger类 发 package cn.itcast_01; import java.math.BigInteger; /* * BigInteger:可以让超过Integer范围内的数据进 ...
- 正则表达式、Calendar类、SimpleDateFormat类、Date类、BigDecimal类、BigInteger类、System类、Random类、Math类(Java基础知识十四)
1.正则表达式的概述和简单使用 * A:正则表达式(一个字符串,是规则) * 是指一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串.其实就是一种规则.有自己特殊的应用. * B: ...
- 处理时间的类 —— System类、Date类 、SimpleDateFormat类 与 Calendar类
在我们以往的编程中,就有过通过运行前和运行后时间差来判断时间复杂度的例子,再扯得远一点,我们在C语言中制造随机数的操作,也要用到有关时间的函数.而且,在我们未来的编程中,也会时不时要用到能够读取当前时 ...
- 时间日期相关类:Date类,DateFormat类&SimpleDateFormat类,Calendar类
日期相关类 1.Date类:日期时间类 A.构造方法 Date(): 根据当前系统时间创建日期对象 Date(Long time):根据传入的毫秒值时间创建日期对象 B.成员方法 Long get ...
- Object类、Date类、Calendar类、System类、StringBuilder类和基本类型包装类
一.Object类--toString方法 1.普通类重写toString方法,不然打印出来是存在栈内存的对象引用名称的堆内存中该对象的地址值: 2.equals方法: String比较equals是 ...
- C++实现日期类(Date类)
#include<iostream>using namespace std;class Date{public: Date(int year = 1900, int month = ...
- String类与Date类的转换
public class DateTest { public static void main(String[] args) throws ParseException { Date date = n ...
随机推荐
- Java 实现斐波那契数列
public class Fibonacci { private static int getFibo(int i) { if (i == 1 || i == 2) return 1; else re ...
- Git/Github Learning
通过网上查找资料,我了解到Git/Github是一款免费.开源的分布式版本控制系统,它可以敏捷高效地处理任何或小或大的项目.同时,它是一个开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的 ...
- 如何在SAP CRM WebClient UI里创建HANA Live Report
1. 使用业务角色ANALYTICSPRO登录WebClient UI: 2. 点击新建按钮: 为新建的报表分配一个HANA Live Query: 指定Query的参数: 上图WebClient U ...
- ACM-ICPC 2018 徐州赛区网络预赛 G. Trace【树状数组维护区间最大值】
任意门:https://nanti.jisuanke.com/t/31459 There's a beach in the first quadrant. And from time to time, ...
- Java中基本类型和引用类型(简单介绍)
8种基本类型 一.4种整型 byte 1字节 -128——127 short 2 字节 -32,768 —— 32,767 in ...
- Android学习笔记_69_android 支付宝之网页支付和快捷支付
参考资料: https://b.alipay.com/order/productDetail.htm?productId=2013080604609654 https://b.alipay.com/o ...
- PAT 1063. Set Similarity
1063. Set Similarity 题目大意 给定 n 个集合, k 个询问, 求任意两个集合的并集和合集. 思路 一道裸的考察 STL 中 set 的题, 我居然还用 hash 错过一遍, 用 ...
- generative models
A generative model G can be seen as taking a random seed h (say, a sample from a multivariate Normal ...
- o'Reill的SVG精髓(第二版)学习笔记——第二章
在网页中使用SVG 将SVG作为图像: SVG是一种图像格式,因此可以使用与其他图像类型相同的方式包含在HTML页面中,具体可以采用两种方法:将图像包含在HTML标记的<img>元素内(当 ...
- mac启动springboot失败,8080端口被占用,mac命令行关闭端口
如下图,idea启动springboot失败,8080端口被占用 Error starting ApplicationContext. To display the conditions report ...