如何获取当前星期几的日期 [英] How to get the date of a selected day for the current week

查看:129
本文介绍了如何获取当前星期几的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!
我有一个星期几的下拉列表.现在,我有了某种如何编写应用程序的伪代码.

我想要的是在选择组合框中的日期时,我应该获得当前星期几的日期.

像这样的东西

Hi!
I have a dropdownlist with days of the week. Now I have some kind of a pseudo code of how to write the application.

What I want is when the day in my combobox is selected, I should get the date of that day for the current week.

something like this;

currentday==DateTime.Now;
date=""

if(Dropdwonlist="tuesday")
{
 currentday+1
}
if(Dropdwonlist="wednesday")
{
 currentday+2
}



我要在这里得到的是本周的星期二/星期三.
值"1和2"代表日期.



What I want here is to get this week''s Tuesday/Wednesday date.
The values "1 and 2" represent the day.

Thanks for your time!

推荐答案

首先,您必须找出当前一周的开始日期.请参考我在CP上发布的以前的解决方案,以作为如何执行此操作的参考:
First you''ll have to find out the start date of the current week. Please take a former solution I posted here on CP as a reference on how to do that: I want to get all the list of week[^].

It''s a piece of cake from there. All you have to do then as add the appropriate number of days to that start date according to your combo list box. You should remember though that different cultures have differen starting days for a week. Please also refer to my former solution for that.

Cheers!

—MRB


这里是基于Week类的解决方案/TimePeriod.aspx">.NET的时间段库 [ ^ ]:
Here a solution based on the Week class of the Time Period Library for .NET[^]:
// ----------------------------------------------------------------------
public DateTime GetDayOfCurrentWeek( DayOfWeek dayOfWeek )
{
  Week week = new Week();
  foreach ( Day day in week.GetDays() )
  {
    if ( day.DayOfWeek == dayOfWeek )
    {
      return day.Start.Date;
    }
  }
  throw new InvalidOperationException();
} // GetDayOfCurrentWeek

请注意,Week 类支持CalendarWeekRule [

Please note that the Week class supports the CalendarWeekRule[^] of the current culture/calendar.


这篇关于如何获取当前星期几的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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