Date calculation - InfoPath Dev
in

InfoPath Dev

Date calculation

Last post 07-04-2009 05:01 PM by Hilary Stoupa. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 07-04-2009 12:36 AM

    • batuqe
    • Not Ranked
    • Joined on 07-04-2009
    • Posts 1

    Date calculation

    i know only todays date and by using date i have to calculate first monday of septamber by using c# coding and c coding..

    plz try to help me out..

    Thank u in advance

  • 07-04-2009 06:34 AM In reply to

    Hi -- what version of InfoPath are you using? 2003 or 2007? They differ, so if you have to use code, it will help anyone who helps you to know that....

    For example, if you are using InfoPath 2007, you can use something like this:

    //create a variable for the first Monday
                DateTime monday;
               
                //get the value of today
                DateTime startDate = DateTime.Now;
     
                //get the first day in September using the year from that date
                DateTime septOne = new DateTime(startDate.Year, 9, 1);
     
                //is it a Monday? If so, we are done
                if (septOne.DayOfWeek == DayOfWeek.Monday)
                {
                    monday = septOne;
                }
                //otherwise we need to do a little date math
                else
                {
                    monday = septOne.AddDays(8 + (0 - septOne.DayOfWeek));
                }
     
                //set another field to the first Monday in Sept
                XPathNavigator nav = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:FirstMonday", NamespaceManager);
                nav.SetValue(monday.ToString());
    Hilary Stoupa

  • 07-04-2009 08:35 AM In reply to

    Hey Hilary,

     That code actually won't work when September 1st is a Sunday, as it will calculate the first Monday as September 9th.  The following is based off your code and should do the trick:

    Hilary Stoupa:

    //create a variable for the first Monday
                DateTime monday;
               
                //get the value of today
                DateTime startDate = DateTime.Now;
     
                //get the first day in September using the year from that date
                DateTime septOne = new DateTime(startDate.Year, 9, 1);
     
               monday = septOne.AddDays((8 - (int)septOne.DayOfWeek) % 7);
     
                //set another field to the first Monday in Sept
                XPathNavigator nav = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:FirstMonday", NamespaceManager);
                nav.SetValue(monday.ToString());

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 07-04-2009 05:01 PM In reply to

    Thanks, Jimmy!
    Hilary Stoupa

Page 1 of 1 (4 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.