Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
int month = DateTime.Now.Month;
int year = DateTime.Now.Year;


SQL
eipyearmonthFrom.SelectedMonth = Convert.ToString(month - 1);
eipyearmonthFrom.SelectedYear = Convert.ToString(year);
eipyearmonthTo.SelectedMonth = Convert.ToString(month);
eipyearmonthTo.SelectedYear = Convert.ToString(year); ;
Posted
Updated 17-Apr-15 21:06pm
v3
Comments
Mario Z 18-Apr-15 3:10am    
What is eipyearmonthFrom? Also what is your issue, I'm sorry but you have explained it very poorly.
Try improving your question so that your issue can be understood instead of decipher.
Richard MacCutchan 18-Apr-15 3:29am    
You are using month - 1, so what do you expect it to be? And what happens if the current month is 1?
manoswc 18-Apr-15 4:39am    
what I want is in default the month control for from has to display current month-1 and in to i want the current month, but it is displaying jan in both from an to controls

For starters, don't do things like that. Even if this worked, what would you get in eipyearmothFrom in January next year?
In addition, never read the DateTime.Now more than once - it can change between readings, so you can get different days, months, and even years in your month and year variables which leads to difficult to track down errors in your DB if you use one.

Instead, use this:
C#
DateTime now = DateTime.Now.Date;
DateTime startOfMonth = new DateTime(now.Year,now.Month,1);
DateTime lastMonth = startOfMonth.AddMonths(-1);

Now you can use the date in whatever control eipyearmonthFrom is.
 
Share this answer
 
this code is showing you both the things that you want DateTo and Date From MonthTo and MonthFrom

DateTime DateTo = DateTime.Now;
DateTime DateFrom = monthCalendar1.SelectionStart.AddMonths(-1);
int MonthTo = DateTime.Now.Month;
int MonthFrom = MonthTo - 1;

MessageBox.Show("Date To: " + DateTo.ToShortDateString() +" \nDateFrom: " + DateFrom.ToShortDateString() + "\nMonth To: " + MonthTo + "\nMonth From: " + MonthFrom);

i showed this up in message box you can take any control you want like label1.text = MonthTo.toString();

---- OR -----
if you want this to show up in your calender than do this..

DateTime GetMonthTo = DateTime.Now.AddMonths(1);
monthCalendar1.SelectionStart = GetMonthTo;

what the above code will do is add 1 month to your current date and set it to your calender
e.g
Date From = 4-18-2015 //4 is month
Date To = 5-18-2015 // 4 + 1 = 5
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900