价值从月份到月份都没有设定为第3个月,但它显示为第1个月。需要快速回复的人 [英] The value didnt set for from month and to month it has to come as 3rd month but istead it shows as 1st month.need a quick reply guys

查看:53
本文介绍了价值从月份到月份都没有设定为第3个月,但它显示为第1个月。需要快速回复的人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int month = DateTime.Now.Month;
int year = DateTime.Now.Year;







eipyearmonthFrom.SelectedMonth = Convert.ToString(month - 1);
eipyearmonthFrom.SelectedYear = Convert.ToString(year);
eipyearmonthTo.SelectedMonth = Convert.ToString(month);
eipyearmonthTo.SelectedYear = Convert.ToString(year); ;

推荐答案

对于初学者,不要做那样的事情。即使这样有效,明年1月你会在eipyearmothFrom中获得什么?

此外,从不读过DateTime.Now不止一次 - 它可以在读数之间改变,所以你可以得到不同的日子,您的变量中的数月,甚至数年,如果您导致难以追踪数据库中的错误使用一个。



相反,请使用:

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:
DateTime now = DateTime.Now.Date;
DateTime startOfMonth = new DateTime(now.Year,now.Month,1);
DateTime lastMonth = startOfMonth.AddMonths(-1);



现在你可以在eipyearmonthFrom的任何控制中使用日期。


Now you can use the date in whatever control eipyearmonthFrom is.


这段代码向你显示你想要的东西DateTo和Date From MonthTo和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()+\\\
Month To:+ MonthTo +\ nMonth From:+ MonthFrom);



i在消息框中显示了这一点你可以采取任何你想要的控制,如label1.text = MonthTo.toString ();



----或者-----

如果你想让它出现在你的日历中而不是这个..



DateT ime GetMonthTo = DateTime.Now.AddMonths(1);

monthCalendar1.SelectionStart = GetMonthTo;



以上代码将做的是添加1个月到您当前的日期并将其设置为您的日历

例如

日期自2015年4月18日// 4月是

日期至= 5-18-2015 // 4 + 1 = 5
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


这篇关于价值从月份到月份都没有设定为第3个月,但它显示为第1个月。需要快速回复的人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆