Parsing a week string during (YYYY-W #)

I am trying to parse a weekly string that HTML5 input with the type of "week" may return to the actual date or time in ruby.

Input will return a string like this: "2014-W05", where W05 is the fifth week of the year.

Here is what I tried:

2.1.0dev :057 > Time.strptime("2014-W05", "%Y-W%V")
2014-01-01 00:00:00 -0800
2.1.0dev :058 > Date.strptime("2014-W05", "%Y-W%V")
2014-01-01
2.1.0dev :058 > DateTime.strptime("2014-W05", "%Y-W%V")
Wed, 01 Jan 2014 00:00:00 +0000

According to the documentation: http://apidock.com/ruby/DateTime/strftime

% V - week week number (01..53)

Thank you for your help!

0
source share
1 answer

Ruby Date (http://www.ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/DateTime.html) " ", %G. :

Date.strptime("2014-W05", "%G-W%V")

, :

2.1.0 :004 > DateTime.strptime("2014-W05", "%G-W%V")
 => #<DateTime: 2014-01-27T00:00:00+00:00 ((2456685j,0s,0n),+0s,2299161j)>

Versus:

2.1.0 :003 > DateTime.strptime("2014-W05", "%Y-W%V")
 => #<DateTime: 2014-01-01T00:00:00+00:00 ((2456659j,0s,0n),+0s,2299161j)>
+1

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


All Articles