如何通过仅选择月份和周数来获得周开始日期。 [英] How to get week starting date by select only month and week no.
本文介绍了如何通过仅选择月份和周数来获得周开始日期。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
先生,我有2个下拉列表。首先,我有几个月的名字。而在第二个我没有几个星期。喜欢: -
< td >
月:
< asp:DropDownList ID = DdlMonth runat = server >
< asp:ListItem 值 = 1 > Jan < / asp:ListItem >
< asp:ListItem 值 < span class =code-keyword> = 2 > 2月< / asp:ListItem >
< asp:ListItem < span class =code-attribute>值 = 3 > Mar < / asp:ListItem >
< asp:ListItem 值 = 4 > Apr < / asp:ListItem >
< asp:ListItem 值 = 5 > 可以< / asp:ListItem >
< asp: ListItem 值 = 6 > Jun < / asp:ListItem >
< asp:ListItem 值 = 7 > Jul < / asp:ListItem >
< asp :ListItem 值 = 8 > 8月< / asp:ListItem >
< asp:ListItem 值 = 9 > Sep < / asp:ListItem >
< asp:ListItem 值 = 10 > 10月< / asp:ListItem >
< asp:ListItem 值 = 11 > 11月< / asp:ListItem >
< asp:ListItem 值 = < span class =code-keyword> 12 > Dec < / asp:ListItem >
< / asp:DropDownList >
周:
< asp:DropDownList ID = DdlWeek runat = server >
< asp:ListItem 值 = 1 > 1 < / asp:ListItem >
< asp:ListItem 值 = 2 > 2 < / asp:ListItem >
< asp:ListItem 值 = 3 > 3 < / asp:ListItem >
< asp :ListItem 值 = 4 > 4 < / asp:ListItem >
< asp:ListItem 值 = 5 < span class =code-keyword>> 5 < / asp:ListItem >
< / asp:DropDownList >
< span st yle = vertical-align:bottom > < asp:ImageButton ID = ImageLoad runat = server ImageUrl = 〜/ Images / Load.png 宽度 = 30px 高度 = 30px / > < / span >
< / td >
现在我选择First Dropdown第五个月意味着'五月'。在第二次下拉我选择2周。然后我点击Imagebutton(ImageLoad)。现在我需要显示那周的开始日期。它表示5月份第2周的开始日期。如何通过使用月和周的值来获取此日期。
解决方案
您可以使用Calendar控件或DateTime对象并向前调整它来获取所需信息或者按天数,周数,月数等返回。查看MSDN上的文档。
这里: http://stackoverflow.com/questions/662379/calculate-date-from-week-number [ ^ ]是一个想法。您必须根据需要更改代码。
您的问题非常有趣,所以我决定编写一段代码来解决它。
这里是TDateFromWeek
类的定义:
Imports System.Globalization
公开 类 TDateFromWeek
私有 firstDayInMonth 作为 日期
私有 dFrom 作为 日期
私有 dTo 作为 日期
公开 Sub 新( ByVal yr 作为 整数, ByVal mh 作为 整数, ByVal weekno 作为 整数)
Dim wn 作为 整数 = 0
firstDayInMonth = 新 日期(年,mh, 1 )
dFrom = firstDayInMonth
wn = GetWeekOfMonth(dFrom) - GetWeekOfMonth(firstDayInMonth)+ 1
(wn< weekno)
dFrom = dFrom.AddDays( 1 )
wn = GetWeekOfMonth(dFrom) - GetWeekOfMonth(firstDayInMonth)+ 1
循环
dTo = dFrom.AddDays( 5 )
结束 Sub
公开 ReadOnly 属性 DateFrom 作为 日期
获取
< span class =code-keyword>返回 dFrom
结束 获取
结束 属性
公共 ReadOnly 属性 DateTo As 日期
获取
返回 dTo
< span class =code-keyword>结束 获取
结束 属性
私有 功能 GetWeekOfMonth( ByVal currDate 作为 日期) 作为 整数
Dim ci 作为 CultureInfo = CultureInfo.CurrentCulture ' 或CultureInfo.CreateSpecificCulture( 恩-US)
返回 ci.Calendar.GetWeekOfYear(currDate,CalendarWeekRule.FirstDay,DayOfWeek.Monday)
结束 功能
结束 类
用法:
Dim dfw As TDateFromWeek = 新 TDateFromWeek( 2015 , 5 , 2 )
Console.WriteLine( {0} - {1},dfw.DateFrom.ToString( yyyy-MM- dd),dfw.DateTo.ToString( yyyy-MM-dd) )
Console.ReadKey()
结果:
2015-05-04 - 2015-05 -09
Sir, i have 2 dropdownlist. in first i have months name . and in second i have no of weeks. like:-
<td>
Month:
<asp:DropDownList ID="DdlMonth" runat="server">
<asp:ListItem Value="1">Jan</asp:ListItem>
<asp:ListItem Value="2">Feb</asp:ListItem>
<asp:ListItem Value="3">Mar</asp:ListItem>
<asp:ListItem Value="4">Apr</asp:ListItem>
<asp:ListItem Value="5">May</asp:ListItem>
<asp:ListItem Value="6">Jun</asp:ListItem>
<asp:ListItem Value="7">Jul</asp:ListItem>
<asp:ListItem Value="8">Aug</asp:ListItem>
<asp:ListItem Value="9">Sep</asp:ListItem>
<asp:ListItem Value="10">Oct</asp:ListItem>
<asp:ListItem Value="11">Nov</asp:ListItem>
<asp:ListItem Value="12">Dec</asp:ListItem>
</asp:DropDownList>
Week:
<asp:DropDownList ID="DdlWeek" runat="server">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
</asp:DropDownList>
<span style="vertical-align:bottom"> <asp:ImageButton ID="ImageLoad" runat="server" ImageUrl="~/Images/Load.png" Width="30px" Height="30px" /></span>
</td>
Now i Select in First Dropdown the 5th month it means 'MAY' month. and in Second dropdown i have select "2" week. and then i Click on Imagebutton(ImageLoad). Now i need to display starting dates of that week. it means Starting date of 2nd week of MAY month. How can i get this date by using the value of month and week.
解决方案
You can get the required information by using a Calendar control or DateTime object and adjusting it forward or back by number of days, weeks, months etc. Check the documentation on MSDN.
Here: http://stackoverflow.com/questions/662379/calculate-date-from-week-number[^] is an idea. You have to change the code to your needs.
[EDIT]
Your issue is quite interesting, so i decided to write the piece of code to resolve it.
Here is a definition ofTDateFromWeek
class:
Imports System.Globalization Public Class TDateFromWeek Private firstDayInMonth As Date Private dFrom As Date Private dTo As Date Public Sub New(ByVal yr As Integer, ByVal mh As Integer, ByVal weekno As Integer) Dim wn As Integer = 0 firstDayInMonth = New Date(yr, mh, 1) dFrom = firstDayInMonth wn = GetWeekOfMonth(dFrom) - GetWeekOfMonth(firstDayInMonth) + 1 Do While (wn < weekno) dFrom = dFrom.AddDays(1) wn = GetWeekOfMonth(dFrom) - GetWeekOfMonth(firstDayInMonth) + 1 Loop dTo = dFrom.AddDays(5) End Sub Public ReadOnly Property DateFrom As Date Get Return dFrom End Get End Property Public ReadOnly Property DateTo As Date Get Return dTo End Get End Property Private Function GetWeekOfMonth(ByVal currDate As Date) As Integer Dim ci As CultureInfo = CultureInfo.CurrentCulture 'or CultureInfo.CreateSpecificCulture("en-US") Return ci.Calendar.GetWeekOfYear(currDate, CalendarWeekRule.FirstDay, DayOfWeek.Monday) End Function End Class
Usage:
Dim dfw As TDateFromWeek = New TDateFromWeek(2015, 5, 2) Console.WriteLine("{0} - {1}", dfw.DateFrom.ToString("yyyy-MM-dd"), dfw.DateTo.ToString("yyyy-MM-dd")) Console.ReadKey()
Result:
2015-05-04 - 2015-05-09
这篇关于如何通过仅选择月份和周数来获得周开始日期。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文