使用Date.commercial将Mysql周转换为日期

使用Date.commercial将Mysql周转换为日期,mysql,ruby-on-rails,Mysql,Ruby On Rails,从数据库中获取对象后 Object.select('week(created_at) as week, year(created_at) as year') 它返回从0到53的星期,然后从中创建日期对象 Date.commercial(x.year,x.week,1) It报告“无效日期”错误,原因是0和53个星期 我也试过这个 Date.strptime("#{x.year}-#{x.week+1}-1","%Y-%W-%w") 但它也因x周+1(例如53+1)而崩溃。搜索一行解决方案我

从数据库中获取对象后

Object.select('week(created_at) as week, year(created_at) as year')
它返回从0到53的星期,然后从中创建日期对象

Date.commercial(x.year,x.week,1)
It报告“无效日期”错误,原因是0和53个星期

我也试过这个

Date.strptime("#{x.year}-#{x.week+1}-1","%Y-%W-%w")

但它也因x周+1(例如53+1)而崩溃。搜索一行解决方案

我认为您的用法是错误的

我在下面的()中找到了这个

当然,请发送您的ruby版本以及您的x.year和x.week输出

commercial(y=-4712, w=1, d=1, sg=ITALY) click to toggle source Create a new Date object for the Commercial Date specified by year y, week-of-year w, and day-of-week d. Monday is day-of-week 1; Sunday is day-of-week 7. w and d can be negative, in which case they count backwards from the end of the year and the end of the week respectively. No wraparound is performed, however, and invalid values cause an ArgumentError to be raised. y defaults to -4712, w to 1, and d to 1; this is Julian Day Number day 0. sg specifies the Day of Calendar Reform.emphasized text 商业(y=-4712,w=1,d=1,sg=ITALY)点击切换源 为y年、w年的周和d周的天指定的商业日期创建新的日期对象。 星期一是第一周的第一天;星期天是第七周的一天。 w和d可以是负数,在这种情况下,它们分别从年底和周末开始倒数。但是,不会执行概括,并且无效值会导致引发ArgumentError。 y默认为-4712,w默认为1,d默认为1;这是朱利安日第0天。
sg指定日历改革的日期。强调文本不要在
strtime
中的
x.week
中添加1。您应该以ISO 8601格式从DB获取日期。 此格式返回从1到52的周数,这符合
#commercial
期望的输入

在Ruby中:

ISO 8601 week-based year and week number:
The week 1 of YYYY starts with a Monday and includes YYYY-01-04.
The days in the year before the first week are in the last week of
the previous year.
  %G - The week-based year
  %g - The last 2 digits of the week-based year (00..99)
  %V - Week number of the week-based year (01..53)

在MySQL中:

%V  Week (01..53), where Sunday is the first day of the week; WEEK() mode 2; used with %X
%v  Week (01..53), where Monday is the first day of the week; WEEK() mode 3; used with %x
%X  Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%x  Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v


SELECT DATE_FORMAT('1999-01-01', '%X %V');
+------------------------------------+
| DATE_FORMAT('1999-01-01', '%X %V') |
+------------------------------------+
| 1998 52                            |
+------------------------------------+