ruby 将日期转化为时间

Ruby数据和时间类 (Ruby Data and Time Classes)

In any program written in any language, at any point of time you may need to fetch the date and time of the system at that instant. Ruby facilitates you with three classes related to Date and Time namely:

在用任何语言编写的任何程序中,您可能需要在任何时间获取该时刻的系统日期和时间。 Ruby通过与日期和时间相关的三个类为您提供便利:

  • Date

    日期

  • DateTime

    约会时间

  • Time

    时间

If you are working with dates, you are required to understand some of the terms which are related to date such as:

如果使用日期,则需要了解一些与日期有关的术语,例如:

  1. Calendar date: You must be familiar with the term. It is a specific day of a calendar month of a distinct year.

    日历日期 :您必须熟悉该术语。 它是不同年份的日历月的特定日期。

  2. Ordinal date: When you identify a specific date of a year with the help of an ordinary year. It is an ordinal date.

    原始日期 :当您借助普通年份确定特定的日期时。 这是一个序数日期。

  3. Week date: It is the combination of calendar week and number of days. The week that includes the first Thursday of the distinct year is the first calendar week of that year.

    周日期 :是日历周和天数的组合。 包括不同年份的第一个星期四的一周是该年的第一个日历周。

  4. Julian day number: It is a kind of progression of date since noon on January 1, 4713 BCE.

    朱利安天数 :是公元前4713年1月1日中午以来日期的一种演变。

Now, let us talk about all those three classes related to date and Time which we have mentioned above in a detailed manner.

现在,让我们详细讨论上面提到的与日期和时间相关的所有这三个类。

1)日期 (1) Date)

Date class objects are mutable means that they cannot modify themselves at any point of time during the execution process. You will need to include date class in Ruby code before implementing date objects. You can create a date object with ::new, ::parse, ::today, ::jd, ::strptime.

日期类对象是可变的,意味着它们无法在执行过程中的任何时间修改自己。 在实现日期对象之前,您将需要在Ruby代码中包括日期类 。 您可以使用:: new , :: parse , :: today , :: jd和:: strptime创建日期对象。

Now let us see, how date objects are created with the help of an example.

现在让我们看看如何借助示例创建日期对象

=begin
Ruby program to show the implementation of
date class.
=end
require 'date'
puts Date.new(2020,4,12)
puts Date.jd(2451877)
puts Date.ordinal(2020,3)
puts Date.commercial(2020,12,6)
puts Date.parse('2020-02-03')
puts Date.strptime('03-02-2020', '%m-%d-%Y')

Output:

输出:

2020-04-12
2000-11-28
2020-01-03
2020-03-21
2020-02-03
2020-03-02

Here,

这里,

  1. new: new method will create a new object depending upon the parameters passed.

    new :new方法将根据传递的参数创建一个新对象。

  2. jd: It is provided with the local time and returns the Julian day number.

    jd :提供当地时间,并返回儒略日。

  3. ordinal: It creates a date object depending upon the parameter passed. The parameter may be negative or positive but can never be zero.

    ordinal :根据传递的参数创建日期对象。 参数可以是负数或正数,但永远不能为零。

  4. commercial: It creates a date object depending upon the provided week date. The parameter can be negative or positive but can never be zero.

    commercial :根据提供的星期日期创建日期对象。 参数可以是负数或正数,但永远不能为零。

  5. parse: It creates a date object and parses the representation of date and time.

    parse :它创建一个日期对象并解析日期和时间的表示形式。

  6. strptime: It returns a hash of parsed elements after parsing the provided representation of date of time along with the specified template.

    strptime :在解析提供的时间日期表示形式和指定模板之后,它返回解析元素的哈希值。

2)日期时间 (2) DateTime)

This class is a subclass of Date class. You can create an object of DateTime class with the help of DateTime.new, DateTime.ordinal, DateTime.parse, DateTime.jd, DateTime.commercial, DateTime.now, and so on. Let us see its example:

此类是Date类的子类。 您可以在DateTime.new , DateTime.ordinal , DateTime.parse , DateTime.jd , DateTime.commercial , DateTime.now等帮助下创建DateTime类的对象。 让我们看一下它的例子:

=begin
Ruby program to show the implementation of
DateTime class.
=end
require 'date'
d = DateTime.parse('12th Oct 2020 13:37:05+05:40')
puts d.hour
puts d.min
puts d.sec
puts d.offset

Output

输出量

13
37
5
17/72

In the above code, we are storing date in an object after parsing it. Then we are calling its multiple methods like an hour, min, sec and offset.

在上面的代码中,我们将日期解析后存储在对象中。 然后我们调用它的多个方法,例如小时 , 分钟 , 秒和偏移量 。

3)时间 (3) Time)

Time class is an abstraction of Date and DateTime class. You can create its objects with the help of ::new which will use the current system's time. You can pass year, month, day, hour, min, sec in the parameter list. An example is given below:

时间类DateDateTime类的抽象。 您可以在:: new的帮助下创建其对象,这将使用当前系统的时间。 您可以在参数列表中传递year , month , day , hour , min , sec 。 下面是一个示例:

=begin
Ruby program to show the implementation of
Time class.
=end
time1 = Time.new
puts "Printing Current Time: "+ time1.inspect
puts time1.year
puts time1.month
puts time1.day
puts time1.hour
puts time1.min
puts time1.sec
puts time1.zone

Output

输出量

Printing Current Time: 2019-09-25 06:26:58 +0530
2019
9
25
6
26
58
India Standard Time

翻译自: https://www.includehelp.com/ruby/date-and-time-classes.aspx

ruby 将日期转化为时间

ruby 将日期转化为时间_Ruby中的日期和时间类相关推荐

  1. matlab得到计算机时间,Matlab中处理日期与时间的函数

    Matlab用三种格式来表示日期与时间 (1)双精度型日期数字:一个日期型数字代表从公元0年到某一日期的的天数,例如,2008年8月26日夜时0点钟被表示为733646,而这同一天的中午12点就被表示 ...

  2. java 正则表达式 判断 日期_怎么在java中利用正则表达式对时间日期进行判断

    怎么在java中利用正则表达式对时间日期进行判断 发布时间:2020-12-08 16:18:34 来源:亿速云 阅读:105 作者:Leah 这篇文章给大家介绍怎么在java中利用正则表达式对时间日 ...

  3. java中定义一个时间_Java中的日期和时间

    根据个人目前正在编写的<Java核心编程-基础篇>的规划,这部分属于第15章内容,这里沿用了书中的章节编号.另外,因为原文篇幅太长,这里适当做了删减. 15.1.概述 在 Java 8 之 ...

  4. php中的点击时间,PHP中的日期及时间_php

    php有很多便于使用的函数以显示及处理日期. 要以某种特定格式显示日期或时间,可使用date()函数.它有两个参数:如何显示日期的格式以及代表你所要显示日期的时间戳.这个时间戳必须是先前所提到的从 1 ...

  5. java中取系统时间_JAVA中获取当前系统时间(示例代码)

    一. 获取当前系统时间和日期并格式化输出: import java.util.Date;import java.text.SimpleDateFormat; public class NowStrin ...

  6. 在java中获取当前时间_JAVA中获取当前系统时间

    一. 获取当前系统时间和日期并格式化输出: import java.util.Date; import java.text.SimpleDateFormat; public class NowStri ...

  7. java 获得当前时间_JAVA中获取当前系统时间

    一. 获取当前系统时间和日期并格式化输出: import java.util.Date; import java.text.SimpleDateFormat; public class NowStri ...

  8. java date当前时间_JAVA中获取当前系统时间

    一. 获取当前系统时间和日期并格式化输出: import java.util.Date; import java.text.SimpleDateFormat; public class NowStri ...

  9. java输出当前时间_JAVA中获取当前系统时间 - Matrix54 - 博客园

    JAVA中获取当前系统时间 - Matrix54 - 博客园 一. 获取当前系统时间和日期并格式化输出: import java.util.Date; import java.text.SimpleD ...

最新文章

  1. MapReduce简述、工作流程
  2. 【论文解读】A Survey on Visual Transformer及引文理解
  3. GDCM:获取SubSequence数据的测试程序
  4. linux可以http安装么,Linux 5下 http的安装
  5. 【python】yaml文件操作
  6. ajax调用方式汇总
  7. 强化学习中价值迭代和策略迭代各有什么优缺点?
  8. 这款应用,让Windows软件也能在Mac上使用起来
  9. swift 将图片资源打包成Bundle
  10. windows彻底删除php,win10如何彻底删除wps
  11. [IOI2018]-day1 简要题解
  12. 孩子不听话家长怎么办
  13. qq邮件服务器名字怎么填,qq邮箱名称应该填写什么?(写qq昵称不好使)
  14. 2014年IT互联网行业薪酬待遇
  15. Babel学习之基础学习
  16. Redmine3.3.3 搭建与不完全填坑指南
  17. Chrome 谷歌分页打印 去除页眉页脚 点击取消或打印关闭当前窗口
  18. ARM嵌入式主板之路
  19. 纪中DAY8做题小结
  20. 使用 Python+request 实现登入淘宝

热门文章

  1. java编写一个集合类_Java集合类
  2. Linux基础(存储结构和磁盘划分)
  3. Linux基础(用户身份和文件权限)
  4. Python二级笔记(7)
  5. MySQL(8)存储过程和函数
  6. Anaconda安装Tensorflow环境
  7. java版spring cloud+spring boot 社交电子商务平台:服务消费(基础)
  8. Linux sudoers文件的写法
  9. js:插入节点appendChild insertBefore使用方法
  10. 内存缓存LruCache实现原理