简体   繁体   English

Utilities.formatDate 在第 7 天而不是第 1 天开始一​​周

[英]Utilities.formatDate starts the week on day 7 instead of day 1

this is NOT a duplicate 这不是重复的
According to its documentation, Utilities.formatDate(date, timeZone, format) works according to Java SE SimpleDateFormat class specifications .根据其文档, Utilities.formatDate(date, timeZone, format)根据Java SE SimpleDateFormat 类规范工作
Which, as you can easily tell, expect the week to start on Monday, that is Day 1.正如您可以轻松判断的那样,预计本周将从星期一开始,即第 1 天。
Why does my week starts on sunday instead, even if it is clearly output as day 7?为什么我的一周从星期日开始,即使它明确输出为第 7 天?

截屏

function testDate() {

  var ss         = SpreadsheetApp.getActiveSpreadsheet();
  var sheet      = ss.getSheetByName('history');
  var extension  = 2;
  var arrayDates = sheet.getRange(3, 2,  extension).getValues();

//flattening the array
function flatten(tdArray){return [].concat.apply([], tdArray);}  
  var arrayDates = flatten(arrayDates);

//"extracting" weeks' numbers from the dates
  var arrayWeekNums  = arrayDates.map(function(el){return Utilities.formatDate(el, "CET", "uyww");});
// CET = Central Europe Time
// u   = day of the week;
// y   = year in 4 digits;
// ww  = week numbers in 2 digits

Logger.log(arrayWeekNums); 
Logger.log(arrayDates);  
}


//Logger.log(arrayWeekNums);
//  [7201803, 6201802]
//  7-2018-03  week day: 7  ;  year: 2018  ;  week number: 03;
//  6-2018-02  week day: 6  ;  year: 2018  ;  week number: 02; ??

//Logger.log(arrayDates); 
//[20-01-11 22:21:54:187 CET] [Sun Jan 14 00:00:01 GMT+01:00 2018, Sat Jan 13 23:59:59 GMT+01:00 2018]

I've also switched my project's global settings from US to any european Contry: nothing changes.我还将我的项目的全局设置从美国切换到任何欧洲国家:没有任何变化。
How can I change this?我怎样才能改变这个?

edit, as per request:编辑,根据要求:
1."impostazioni internazionali" aka spreadsheet timezone(File>spreadsheet settings>locale) at the moment "Italia", but even with "United States" it's the same outcome; 1.“Impostazioni internazionali”又名电子表格时区(文件>电子表格设置>区域设置)目前为“意大利”,但即使使用“美国”也是相同的结果;

2.script timezone(File> project settings): "GMT +01:00 - Paris" 2.脚本时区(文件>项目设置):“GMT +01:00 - 巴黎”

spreadsheet copy: https://docs.google.com/spreadsheets/d/1f8ep3Hczo1jbu7s2PTGsaqog8qkW-dc9MAeYNOEBRbw/edit?usp=sharing电子表格副本: https : //docs.google.com/spreadsheets/d/1f8ep3Hczo1jbu7s2PTGsaqog8qkW-dc9MAeYNOEBRbw/edit?usp=sharing

I don't think this is an issue with the locale or time-zone settings.我不认为这是语言环境或时区设置的问题。

I ran the script for different points of time and this is what I found.我在不同的时间点运行脚本,这就是我发现的。

It seems that the day-of-the-week (dotw) and the week-of-the-year (woty) numbers are not connected.似乎星期几 (dotw) 和星期几 (woty) 数字没有联系。

In any year, week-1 does not start on the 1st Monday of that year.在任何一年中,第 1 周都不会从该年的第一个星期一开始。

Dotw is strictly Mon = 1, Tue = 2, etc. Dotw 是严格的 Mon = 1,Tue = 2,等等。

Woty is tricky. Woty 很棘手。

It appears that a year will have 53 weeks as long as the 53rd week can be completed by 31st Dec.只要第 53 周可以在 12 月 31 日之前完成,一年似乎就有 53 周。

Else the year will have 52 weeks.否则一年将有 52 周。 And the "week 1" of the subsequent year will start in December of the previous year.下一年的“第 1 周”将从上一年的 12 月开始。

For example, 2016 ended on the 53rd week.例如,2016 年在第 53 周结束。 And 2017 started on "week 1": 2017 年从“第 1 周”开始:

Date           Doty*  Dotw Woty    
23-Dec-2016    358    5    52
24-Dec-2016    359    6    52
25-Dec-2016    360    7    53
26-Dec-2016    361    1    53
27-Dec-2016    362    2    53
28-Dec-2016    363    3    53
29-Dec-2016    364    4    53
30-Dec-2016    365    5    53
31-Dec-2016    366    6    53
01-Jan-2017    1      7    1
02-Jan-2017    2      1    1
03-Jan-2017    3      2    1
                                     *Day of the year

But in 2017, as the 53rd week could not be accommodated before year-end, 31-Dec-17 was in "week 1" of 2018:但在 2017 年,由于第 53 周在年底前无法容纳,31-Dec-17 是 2018 年的“第 1 周”:

Date          Doty    Dotw Woty    
29-Dec-2017    363    5    52
30-Dec-2017    364    6    52
31-Dec-2017    365    7    1
01-Jan-2018    1      1    1
02-Jan-2018    2      2    1
03-Jan-2018    3      3    1

Similarly for 2019-20, the 52nd week ended on 28-Dec-19.同样,对于 2019-20 年,第 52 周于 19 年 12 月 28 日结束。 And "week 1" of 2020, started on 29-Dec-19: 2020 年的“第 1 周”于 19 年 12 月 29 日开始:

Date           Doty   Dotw Woty    
27-Dec-2019    361    5    52
28-Dec-2019    362    6    52
29-Dec-2019    363    7    1
30-Dec-2019    364    1    1
31-Dec-2019    365    2    1
01-Jan-2020    1      3    1
02-Jan-2020    2      4    1
03-Jan-2020    3      5    1
04-Jan-2020    4      6    1

Edit编辑

I tried several time periods including:我尝试了几个时间段,包括:

new Date(0) => Thu Jan 01, 1970

and

new Date(0, 0, 0) => Sat Dec 30, 1899 (?!)

As far as I can tell, woty changes on a Sunday.据我所知,星期天会发生变化。 But dotw resets to 1 on a Monday.但是 dotw 在星期一重置为 1。 And as multiples of 7, they keep to that cycle.并且作为 7 的倍数,它们保持该循环。

So the best explanation seems to be that the two are not connected except for their 7-day cycle offset by a day .所以最好的解释似乎是两者没有联系,除了它们的 7 天周期偏移了一天

And that the start of a new week那是新一周的开始

  • in the context of weekdays cycles through Monday to Sunday with Monday = 1weekdays从星期一到星期日循环的情况下,星期一 = 1
  • in the context of week number in a year ( 1 through 52 or 53), cycles through Sunday to Monday with a new week number starting on Sunday.week number in a year中的week number in a year (1 到 52 或 53)的上下文中,从星期日到星期一循环,新的周数从星期日开始。

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

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