Time in W3C format

How to make time in W3C format, including hours, minutes, seconds and time zone offset.

Example:

1997-07-16T19:20:30+01:00 

Update 1

I have the following declaration in config / locales / my_locale.yml:

 time: formats: w3c: %Y-%m-%dT%H:%M 

The only missing part is shifted, for example, "+01: 00". What is this interpolation symbol?

I found this link that contains the time zone name interpolated with% Z.

But where is the bias ?

+4
source share
3 answers

To insert the offset time zone, use the% z (lowercase) key.

0
source

In fact, you should use%: z to include the + character. You also need to turn on the seconds.

 "%Y-%m-%dT%H:%M:%S%:z" 

Update: note that this is only possible with Ruby 1.9.3+, the%: z format marker is not available in earlier versions!

+5
source

You already have an answer, but I just wanted to say that, since it does not depend on the user locale, but on the standard defined by the organization, it might be better to leave it outside the local file, for example en.yml

I personally add

 Time::DATE_FORMATS[:w3c] = "%Y-%m-%dT%H:%M:%S%:z" Date::DATE_FORMATS[:w3c] = "%Y-%m-%d" 

bottom application.rb

0
source

Source: https://habr.com/ru/post/1346965/


All Articles