问题描述
我想找一周的天数。在下面的代码中,变量'daynumber'是
只显示零。在Web表单变量'daynumber'工作。所以我在窗口形式中复制了
整个事物,但'daynumber'变量只显示为零。如何纠正代码?
I want to find the day number of a week. In the following code the variable 'daynumber' is
showing just zero.In web form variable 'daynumber' works. So I copied
the entire thing in window forms but the 'daynumber' variable shows just zero. How to rectify the code?
DateTime v_todaydt = DateTime.Today;
v_DespatchDate = v_todaydt.AddDays(7).Date.ToString("yyyy/MM/dd");
DateTime dateTime = DateTime.ParseExact(v_DespatchDate, "yyyy/MM/dd", System.Globalization.CultureInfo.InvariantCulture);
var daynumber = ((int)dateTime.DayOfWeek);
推荐答案
DayOfWeek枚举范围中常量的值从DayOfWeek.Sunday到DayOfWeek.Saturday。如果转换为整数,其值的范围从零(表示DayOfWeek.Sunday)到六(表示DayOfWeek.Saturday)。
The value of the constants in the DayOfWeek enumeration ranges from DayOfWeek.Sunday to DayOfWeek.Saturday. If cast to an integer, its value ranges from zero (which indicates DayOfWeek.Sunday) to six (which indicates DayOfWeek.Saturday).
阅读 []
如果您需要一周的日期数
Read the Remarks section of the DateTime.DayOfWeek Property[^]
if you need Day number of the week
var daynumber = ((int)dateTime.DayOfWeek) +1;
备注:你的代码可以减少到
Side Note: Your code can reduce to
int dayNum = (int)DateTime.Today.AddDays(7).DayOfWeek + 1;
这篇关于如何找到星期几的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!