ruby 格式化当前日期时间

ruby 用Time类获取当前时间。

t = Time.new

puts t

可以看到输出的是(我现在运行的时间):

Sat Jan 29 10:45:22 +0800 2011

一般我们拿来用,都不会直接用这样“复杂”的时间格式。

下面通过Time的strftime方法来格式化处理,得到想到的日期格式。

1.比如你想获取 “2011-01-29” 这样的日期格式

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

2.比如你想获取“2011年01月29日 星期六 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

ruby 格式化当前日期时间的更多相关文章

  1. JS获取当前日期时间及JS日期格式化

    Js获取当前日期时间: var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份( ...

  2. Smarty 获取当前日期时间和格式化日期时间

    在Smarty 中获取当前日期时间和格式化日期时间与PHP中有些不同的地方,这里就为您详细介绍: 首先是获取当前的日期时间:在PHP中我们会使用date函数来获取当前的时间,实例代码如下:date(& ...

  3. Python 日期和时间_python 当前日期时间_python日期格式化

    Python 日期和时间_python 当前日期时间_python日期格式化 Python程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能. Python 提供了一个 time 和 cal ...

  4. Js获取当前日期时间及其它操作

    Js获取当前日期时间及其它操作var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); //获取完整的年份 ...

  5. Js获取当前日期时间及其它格式化操作

    Js获取当前日期时间及其它操作 var myDate = new Date();myDate.getYear();        //获取当前年份(2位)myDate.getFullYear();   ...

  6. Js获取当前日期时间及时间相关操作

    Js获取当前日期时间及时间格式 var myDate = new Date();myDate.getYear();        //获取当前年份(2位)myDate.getFullYear();   ...

  7. 格式化日期时间字符串 Get-Date -Uformat , -format

    #将字符串格式化为时间格式 $dateTimeStr = '20141231T23:59:59' $format = 'yyyyMMddTHH:mm:ss' $formatProvider = [Gl ...

  8. 【转】Js获取当前日期时间及其它操作

    Js获取当前日期时间及其它操作 原文地址:http://www.cnblogs.com/carekee/articles/1678041.html var myDate = new Date();my ...

  9. [转]Js获取当前日期时间及其它操作

    转载自:http://www.cnblogs.com/carekee/articles/1678041.html Js获取当前日期时间及其它操作 var myDate = new Date();myD ...

随机推荐

  1. ElasticSearch 高可用分布式集群搭建,与PHP多线程测试

    方案: 使用HAproxy:当其中一台ElasticSearch Master宕掉时,ElasticSearch集群会自动将运行正常的节点提升为Master,但HAproxy不会将失败的请求重新分发到 ...

  2. PHP浮点数的精度

    在百度知道上看到这么一个问题 var_dump((0.3-0.2)==0.1); 结果是:false 后来查查手册,原来是浮点数的精度问题.那么0.3-0.2-0.1等于多少呢,结果:2.775557 ...

  3. C# 获取文件名及扩展名

    C# 获取文件名及扩展名 string aFirstName = aFile.Substring(aFile.LastIndexOf("\\") + 1, (aFile.LastI ...

  4. 读书笔记-常用设计模式之MVC

    1.MVC(Model-View-Controller,模型-视图-控制器)模式是相当古老的设计模式之一,它最早出现在SmallTalk语言中.MVC模式是一种复合设计模式,由“观察者”(Observ ...

  5. MYSQL基础03(日期函数)

    工作中对日期的处理是经常遇到的,需求可能多种多样,因此重点介绍. 1.获取当前日期 select NOW() -- 结果:2015-10-28 22:41:11 ),NOW() -- 结果 2015- ...

  6. startup.bat闪退---tomcat环境变量配置中遇到的问题

    常用的方法: 1.在已解压的tomcat的bin文件夹下找到startup.bat,右击->编辑.在文件头加入下面两行: SET JAVA_HOME=D:\Java\jdk1.6.0_10   ...

  7. java web 实现验证码

    验证码的作用:通常的登录或者注册系统时,都会要求用户输入验证码,以此区别用户行为和计算机程序行为,目的是有人防止恶意注册.暴力破解密码等. 实现验证码的思路:用 server 实现随机生成数字和字母组 ...

  8. CentOS6.4下使用默认的PDF文档阅读器出现乱码的解决方案

    方法一:修改/etc/fonts/conf.d/49-sansserif.conf文件,如下: 1: <?xml version="1.0"?> 2: <!DOC ...

  9. MyBatis用嵌套ResultMap实现一对多映射

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3959451.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  10. 一个适用于层级目录结构的makefile模版

    今天写了个层次化的Makefile模版,用来自动化编译项目,这个模版应当包含以下功能: 适用于层次化结构,Makefile主要内容都放在顶层目录下的Makefile.env中,子层Makefile包含 ...