In Rails, for your model classes, if you add the fields created_on and updated_on (or created_at and updated_at), Rails automatically maintains the proper values for them. (On the database side, you need to create them as DATETIME fields.)
How cool is that! No longer do you need to manually track when users register, when moderators edit comments, etc.–Rails maintains all this information FOR you!
The only catch is printing them–by default, Rails prints them in a fairly ugly pattern, like this:
Sun Nov 04 12:56:00 -0500 2007
Note that it prints the day, the second, and even your time-zone difference! While useful, you probably don’t need all this information.
How, then, can you print them out nicely?
The asnwer is, like most programming languages, Ruby provides a method (called strftime) which formats dates, given a string with a particular syntax. For example, you could write:
self.created_on.strftime("%B %d, %Y")
* %a - The abbreviated weekday name (eg. Sun)
* %A - The full weekday name (eg. Sunday)
* %b - The abbreviated month name (eg. Nov)
* %B - The full month name (eg. January)
* %d - Two-digit day of the month (eg. 07)
* %H - Two-digit hour of the day, 24-hour clock (from 00 to 23)
* %I - Two-digit hour of the day, 12-hour clock (from 01 to 12)
* %j - Three-digit day of the year (from 001 to 366)
* %m - Two-digit month of the year (from 01 to 12)
* %M - Two-digit minute of the hour (from 00 to 59)
* %p - Meridian indicator (AM or PM)
* %S - Two-digit second of the minute (from 00 to 60)
* %U - Two-digit week number of the current year, starting with the first Sunday as the first day of the first week (from 00 to 53)
* %W - Two-digit week of the current year, starting with the first Monday as the first day of the first week (from 00 to 52)
* %w - Day of the week (Sunday is 0; from 0 to 6 inclusively)
* %y - Year without a century (from 00 to 99; eg. 00 for 2000, 47 for 2047)
* %Y - Year with century (eg. 2007)
* %Z - Time zone name
* %% - Literal percent character
分享到:
相关推荐
异议时间戳记自动修改模型上的created_at和updated_at列。设置基本的基本设置假定您的表中有created_at和updated_at列。 let Model = require ( 'objection' ) . Model ;let timestampPlugin = require ( 'objection...
private LocalDateTime createdOn; @UpdatedOnFuncation private LocalDateTime updatedOn; } ``` 现在,我们需要定义一个拦截器来自动填充 created_on 和 updated_on 字段。我们将创建一个名为 `...
固定装置我生成用于测试的夹具和夹具文件如果您想从开发数据库为 Ruby on Rails 应用程序生成装置,这里是一个帮助器。... create_all_fixtures 排除 created_at 和 updated_at 列 fixme = FixtureMe ::
updated_at DATETIME ON UPDATE CURRENT_TIMESTAMP ); ``` 这里,`created_at`字段默认设置为当前时间,而`updated_at`字段在每次记录更新时自动更新。 2. **插入数据**:当我们添加新的记录时,可以使用`INSERT ...
在这个例子中,`created_at`字段会在新记录插入时自动填充当前时间,而`updated_at`字段不仅在插入时会自动填充,而且每次记录被更新时也会更新为当前时间。 此外,需要注意的是,从MySQL 5.6.5版本开始,可以设置...
Crie o Banco de Dados CREATE DATABASE表示字符集utf8mb4 COLLATE utf8mb4_general_ci;...TIMESTAMP, updated_at时间戳NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,PRIMARY KEY( id ))ENGI
Crie o Banco de Dados CREATE DATABASE表示字符集utf8mb4 COLLATE utf8mb4_general_ci;...TIMESTAMP, updated_at时间戳NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,PRIMARY KEY( id ))ENGI
ActiveRecord ::事件 ActiveRecord扩展提供了方便的时间戳管理方法。截屏 (由)安装将以下行添加到应用程序的Gemfile... 这种方法的一个很好的例子是ActiveRecord如何处理created_at和updated_at字段。 这个gem允许您
保存(并返回)的对象将包括所有这些字段(空白为可选,无输入),还包括created_on(日期/时间),updated_on(日期/时间),open(布尔值,打开为true,关闭为false),和_id。 我可以使用/ id / issues / {...
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ); ``` 这个表包含了评论的ID(主键)、评论内容、用户ID、关联的帖子ID以及创建和更新的时间戳。 2. 获取所有学校列表 ```sql ...
Django简化 介绍 Django simple提供了类似Ruby on Rails的命令行功能,模型和帮助器功能,因此您可以专注于...TimeBasedModel(允许您访问created_at和updated_at字段) NamedTimeBasedModel-使您可以访问字段name ,
NO_WIN32_LEAN_AND_MEAN The define *must* be made in the project options. It is not sufficient to #define it in the source. If you manage to compile with C++ Builder (any version), I would very ...
以及两个“时间戳”列: created_at和updated_at 。这将总共提供六列。 costume_stores表将包含七列: 姓名 地点 服装数量或“服装库存” 在职员工人数 是否仍在营业 营业时间 关闭时间 haunted_houses表将包含八列...
as variable elimination and Markov Chain Monte Carlo, and we have created a new chapter on uncertain temporal reasoning, covering hidden Markov models, Kalman filters, and dynamic Bayesian networks. ...
tb_order_shipping订单物流表(order_id,receiver_name,receiver_phone,receiver_mobile,receiver_state,receiver_city,receiver_district,receiver_address,receiver_zip,created,updated) tb_content_category...
通过这个扩展,我们可以自动维护模型的创建者(`created_by`)和更新者(`updated_by`)字段,从而简化了数据审计的流程。 首先,让我们详细了解一下如何在Laravel项目中集成`laravel-attributions`。安装该扩展包...
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ); ``` 接下来,测试数据的导入是为了验证表的正确性和功能。在MySQL中,我们可以使用`INSERT INTO`语句来添加记录。例如,如果我们...
简易模型MySQL简单模型,简单查询。 轻松输入!安装npm i mysql-easy-type mysql2例子CREATE DATABASE ` test `;... ` updated_at ` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP , PRI
* Covers over 25 new topics, as well as most updated information on topics presented in first edition Includes over 30 new world wide contributors, who are experts in this field New case studies ...
Laravel Go Laravel的Golang开源框架,包含了GORM,Echo等,当前配置里的APP_POLICE... `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=Inno