ruby 格式化当前日期时间
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 格式化当前日期时间的更多相关文章
- JS获取当前日期时间及JS日期格式化
Js获取当前日期时间: var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份( ...
- Smarty 获取当前日期时间和格式化日期时间
在Smarty 中获取当前日期时间和格式化日期时间与PHP中有些不同的地方,这里就为您详细介绍: 首先是获取当前的日期时间:在PHP中我们会使用date函数来获取当前的时间,实例代码如下:date(& ...
- Python 日期和时间_python 当前日期时间_python日期格式化
Python 日期和时间_python 当前日期时间_python日期格式化 Python程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能. Python 提供了一个 time 和 cal ...
- Js获取当前日期时间及其它操作
Js获取当前日期时间及其它操作var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); //获取完整的年份 ...
- Js获取当前日期时间及其它格式化操作
Js获取当前日期时间及其它操作 var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); ...
- Js获取当前日期时间及时间相关操作
Js获取当前日期时间及时间格式 var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); ...
- 格式化日期时间字符串 Get-Date -Uformat , -format
#将字符串格式化为时间格式 $dateTimeStr = '20141231T23:59:59' $format = 'yyyyMMddTHH:mm:ss' $formatProvider = [Gl ...
- 【转】Js获取当前日期时间及其它操作
Js获取当前日期时间及其它操作 原文地址:http://www.cnblogs.com/carekee/articles/1678041.html var myDate = new Date();my ...
- [转]Js获取当前日期时间及其它操作
转载自:http://www.cnblogs.com/carekee/articles/1678041.html Js获取当前日期时间及其它操作 var myDate = new Date();myD ...
随机推荐
- Flask Restful Small Demo
参考: http://www.pythondoc.com/flask-restful/first.html 什么是Rest Client-Server:服务器端与客户端分离. Stateless(无状 ...
- 详解JSTL的forEach标签
详解JSTL的forEach标签 为循环控制,它可以将集合(Collection)中的成员循序浏览一遍. <c:forEach> 标签的语法 说明 : 语法:迭代一集合对象之所有 ...
- js实现模拟自动点击按钮,并且在10秒倒计时之后疯狂点击
需求来自于csdn问答,可以利用这个原理做秒杀抢单外挂. 代码示例如下: <html> <head> <meta charset="utf-8"/&g ...
- Silverlight 中DataGrid中全选与非全选问题
问题:当点击全选时,全选所有的复选框,但是滚动屏幕时,却复选框就会取消选中 一.解决方法(将要展示的实体数据模型添加bool属性,在数据绑定时添加click时间,盘带选中的状态,就可以了) 1. xa ...
- linux中nodejs后台运行工具forever
forever让nodejs应用后台执行 命令如下: forever start './bin/www' nodejs一般是当成一条用户命令执行的,当用户断开客户连接,运用也就停了,很烦人.如何让no ...
- oracle中的存储过程例子
用了两年Oracle还没写过存储过程,真是十分惭愧,从今天开始学习Oracle存储过程,完全零起点,争取每日一篇学习笔记,可能开始认识的不全面甚至有错误,但坚持下来一定会有收获. . 建立一个存储过程 ...
- 小课堂Week11 会说话的代码
小课堂Week11 会说话的代码 今天主要讨论下,在编码过程中和"命名"相关的问题.因为命名方法比较自由,如果要提高可读性,我们需要尽量使其符合正规的英文语法习惯. 变量/属性 通 ...
- SetTimeOut jquery的作用
1. SetTimeOut() 1.1 SetTimeOut()语法例子 1.2 用SetTimeOut()执行Function 1.3 SetTimeOut()语法例子 1.4 设定条件使SetTi ...
- 源码编译安装MySQL 5.7.9
安装CentOS 6.3 配置yum:[root@hank-yoon ~]# cd /etc/yum.repos.d/ [root@hank-yoon yum.repos.d]# mkdir a [r ...
- Linux挂载60T存储
操作系统: CentOS 6.3 存储:总大小为72T,并划分成3个块,每块20T 安装多实例MySQL数据库,不想挂载3个块,弄成一个大的比较方便管理,个人比较懒. 配置多路径:http://blo ...