延长预测的星期几

时间:2014-07-23 17:43:22

标签: c# xslt-1.0

这样做的目的是让预测的第一天成为当周的当天,其余的则是。 0显然是星期日,6是星期六。我试图弄清楚这是否会正常工作或是否有更好的方法。下面的c#代码显示了我如何找到它最简单的方法,但显然我的数学超过6,所以它没有显示到最后的日子。非常清楚我只需要从当天开始按顺序排列星期几,这样我就可以申请xsl代码了,我也展示了一个例子。 xsl中的每个位置都被视为一天。如果你可以帮我解决数学问题,我认为这会有用。

`               DayOfWeek day1 = DateTime.Now.DayOfWeek;
                    DayOfWeek day2 = day1 + 1;
                    DayOfWeek day3 = day2 + 1;
                    DayOfWeek day4 = day3 + 1;
                    DayOfWeek day5 = day4 + 1;
                    DayOfWeek day6 = day5 + 1;
                    DayOfWeek day7 = day6 + 1;
                    switch (day1)
                    {
                        default:
                        case DayOfWeek.Sunday:                      
                            break;
                        case DayOfWeek.Monday:
                            break;
                        case DayOfWeek.Tuesday:
                            break;
                        case DayOfWeek.Wednesday:
                            break;
                        case DayOfWeek.Thursday:
                            break;
                        case DayOfWeek.Friday:
                            break;
                        case DayOfWeek.Saturday:
                            break;

                    }
                     xFore.Root.Add(new XElement("day", day1),
                         new XElement("day2", day2),
                         new XElement("day3", day3),
                         new XElement("day4", day4),
                         new XElement("day5", day5),
                         new XElement("day6", day6),
                         new XElement("day7", day7));`



`                     <xsl:if test ="position()=1">Today</xsl:if>
                      <xsl:if test ="position()=2">
                        <xsl:value-of select="//day2"/>
                      </xsl:if>
                      <xsl:if test ="position()=3">
                        <xsl:value-of select="//day3"/>
                      </xsl:if>
                      <xsl:if test ="position()=4">
                      <xsl:value-of select="//day4"/>
                      </xsl:if>
                      <xsl:if test ="position()=5">
                        <xsl:value-of select="//day5"/>
                      </xsl:if>
                      <xsl:if test ="position()=6">
                        <xsl:value-of select="//day6"/>
                      </xsl:if>
                      <xsl:if test ="position()=7">
                        <xsl:value-of select="//day7"/>
                      </xsl:if>`

2 个答案:

答案 0 :(得分:0)

您需要使用模数运算符(http://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx)来防止超过6:

DayOfWeek day1 = DateTime.Now.DayOfWeek;
DayOfWeek day2 = (DayOfWeek)((byte)(day1 + 1) % 7);
DayOfWeek day3 = (DayOfWeek)((byte)(day2 + 1) % 7);
DayOfWeek day4 = (DayOfWeek)((byte)(day3 + 1) % 7);
DayOfWeek day5 = (DayOfWeek)((byte)(day4 + 1) % 7);
DayOfWeek day6 = (DayOfWeek)((byte)(day5 + 1) % 7);
DayOfWeek day7 = (DayOfWeek)((byte)(day6 + 1) % 7);

// Rest of code omitted

答案 1 :(得分:0)

我尝试解决您的问题。如果我是正确的你只需要实现一个mod操作,以便没有一个数字高于6?

示例:

DayOfWeek[] Days = new DayOfWeek[7];

DayOfWeek[0] = DateTime.Now.DayOfWeek;
For(Int i=1;i < 7;i++)
  DayOfWeek[i] = (DayOfWeek)(((byte)DayOfWeek[i-1]+1) % 7);

XElement[] XmlDays = new XElement[7];
For(Int i=0;i < 7;i++)
  XmlDays[i] = new XElement("day" + i.ToString(), DayOfWeek[i]);

xFore.Root.Add(XmlDays);