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. 【Mongodb】---关联表查询population

    Population MongoDB是非关联数据库.但是有时候我们还是想引用其它的文档.这就是population的用武之地. Population是从其它文档替换文档中的特定路径.我们可以迁移一个单 ...

  2. ContentProvider使用

    package activity.cyq.contentprovidertenderer; import android.content.ContentProvider; import android ...

  3. 最新32位和64位xp,win7,win8,win10系统大全(电脑装机版)

    一.系统主要特点 1.安装维护方便快速 - 全自动无人值守安装,采用万能GHOST技术,安装系统过程只需3-5分钟,适 合新旧各种机型. - 集成常见硬件驱动,智能识别+预解压技术,绝大多数硬件可以快 ...

  4. 关于Windows® API Code Pack for Microsoft® .NET Framework

    相比之前的操作系统,Window 7(or Vista)提供了很多新特性,我们在应用实现中可以利用这些特性来提升用户体验. 这些特性主要包括以下几个方面: Shell Enhancements Dir ...

  5. 两种获取connectionString的方式

    两种获取connectionString的方式 1. public static string connectionString = ConfigurationManager.ConnectionSt ...

  6. Java一些常见的出错异常处理

    一些平时常见的错误及解决办法,我 是新手,每次遇到的错误都记录了下来. 1. 404错误 description The requested resource (/Struts2_0100_Intro ...

  7. Codevs 1078 ==Poj 1258 Agri-Net

      Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53270   Accepted: 22140 Description D ...

  8. Java通过代理类实现数据库DAO操作

    下面的所有代码示例都取自李兴华的<Java Web开发实战经典>的随书源码,因为觉得设计得很好,所以将代码摘录下来作成笔记. 首先,我们在一个java文件中定义要存储的结构类型: impo ...

  9. 【风马一族_Java】在某个范围内,找出具有水仙花特征的数字

    打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如: 153是一个"水仙花数",因为153=1的三 ...

  10. mysql数据库创建database(实例),和用户,并授权

    前言:mysql创建用户的方法分成三种:INSERT USER表的方法.CREATE USER的方法.GRANT的方法. 一.账号名称的构成方式 账号的组成方式:用户名+主机(所以可以出现重复的用户名 ...