简体   繁体   English

如何使用客户端时区在客户端中输出数据库时间戳作为日期戳?

[英]How to output a db timestamp as a date stamp in the client using the clients timezone?

Given a timestamp in the DB like so: 2012-06-06T00:27:33Z 给定数据库中的时间戳,如下所示:2012-06-06T00:27:33Z

What's the best way to render this in the browser for the user like so: 像这样在用户的浏览器中呈现此内容的最佳方法是:

5/02/12 3:48 PM

Is it best to use a JS library to use the client to render in the client's localtime? 最好使用JS库来使用客户端在客户端的本地时间进行渲染吗? What's the best practice here? 这里的最佳做法是什么? Thanks 谢谢

I've used the Moment JS library for general date parsing and formatting. 我已经使用Moment JS库进行常规的日期解析和格式化。 Its pretty sweet. 非常漂亮

http://momentjs.com/ http://momentjs.com/

Your example would be basically: 您的示例基本上是:

var date = moment('2012-06-06T00:27:33Z');
console.log('Formatted is %s', date.format('M/DD/YY h:m A'));
>> Formatted is 6/05/12 5:27 PM

By default Moment parses in local time, doing the timezone conversion automatically. 默认情况下,Moment会以本地时间进行解析,并自动进行时区转换。

If you are using Rails, in environment.rb add something like 如果您使用的是Rails,请在environment.rb中添加如下内容

Date::DATE_FORMATS[:browser] = "%d/%b/%Y %H:%m"

Take a look at this to format according to your tastes. 看看这个可以根据您的口味进行格式化。 http://www.onrails.org/2008/08/20/what-are-all-the-rails-date-formats http://www.onrails.org/2008/08/20/what-are-all-the-rails-date-formats

In the view you would put 您认为

<%= Model.date_field.to_s(:browser) %>

If you need to adjust for timezones, you will first need to query your user's timezone. 如果需要调整时区,则首先需要查询用户的时区。 There are several ways you could do this. 您可以通过几种方法来执行此操作。

Then in the view you could use eg 然后在视图中,您可以使用例如

<%= Model.date_field.change(:offset => "+0300").to_s(:browser) %>

or 要么

<%= Model.date_field.in_time_zone(-3).to_s(:browser) %>

or 要么

<%= Model.date_field.utc.to_s(:browser) %>

or 要么

<%= (Model.date_field.utc + Time.zone_offset('EST')).to_s(:browser) %>

There are any options! 有什么选择!

Hope this is helpful! 希望这会有所帮助!

try: 尝试:

<%= article.created_at.local_time %>

you should use locales. 您应该使用语言环境。

locales/de.yml 语言环境/de.yml

de:
  date:
    formats:
      default: "%d.%m.%Y"
      short: "%e. %b"
      long: "%A, %d.%B %Y um %H:%M"
      only_day: "%e"
    day_names: [Sonntag, Montag, Dienstag, Mittwoch, Donnerstag, Freitag, Samstag]
    abbr_day_names: [So, Mo, Di, Mi, Do, Fr, Sa]
    month_names: [~, Januar, Februar, März, April, Mai, Juni, Juli, August, September, Oktober, November, Dezember]
    abbr_month_names: [~, Jan, Feb, Mär, Apr, Mai, Jun, Jul, Aug, Sep, Okt, Nov, Dez]
    order: [ :day, :month, :year ]
  time:
    formats:
      default: "%A, %d.%B %Y um %H:%M"
      short: "%d.%m.%Y"
      long: "%d.%m.%Y, %H:%M:%S"

in views: 在视图中:

 <%= I18n.l current_article.created_at, :format => :long %>

and for the timezone conversion: 对于时区转换:

config/environments/development.rb config / environments / development.rb

config.time_zone = 'Berlin'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM