我有一个包含位置和日期的数据集.我想计算一年中的周数(00-53),但是使用星期四作为一周的第一天.数据如下所示:
location <- c(a,b,a,b,a,b)
date <- c("04-01-2013","26-01-2013","03-02-2013","09-02-2013","20-02-2013","03-03-2013")
mydf <- data.frame(location, date)
mydf
Run Code Online (Sandbox Code Playgroud)
我知道有一个strftime函数用于计算一年中的一周,但它只能使用星期一或星期日作为一周的第一天.任何帮助将受到高度赞赏.
我正在计算日期的周数,但是在System.Globalization.Calendar2007年和2012年的12月31日(其他年份)中,它将返回奇数结果.
Calendar calendar = CultureInfo.InvariantCulture.Calendar;
var date = new DateTime(2007, 12, 29);
for (int i = 0; i < 5; i++)
{
int w = calendar.GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
Console.WriteLine("{0}\t{1}", date.ToString("dd.MM.yyyy"), w);
date = date.AddDays(1);
}
Run Code Online (Sandbox Code Playgroud)
结果
29.12.2007 52
30.12.2007 52
31.12.2007 53 <--
01.01.2008 1
02.01.2008 1
29.12.2012 52
30.12.2012 52
31.12.2012 53 <--
01.01.2013 1
02.01.2013 1
Run Code Online (Sandbox Code Playgroud)
据我了解,2007年和2012年不应该有53周,但是应该在第1周包括这些日子.有没有办法改变这种行为Calendar?
有人可以向我解释为什么这两个会给出不同的结果吗?
我用PHP执行它.
date("YW",mktime(0, 0, 0, 3, 22 , 2013)); // outputs 201312
Run Code Online (Sandbox Code Playgroud)
当我用MySQL执行它时
SELECT YEARWEEK(now()); // outputs 201311
Run Code Online (Sandbox Code Playgroud) 我有一系列随机日期(不是来自MySQL).我需要将它们按周分组为第1周,第2周,依此类推至第5周.
我有的是这个:
$dates = array('2015-09-01','2015-09-05','2015-09-06','2015-09-15','2015-09-17');
Run Code Online (Sandbox Code Playgroud)
我需要的是通过提供日期来获取月份周数的函数.
我知道我可以通过这样做获得
date('W',strtotime('2015-09-01'));
周数,但本周数是年(1-52)之间的数字,但我只需要一个月的周数,例如2015年9月有5周:
我应该能够通过提供日期来获得第一周的第1周
$weekNumber = getWeekNumber('2015-09-01') //output 1;
$weekNumber = getWeekNumber('2015-09-17') //output 3;
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用wpf制作日历.通过使用itemsPanel和更多,我有一个网格有7列(星期六 - 星期六)和6行(星期#月).如果我可以通过获取工作日和周数(月份)找到每月第一个的起始位置,我如何找到周数(每月0-5)?我也不能以某种方式从那里填写日历?我迷路了,我不知道还能尝试什么.
public partial class SchedulePage : Page
{
MainWindow _parentForm;
public int dayofweek;
public SchedulePage(MainWindow parentForm)
{
InitializeComponent();
_parentForm = parentForm;
// DateTime date = new DateTime(year, month, day);
_parentForm.bindings = new BindingCamper();
_parentForm.bindings.schedule.Add(new Schedule { WeekNo = (int) getWeekNumber(), WeekDay = dayofweek });
DataContext = _parentForm.bindings;
// lblTest.Content = dates(2011, 10, 27);
}
public double getWeekNumber()
{
dayofweek = getWeekDay(2011, 10, 31);
double h = dayofweek / 7;
double g = Math.Floor(h);
return g;
}
public int …Run Code Online (Sandbox Code Playgroud) 我正在使用新的Android 5.0(API21)测试我的应用程序,但是在CalendarView中出现错误
Caused by: java.lang.UnsupportedOperationException: CalendarView does not exists for the new DatePicker at android.widget.DatePickerCalendarDelegate.getCalendarView(DatePickerCalendarDelegate.java:501) at android.widget.DatePicker.getCalendarView(DatePicker.java:365)
令我惊讶的是,错误并不是因为新API21 DatePicker中没有CalendarView,事实上,API21中没有DatePicker,只有CalendarView.
产生错误是因为我在CalendarView中隐藏了Week Numbers
picker.getCalendarView().setShowWeekNumber(false);
Run Code Online (Sandbox Code Playgroud)
这条指令抛出了UnsupportedOperationException,在CalendarView中不再警告周数,它们只是消失了,没有解释.
下面的代码给出了Nougat和pre-Nougat的不同结果.看看,如果你想要自己尝试一下.如果有人能解释我为什么并给出解决方案,我将不胜感激.
我想在所有Android版本上获得正确的WEEK_OF_YEAR值,具体取决于一周的第一天.我有一个时间表应用程序,我正在使用gregorianCalendar很多,所以我不想切换到另一个类/ lib.
//default first day of the week is Monday for replication. I live in the Netherlands, it's weird.
Locale l = new Locale("nl", "NL");
GregorianCalendar test = new GregorianCalendar(l);
test.set(Calendar.YEAR, 2017);
test.set(Calendar.MONTH, 0);
test.set(Calendar.DAY_OF_MONTH, 29);//this is a Sunday
int week = test.get(Calendar.WEEK_OF_YEAR);//should be 4
test.setFirstDayOfWeek(1);//Set it to Sunday
int week2 = test.get(Calendar.WEEK_OF_YEAR);//should be 5 but is 4 below nougat???
Run Code Online (Sandbox Code Playgroud) 在Excel 2007中,我有一个年份编号和周编号,我想计算月份编号.
问题在于,在我的情况下,每周的开始是星期一,因此有些星期将会重叠多年.
例子:
Year: 2012
Week 1 started: Monday 2nd January
Sunday 1st January was in week 52 of 2011
Run Code Online (Sandbox Code Playgroud)
所以给出以下内容:
Year: 2011
Week: 10
Run Code Online (Sandbox Code Playgroud)
我如何计算出第3周开始的第10周,因此第10周是第3个月.
感谢您的帮助.
我正在做一个项目。在那里我应该找到一年的总周数。我尝试使用以下代码,但得到错误答案:2020 年有 53 周,但此代码给出了 52 周。
我在这段代码中哪里出错了?
package com.hib.mapping;
import java.time.LocalDate;
import java.time.temporal.WeekFields;
import java.util.Calendar;
import java.util.GregorianCalendar;
import org.joda.time.DateTime;
public class TestWeek {
public static void main(String args[]) {
System.out.println(getWeeks());
}
public static int getWeeks() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2020);
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DAY_OF_MONTH, 1);
GregorianCalendar gregorianCalendar = new GregorianCalendar();
int weekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (gregorianCalendar.isLeapYear(2020)) {
if (weekDay == Calendar.THURSDAY || weekDay == Calendar.WEDNESDAY)
return 53;
else
return 52;
} else {
if (weekDay == Calendar.THURSDAY) …Run Code Online (Sandbox Code Playgroud) 我想在C#中创建一个函数,一个星期的数字将在那一周返回给我.
例如,对于第40周,我怎样才能得到这些日子:4/10,5/10,6/10,7/10,8/10,9/10,10/10.
先感谢您!