`
peryt
  • 浏览: 54380 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论
  • waiting: 既然都指定了dataType为'script'那就不必特别在b ...
    jQuery

play with formating time.

 
阅读更多
如果我们在irb中输入:
Time.now
=>Thu May 21 14:31:35 +0800 2009

我们看到这个格式一般不是我们要显示出来的格式,之所以以这种格式显示出来,是因为调用了Time对象的to_s方法,而to_s方法可以接收一些参数,如果没有参数,那么显示出来的就是默认的上面那种格式,我们可以通过参数改变格式:
Format                         Result
Task.first.due_at.to_s 2009-02-19 00:00:00 UTC
Task.first.due_at.to_s(:long) February 19, 2009 00:00
Task.first.due_at.to_s(:short) 19 Feb 00:00
Task.first.due_at.to_s(:db) 2009-02-19 00:00:00

那么如果上面的格式都不是你想要的格式呢?
这就要用到Time对象的strftime这个方法了,要知道详细的什么字符代表什么格式,最好还是查documentation, ri Time.strftime.

比如如下的格式:
   1. <%= task.due_at.strftime("due on %B %d at %I:%M %p") %></li> 
显示出来的就是:
due on February 19 at 12:00 AM

但是如果我们的程序中多处用到这种格式,那么每次都写一行这样的代码显然是违背DRY原则的。
如果refactor呢,我们可以使用helper method, 也可以添加一个新的方法到task模型,但是这里我们采用另外一种方法,就是给to_s方法增加一种我们要的格式。(除了:long, :short, :db 之外)

   1. <%= task.due_at.to_s(:due_date) %></li> 

由于这种新的格式需要在我们的程序load之前就被载入,所以我们需要把他的定义放在environment.rb 中:
(如果有什么东西需要在程序载入之前就准备好,那么最好放在environment中),各种时间格式是作为一个常量hash存放在Time这个类中,TIME_FORMATS, 而hash的keys就是用来标志各种格式的symbol,因此我们只需要在该文件的最后一行加入:

   Time::DATE_FORMATS[:due_date] = "due on %B %d at %I:%M %p" 

然后重启我们的程序,搞定!
分享到:
评论

相关推荐

    Python 2.4 Quick Reference Card (Letter) (2007).pdf

    Formating..............................................6 Constants..............................................6 Regular Expressions..............................6 Localization..........................

    formating:一个简单的数据格式库

    格式化一个简单的数据格式库正在开发中安装格式化在npm上可用: npm install formating --save用法使用ES6 /导入 import { format , string } from 'formating'let toFormat = format ( string . trim )let result =...

    Excel 好用VBA代码

    在提供的压缩包文件中,有四个XLA文件(K_Formating.xla、K_Formating - copy.xla、K_Formating - copy -1.xla、图号整理程式【Drawing sorting program】.xla),它们可能是包含上述或其他VBA宏的模板或自定义函数...

    Flattern.zip

    * How to Use Moment js on Laravel (Used for formating date time) * How to Use sweetalert2 (Used for flash message) * How to Image intervation with Laravel (Used for image resize) * How to Use ...

    JCOP Formating Tool_tool_

    【JCOP格式化工具详解】 JCOP(Java Card Operating System)是智能卡操作系统的一种,主要用于安全芯片,如SIM卡、银行卡等。这些芯片通常包含了微控制器和存储单元,能够执行安全的Java应用程序。...

    UltraISO使用方法

    ### UltraISO使用方法详解 #### 一、创建ISO文件 - **步骤说明**: ... - 在软件界面左侧的“本地目录”中,根据需要切换到合适的文件夹路径。 - 将右侧需要包含在ISO文件中的文件或文件夹直接拖动至“光盘文件”...

    Vrp-Set-Li.zip_3123li.com_https www3123li com_matlab_vrp_www.312

    data set for formating vrp

    sunrise-challenge-backend:我对日出挑战的建议

    日出挑战后端。... logBonus.js # The bonus formating library server.js # main files for express test/ # All the test files 记录功能 为了获得完整的日志,将的DEBUG常量设置为 true 我也开始了

    iterdet:[S + SSPR2020] IterDet

    IterDet:拥挤环境中... mmdet /数据集/管道/formating.py mmdet /数据集/crowd_human.py mmdet / models / dense_heads / anchor_head.py mmdet / models / dense_heads / rpn_head.py mmdet / models / roi_hea

    String-Formating-Basics:这就是字符串格式化,逻辑播放的基础

    在这个“String-Formating-Basics”主题中,我们将深入探讨Python中的字符串格式化方法,特别关注与Jupyter Notebook相关的实践应用。 1. **基本格式化方法:`%`操作符** 在早期的Python版本中,`%`运算符被用来...

    Latex中文手册-汉译版本

    * Time for action-creating a macro for formating keywords:可以使用 Time for action 任务来创建用于格式化关键词的宏。 * Using optional arguments:可以使用可选参数来提高命令的灵活性。 LaTeX 是一个功能...

    编写C程序PPT课件.pptx

    class Formating { public static void Main() { Console.WriteLine("在宽度为10的空间里靠左对齐 :{0,-10}", 99); Console.WriteLine("在宽度为10的空间里靠右对齐 :{0,10}", 99); Console.WriteLine("在宽度为...

    海康威视sdk开发中返回的错误码

    NET_DVR_NETWORK_RECV_TIMEOUT (10) - 从设备接收数据超时 **含义**:在规定时间内没有接收到数据。 **解决方案**: - 增加超时时间设置。 - 检查网络延迟是否过高。 #### 12. NET_DVR_NETWORK_ERRORDATA (11) - ...

    Printed Circuits Handbook

    Information Formating and Exchange 18.1 18.1 Introduction to Data Exchange / 18.1 18.2 The Data Exchange Process / 18.3 18.3 Data Exchange Formats / 18.9 18.4 Drivers for Evolution / 18.22 18.5 ...

    VBA 我的学习积累

    首先,`K_Formating.xla`和它的副本可能是用来处理表格格式化的工具。VBA宏可以自动化复杂的格式设定,如单元格颜色、字体样式、边框样式等,这在处理大量数据时尤其有用。这些宏可能包含了根据特定条件进行格式化,...

    解决用Aspose.Words,在word文档中创建表格的实现方法

    代码如下所示: 代码如下: //Open document and create Documentbuilder Aspose.... //Set table formating //Set borders builder.CellFormat.Borders.LineStyle = LineStyle.Single; builder.CellFormat.Borde

    DelForEx for D2007

    Used for formating the source code of delphi 2007, and only for delphi 2007. all of the bugs has been corrected based on the official version. enjoy!

    802.11n_draft_7.0_MAC_AP_Test_Suitev1.7

    - **Test#1.10: Aggregate MPDU (A-MPDU) Frame Formating**:测试聚合多协议数据单元帧的格式化。 - **Test#1.11: Aggregate MSDU (A-MSDU) Frame Formatting**:测试聚合多服务数据单元帧的格式化。 - **Test#1.12...

    matlab中云滴代码-Kaggle-Survey:2020Kaggle数据集分析

    Formating the columns for comfortable access column_dict = {} # Getting the dictionary needed to change the column names for col in res . columns : n_col = col . replace ( '_Part_' , '.' ) n_col = n_...

Global site tag (gtag.js) - Google Analytics