Extract Week Number from Date - InfoPath Dev
in

InfoPath Dev

Extract Week Number from Date

Last post 10-23-2006 11:35 AM by kurtd. 19 replies.
Page 2 of 2 (20 items) < Previous 1 2
Sort Posts: Previous Next
  • 10-16-2006 05:35 AM In reply to

    • kurtd
    • Not Ranked
    • Joined on 10-09-2006
    • USA
    • Posts 13

    Re: Extract Week Number from Date

    Hi, I tried that but I get a syntax error on this line.

    Set objDate = eventObj.Source.selectSingleNode("@dteDate")

    "A text node child of an attribute may not be passed as the context node for an XSLT transform or an XPath query."
  • 10-18-2006 07:30 AM In reply to

    Re: Extract Week Number from Date

    BTW - If you want to have the correct Calendar Week for a specific date instead of only the week number within the month you can use this. (Parts of the code I found in the net)

    public int getCalendarWeek (DateTime date)
    {
    const int JAN = 1;
    const int DEC = 12;
    const int LASTDAYOFDEC = 31;
    const int FIRSTDAYOFJAN = 1;
    const int THURSDAY = 4;
    bool ThursdayFlag = false;

    int DayOfYear = date.DayOfYear;

    int StartWeekDayOfYear =
    (int)(new DateTime(date.Year, JAN, FIRSTDAYOFJAN)).DayOfWeek;
    int EndWeekDayOfYear =
    (int)(new DateTime(date.Year, DEC, LASTDAYOFDEC)).DayOfWeek;

    if( StartWeekDayOfYear == 0)
    StartWeekDayOfYear = 7;
    if( EndWeekDayOfYear == 0)
    EndWeekDayOfYear = 7;

    int DaysInFirstWeek = 8 - (StartWeekDayOfYear );
    int DaysInLastWeek = 8 - (EndWeekDayOfYear );

    if (StartWeekDayOfYear == THURSDAY || EndWeekDayOfYear == THURSDAY)
    ThursdayFlag = true;

    int FullWeeks = (int) Math.Ceiling((DayOfYear - (DaysInFirstWeek))/7.0);

    int WeekNumber = FullWeeks;

    if (DaysInFirstWeek >= THURSDAY)
    WeekNumber = WeekNumber +1;

    if (WeekNumber > 52 && !ThursdayFlag)
    WeekNumber = 1;


    if (WeekNumber == 0)
    WeekNumber = getCalendarWeek(
    new DateTime(date.Year-1, DEC, LASTDAYOFDEC));
    return WeekNumber;
    }

    // splits the string coming form the date picker into three substrings (year, month, day) and converts these
    // into integer values so it can be passed to the dateTime object
    public DateTime ConvertString(string date)
    {
    string strYear = date.Substring(0,4);
    string strMonth = date.Substring(5,2);
    string strDay = date.Substring(8,2);
    int year = Convert.ToInt32(strYear);
    int month = Convert.ToInt32(strMonth);
    int day = Convert.ToInt32(strDay);
    DateTime dateTime = new DateTime(year, month, day);
    return dateTime;
    }
    Listen to Motörhead cos they play Rock'n Roll :)
  • 10-18-2006 11:06 AM In reply to

    • kurtd
    • Not Ranked
    • Joined on 10-09-2006
    • USA
    • Posts 13

    Re: Extract Week Number from Date

    Wow that is a lot more code then I had. I was so close to getting it working with this code:

    If eventObj.Operation = "Insert" Then
    Dim objDate
    Dim objWeek

    Set objDate = XDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:tblTasks/@dteDate")
    Set objWeek = XDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:tblTasks/@txtWeek")

    objWeek.text = DatePart("ww", objDate.text)
    End If


    But the code above only works for the first row in my repeating table. I tried using this line

    Set objDate = eventObj.Source.selectSingleNode("@dteDate")
    Set objWeek = eventObj.Source.selectSingleNode("@txtWeek")

    instead of this line

    Set objDate = XDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:tblTasks/@dteDate")
    Set objWeek = XDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:tblTasks/@txtWeek")

    but them I get this error:
    "A text node child of an attribute may not be passed as the context node for an XSLT transform or an XPath query."

    I would rather get this code working before trying yours. Also yours looks like it is in C#? Why do I only have options for vbscript and jscript? I would like to just stick with vbscript. It is so close to working. Does anyone know how to fix the code so it will work in my repeating table?

    Thanks,
    Kurt
  • 10-19-2006 11:24 PM In reply to

    Re: Extract Week Number from Date

    Sorry,

    can't help you with your question but you need to install the InfoPath 2003 Toolkit for Visual Studio .NET to be able to use managed code. There is actually a link in the left frame of this forum from where you can download the too.
    Listen to Motörhead cos they play Rock'n Roll :)
  • 10-23-2006 11:35 AM In reply to

    • kurtd
    • Not Ranked
    • Joined on 10-09-2006
    • USA
    • Posts 13

    Re: Extract Week Number from Date

    Well, I tried to install it but I don't have Visual Studio. I installed Visual Studio C# Express but the toolkit still wouldn't install. Maybe I will make a new post to try and get this code working on a repeating table.
Page 2 of 2 (20 items) < Previous 1 2
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.