get information based on dates using DateTime.Now.AddDays(-1).Day.ToString()

I have a c# web app that I am needing to generate where I use the following lines to determine 'yesterday' day

dayofmonth = DateTime.Now.AddDays(-1).Day.ToString();
if (Convert.ToInt32(dayofmonth) < 10)
{
      dayofmonth = dayofmonth.PadLeft(2,'0');
}
numberofmonth = DateTime.Now.Month.ToString();
if (Convert.ToInt32(numberofmonth) < 10)
{
      numberofmonth = numberofmonth.PadLeft(2,'0');
}

filename = "ex" + year.Substring(2,2) + numberofmonth.ToString() + dayofmonth.ToString();

this all works fine except, when I get to the date of Feb.1, the code above generates to
ex080231.log
but I dont want that to be....the generation of the 'day' is right, but the month should be 01 because the day month stil pertains to the first month.
How can I get the correct month when this code runs...
dayofmonth = DateTime.Now.AddDays(-1).Day.ToString();
because the day is working right to give me the 31, but the month code is not right...so how can I tell the month code to 'go back' one because the dayofmonth is still going back to Jan.31
lobosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Smacky311Commented:
Yesterday:
            DateTime dt = DateTime.Now;
            dt = dt.Subtract(TimeSpan.FromHours(24));

Open in new window

0
p_davisCommented:
cant you do the same with month?
DateTime.Now.AddMonths(-1).Month.ToString();
0
tempstf1Commented:
You do not have to go through all that, the toString() function of a datetime allows you to specify the format of the date.


filename = "ex" + DateTime.Now.AddDays(-1).ToString("yyMMdd")

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Smacky311Commented:
Combine what I said w/ what tempstf1 said.
            DateTime dt = DateTime.Now;
            dt = dt.Subtract(TimeSpan.FromHours(24));
            filename = dt.ToString("yyMMdd");

Open in new window

0
PlatoConsultantCommented:
u r not catering the Case of st day of month and first Month Of Year


i have added check for it

   private string  calcPreviousDate()
 
        {
 
            string dayofmonth, numberofmonth, filename ;
            string year = "2008";
 
            Boolean ChangeMonth = false;
            Boolean ChangeYear = false;
 
 
            if (DateTime.Now.Day == 1)
            {
                ChangeMonth = true;
            }
 
 
            dayofmonth = DateTime.Now.AddDays(-1).Day.ToString();
            
            
            if (Convert.ToInt32(dayofmonth) < 10)
            {
                dayofmonth = dayofmonth.PadLeft(2, '0');
            }
 
 
            if (DateTime.Now.Month == 1)
            {
                ChangeYear = true;
           
            }
            if (ChangeMonth)
            {
                numberofmonth = DateTime.Now.AddMonths(-1).Month.ToString();
            }
            else
            {
                numberofmonth = DateTime.Now.Month.ToString();
            }
 
            
            if (Convert.ToInt32(numberofmonth) < 10)
            {
                numberofmonth = numberofmonth.PadLeft(2, '0');
            }
 
 
            if (ChangeYear)
            { 
                // Year = year -1 
 
            }
            filename = "ex" + year.Substring(2, 2) + numberofmonth.ToString() + dayofmonth.ToString();
            return filename;
 
        }

Open in new window

0
lobosAuthor Commented:
p_davis: yes I could do that with the months, but I don't know when to run that code...meaning, its only suppose to run that code, when in fact I need to go back a month...but how is this done without hardcoding each month and how many days it has

tempstf1: thanks for your post, but that doesn't really answer my question

Smacky311: your second post is looking like it might do what I want...just could be troublesome depending on what time I run that code?  - just trying to think if using the timespan is the most effective way?

PlatoConsultant: - I have a big concern for the hardcoding of the year... - also dont quite understand what your doing? - inline comments?
0
p_davisCommented:
actually tempstf1's answer does answer your question.-- test it.

String str = dt.AddDays(-1).ToString("yyMMdd");  

if you have a date that is say 2/1/2008 and you run the above line (dt being the date mentioned) str will show 080131
0
Smacky311Commented:
I have validated the three lines of code i sent you by testing it in a console application.  It works today returning the proper date/time/year.  Your code and my code are dependant on what time you run the code due to the fact they use DateTime.Now().  If your not looking to go back 24 hours from the time the code is run then please clarify what it is your trying to do.
0
tempstf1Commented:
What is your question? From what I understand you want your filename to be ex080131.log right?  Thats what my solution will give.

DateTime.Now.AddDays(-1)  //This goes from now "Feb 1st 2008" to "Jan 31 2008"
DateTime.Now.AddDays(-1).toString("yyMMdd")  //This returns the string 080131

What else do you need that I'm not seeing?
0
lobosAuthor Commented:
This is exactly what I was doing except liek you said I wasn't using the .toString properly...I could get everything I needed in just this line here....the .ToString always seems to throw me off but very handy...
dayofmonth = DateTime.Now.AddDays(-1).Day.ToString();
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.