查找当前月份的第一个和最后一个星期六的代码 [英] code for find first and last saturday of current months

查看:73
本文介绍了查找当前月份的第一个和最后一个星期六的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是如何找到当前月份的第一个和最后一个星期六的.pls帮助我

解决方案

示例代码:

  void  Main()
{
var dates = GetDates(DateTime .Now.Year,DateTime.Now.Month);
var fistSat = dates.FirstOrDefault(d => d.DayOfWeek == DayOfWeek.Saturday);
var lastSat = dates.LastOrDefault(d => d.DayOfWeek == DayOfWeek.Saturday);
}

public static 列表< DateTime> GetDates( int year, int month)
{
return Enumerable.Range( 1 ,DateTime.DaysInMonth(year,month))
.Select(day = > new DateTime(年,月,日))
.ToList();
}


Linq方式实用且简单,如果有人需要非linq方法,这是经典的方法;



  public   static 元组< DateTime ,日期时间> GetFirstAndLastSaturday()
{
var curDate = DateTime.Now;
var firstSaturday = new DateTime(curDate.Year,curDate.Month, 1 );
while (firstSaturday.DayOfWeek!= DayOfWeek.Saturday)
{
firstSaturday = firstSaturday.AddDays( 1 );
}
var lastSaturday = firstSaturday;
while (lastSaturday.AddDays( 7 )。月== curDate.Month)
{
lastSaturday = lastSaturday.AddDays( 7 );
}

return new 元组< DateTime,DateTime>( firstSaturday,lastSaturday);
}

var result = GetFirstAndLastSaturday();


< blockquote>如果让DateTime类完成工作,这是最简单的。



使用当月的第一天计算到下周六。使用下个月的第一天计算到上周六。



 private void onDtPickerValueChanged(object sender,EventArgs e)
{
DateTimePicker dtPicker =(DateTimePicker)sender;
int firstSaturday = 0;
int lastSaturday = 0;

getFirstLastSatuday(dtPicker.Value,out firstSaturday,out lastSaturday);
System.Diagnostics.Debug.WriteLine(First Saturday = {0},Last Saturday = {1},
firstSaturday,lastSaturday);
}

private static void getFirstLastSatuday(DateTime date,out int firstDay,out int lastDay)
{
DateTime dateTime = new DateTime(date.Year,date。月,1);
int dayofWeek =(int)dateTime.DayOfWeek; //星期日= 0,星期六= 6
firstDay = 1 +(6 - dayofWeek);

dateTime = dateTime.AddMonths(1);
dayofWeek =(int)dateTime.DayOfWeek;
lastDay = dateTime.AddDays((dayofWeek + 1)* -1).Day;
}


how I found first and last saturday of current months .pls help me

解决方案

sample code :

void Main()
{
    var dates = GetDates(DateTime.Now.Year, DateTime.Now.Month);
    var fistSat = dates.FirstOrDefault(d=>d.DayOfWeek==DayOfWeek.Saturday );
    var lastSat = dates.LastOrDefault(d=>d.DayOfWeek==DayOfWeek.Saturday );
}

public static List<DateTime> GetDates(int year, int month)
{
   return Enumerable.Range(1, DateTime.DaysInMonth(year, month))
                    .Select(day => new DateTime(year, month, day))
                    .ToList();
}


Linq way is practical and easy, this is the classical method if anyone needs a non linq method;

public static Tuple<DateTime,DateTime> GetFirstAndLastSaturday()
{
    var curDate = DateTime.Now;
    var firstSaturday = new DateTime(curDate.Year,curDate.Month,1);
    while (firstSaturday.DayOfWeek != DayOfWeek.Saturday)
    {
       firstSaturday = firstSaturday.AddDays(1);
    }
    var lastSaturday = firstSaturday;
    while (lastSaturday.AddDays(7).Month == curDate.Month)
    {
        lastSaturday = lastSaturday.AddDays(7);
    }

    return new Tuple<DateTime, DateTime>(firstSaturday,lastSaturday);
}

var result = GetFirstAndLastSaturday();


It's easiest if you let the DateTime class do the work.

Use the 1st day of the month and calculate to next Saturday. Use the 1st day of the next month and calculate to previous Saturday.

private void onDtPickerValueChanged(object sender, EventArgs e)
{
	DateTimePicker dtPicker = (DateTimePicker)sender;
	int firstSaturday = 0;
	int lastSaturday = 0;

	getFirstLastSatuday(dtPicker.Value, out firstSaturday, out lastSaturday);
	System.Diagnostics.Debug.WriteLine("First Saturday = {0}, Last Saturday = {1}",
		firstSaturday, lastSaturday);
}

private static void getFirstLastSatuday(DateTime date, out int firstDay, out int lastDay)
{
	DateTime dateTime = new DateTime(date.Year, date.Month, 1);
	int dayofWeek = (int)dateTime.DayOfWeek; // Sunday = 0, Saturday = 6
	firstDay = 1 + (6 - dayofWeek);

	dateTime = dateTime.AddMonths(1);
	dayofWeek = (int)dateTime.DayOfWeek;
	lastDay = dateTime.AddDays((dayofWeek + 1) * -1).Day;
}


这篇关于查找当前月份的第一个和最后一个星期六的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆