`
yacole
  • 浏览: 237987 次
  • 性别: Icon_minigender_1
  • 来自: 浙江科技学院
社区版块
存档分类
最新评论

rails时间显示

阅读更多

---------

At lunch today I wanted to add today’s date onto a Rails page.

<!---->

works relatively well enough, except it gives the date in a pretty ugly format… like this:

Thu Aug 31 13:51:33 US Mountain Standard Time 2006

I don’t need that much information and I’d like the day and month to be fully spelled out. I figured that reformatting the date should be awfully simple. It is, but it isn’t straightforward and it was difficult to find any documentation about it.

I looked in the rdoc for the ruby API. I looked at the “Time” class and just didn’t see what I was looking for. There’s so much there, I just didn’t look hard enough. I looked in the pick-axe book. No help there either. Finally, I went back to my copy of the rails mailing list and found the answer! Then I went back into the API and found it there as well.

The magic method is strftime. Find the time class in the api and scroll waaaaaaaaaaaaaaaaay down to strftime. With this method, you can pretty much spit out time and date strings however you’d like. Here’s what the API says:

time.strftime( string ) => string

Formats time according to the directives in the given format string. Any text not listed as a directive will be passed through to the output string.

Format meaning:

%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

t = Time.now
t.strftime("Printed on %m/%d/%Y") #=> "Printed on 04/09/2003"
t.strftime("at %I:%M%p") #=> "at 08:56AM"

So, once found it was easy enough. Here’s what I put in my .rhtml file:

<!---->

which spits out: “Thursday August 31, 2006”.

Also, this:

<!---->

spits out something like: “01:51 PM”.

Again, this is probably very elementary for most RubyNoob readers, but I’ve posted it in part as a reference for me to easily find when I need it again.

I was really sure that Rails had some quick/easy way to do this, but I couldn’t find it. If there’s an easier Railsish way to do this, please let me know in the comments. Thanks!

分享到:
评论

相关推荐

    Ruby-localtimeRails引擎向用户用当地时间格式来显示时间和日期

    local_time - Rails引擎向用户用当地时间格式来显示时间和日期

    使用Ruby on Rails开发LBS网站初探示例代码

    这几天一直在学习使用RoR(Ruby on Rails),想建立一个功能全面一点的LBS(Location Based Service)网站。但是对于我这个RoR的初学者(仅有几天时间)来说,毕竟太复杂了。因此本文试图简化原来的设计思路,抛弃一切...

    Web开发敏捷之道-应用Rails进行敏捷Web开发-第三版.rar

    16.6 时间和日期的扩展 208 16.7 Ruby符号的扩展 210 16.8 with_options 211 16.9 Unicode支持 211 第17章 数据迁移 215 17.1 创建和运行迁移任务 216 17.2 剖析迁移任务 218 17.3 表的管理 221 17.4 数据迁移任务 ...

    ActiveModel和Rails的日期和时间验证插件。 支持多个ORM,并允许自定义日期/时间格式。-Ruby开发

    ValidatesTimeliness¶↑来源:github.com/adzap/validates_timeliness问题:github....功能¶↑将日期,时间和日期时间的验证添加到ActiveModel Handles时区,并为您键入值的强制转换Only Rails日期/时间

    shog:Rails 4和5应用程序的简单彩色日志记录

    Shog添加了一些颜色来突出显示上下文,并使其更容易挑选出消息的重要部分。 像这样的未格式化日志 变得易于准备和处理这样的日志 使用粗毛 只需将以下内容添加到您的Gemfile中即可启用闪亮的输出日志。 gem '...

    rails-health-care:一个显示医疗保健公司营业时间的应用程序

    Small Rails应用程序旨在显示医生/医疗实践的开放时间。 简单响应的设计与小屏幕兼容。 可以选择日期和营业时间将新医生添加到列表中。 科技类 icon由

    ParseCSV:Rails应用解析CSV文件

    在项目目录中运行: $rails s我的方法思考过程当我得到问题陈述时,这些是我最初的指导性问题: 从用户的角度来看,真正的问题是什么? 成功会是什么样? 我需要测试什么才能确保其正常工作? 我将如何对数据建模?...

    tracklog-rails:Tracklog是用于管理GPS跟踪日志的Rails应用程序

    仪表板显示整体统计信息 按距离和持续时间划分的过去 12 个月活动的图表 GPX进出口 基本用户管理 屏幕截图 (注意:由于Tracklog正在开发中,因此屏幕截图可能并不总是代表当前状态。) 安装 由于Tracklog是标准的...

    highstocks-rails:在 Rails 应用程序中包含 highstocks 的库

    Highstock 允许您使用纯 JavaScript 创建股票或一般时间线图表,包括复杂的导航选项,如小型导航器系列、预设日期范围、日期选择器、滚动和平移。 Highstock 不可免费用于商业用途,因此请确保您拥有使用 ...

    heatmap-rails:针对网站上点击和用户关注区域的完整解决方案。 它包括存储和显示热图相关数据的功能

    将热图集成到Web应用程序中,以查看用户在Web应用程序上花费最多时间的部分。 用户在哪里单击页面。 帮助收集分析信息,以找出什么在网络上起作用,什么吸引了大多数用户。 查看用户交互,使您的应用程序更加出色! ...

    rails-sms-voting:基于Rails的SMS投票应用程序

    #使用Chartist和Rails构建SMS投票系统在我们举办的应用宣传晚会上,我希望与会者可以通过一种简单... 该应用程序的其余部分是基本的Rails应用程序。 您可以在上的GitHub上找到我的应用程序的完整源代码。 ##处理传入的

    facture:Ember.js Rails 4 API发票管理应用程序

    Facture使用Ember.js和Rails API后端对我原来的仅Rails发票管理应用程序进行了重写。 它使用来呈现PDF发票。 特征 按项目创建,编辑和显示发票 按月显示迄今为止的年收入,转换为默认货币 使用渲染发票 重复现有...

    genetic-startups-web:代表初创企业生命的遗传算法(用Ruby on Rails + React编写)

    该算法可在几代人的时间内改进启动选择,以实现可能的最成功结果。 在地图上显示投资者,产品发布,团队成员,不幸的消息和销售以及其他选项。 配置 使用以下项目创建该项目: rails new . --webpack=react rails ...

    wdi_10_rails_query_optimization

    这是非常低效的,因为从 Ruby 到 Postgres 的每次往返都需要时间。 预加载或缓存您知道需要的数据会好得多,因此无论显示多少记录,数据库往返次数都是一个很小的常数。 一个典型的例子可能是显示一个文章列表,每...

    logjam_app:logjam插件的Rails应用程序包装

    Logjam可以生成各种图形和表格,显示应用程序的不同部分消耗了多少时间和内存。 该界面足够灵活,能够为您可能对应用程序提出的许多问题提供答案,例如 我的应用程序中最需要优化的操作是什么? 在最坏的情况下,我...

    ratestuf_on_rails:将原始PHP Web应用程序迁移到Ruby on Rails

    我们的品牌地图是动态的,因此随着用户对任何品牌相对于其当前竞争对手的质量和价格变化的评估,它会随着时间而变化。 由于市场在不断变化,因此我们地图上的品牌也会发生变化,以反映用户不断变化的观念。 谢谢你...

    eventor:Ruby on Rails活动计划系统

    从头开始在Rails中制作的事件管理Web应用程序。 角色:活动组织者,场地所有者,服务提供商和普通用户。 设置: 克隆仓库 运行捆绑安装 运行rake db:setup 生成一些伪数据:运行rake setup_data可选),使用rake ...

    Rails-project-furniture-marketplace

    Ruby on Rails 赫鲁库 AWS S3 传单和地图框 PostgreSQL 特征 按关键字搜索列表 通过多种条件过滤列表 保存清单 显示清单位置 管理仪表盘 用户认证和授权 图片上传。 状态 项目完成时间: 用法 通过顶部的URL访问该...

    rails_cli_sample

    在 rails 应用程序中使用 CLI 进行示例 登录后,将显示任务列表屏幕,并在顶部显示命令行栏。所以 new task or new task タスク追加 您可以通过执行诸如以下命令来基于字符控制 Web 服务。 如果我有时间,我希望能够...

Global site tag (gtag.js) - Google Analytics