Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 6th, 2005, 02:34 PM
Authorized User
 
Join Date: Aug 2004
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!

*******(*)*******
__________________
*******(*)*******
 
Old January 6th, 2005, 03:34 PM
Authorized User
 
Join Date: Aug 2004
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.

*******(*)*******





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.