Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 6th, 2005, 02:34 PM
Authorized User
Points: 457, Level: 7
Points: 457, Level: 7 Points: 457, Level: 7 Points: 457, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2004
Location: Burbank, CA, USA.
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Arsi
Default Can't subtract days from a date Please Help

Hello,

I'm trying to get the week ending and month ending dates of approximately 4 weeks past today's date. I have written some code that gets the date for last sunday, but does not go past that. If anyone can help me out I would appreciate it. Here is the code:

           TimeSpan oneDay = new TimeSpan(1,0,0);
            Calendar calendar = info.Calendar;
            DateTime dtNow = DateTime.Today;
            dt = new DateTime(dtNow.Year, dtNow.Month, dtNow.Day, calendar);
                        int count = 0;
             dt.Month);
                        int x, y;
            for (x = 0; x <= 3; x++)
            {
                while(!dt.DayOfWeek.Equals(DayOfWeek.Sunday) && (dt.Day != eom))
                {
                    dt = dt.Subtract(oneDay);
                    eom = DateTime.DaysInMonth(dt.Year, dt.Month);
                }
                endingDatesList += dt.ToShortDateString() + ", ";
                for (y = 0; y <= 25; y++)
                {
                    dt = dt.Subtract(oneDay);

                }
            }

Thanks!

*******(*)*******
__________________
*******(*)*******
Reply With Quote
  #2 (permalink)  
Old January 6th, 2005, 03:34 PM
Authorized User
Points: 457, Level: 7
Points: 457, Level: 7 Points: 457, Level: 7 Points: 457, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2004
Location: Burbank, CA, USA.
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Arsi
Default

Ok. I think I've got it now. I changed the code because someone here helped me see what could be done from a different angle and I put code behind it and it worked. You tend to get tunnel vision sometimes and need a kick in a different direction to help.

TimeSpan oneDay = new TimeSpan(1,0,0);
            Calendar calendar = info.Calendar;
            DateTime dtNow = DateTime.Today;
            if(!dtNow.Month.Equals(1))
            {
                dt = new DateTime(dtNow.Year, dtNow.Month - 1, dtNow.Day - dtNow.Day + 1, calendar);
            }
            else
            {
                dt = new DateTime(dtNow.Year - 1, dtNow.Month + 11, dtNow.Day - dtNow.Day + 1, calendar);
            }
            int count = 0;
            int eom = DateTime.DaysInMonth(dt.Year, dt.Month);
            while(count < 15)
            {
                // insert to endingDatesList when date is Sunday or End of Month
                while(!dt.DayOfWeek.Equals(DayOfWeek.Sunday) && (dt.Day != eom))
                {
                    // add one day to the date.
                    dt = dt.AddDays(1);
                    eom = DateTime.DaysInMonth(dt.Year, dt.Month);
                }
                endingDatesList += dt.ToShortDateString() + ", ";
                count++;
                dt = dt.AddDays(1);
            }

Thanks anyway. Hope this helps someone else too.

*******(*)*******
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
date conversion in years, months and days naveed77 Beginning VB 6 0 January 2nd, 2008 01:18 AM
Subtract date stealthdevil Access VBA 2 December 7th, 2007 03:58 PM
how to convert date to days teamml SQL Server 2000 3 May 8th, 2007 07:05 AM
Todays date and date of 3 days later Singh591 Visual Basic 2005 Basics 1 December 21st, 2006 06:06 PM
How to calculate a number of days between 2 date windy417 Pro JSP 1 July 20th, 2006 07:09 PM



All times are GMT -4. The time now is 08:02 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.