我有一个字段返回年份和月份值。例如,20119(2011年是9年,9月是9月)。我怎么能比较这与当前的一年和一个月来获得几个月的差异?例如,在同一个格式的当前年份和月份是20135,所以我要寻找的值是20。20135零下20个月将是20119.不知道如何构建公式动态计算以月为单位的区别也许使用日期函数。以月计算日期差异
以月计算日期差异
回答
试试这个
DateTime x1 = DateTime.ParseExact("20119", "yyyyM", CultureInfo.InvariantCulture);
DateTime x2 = DateTime.ParseExact("20135", "yyyyM", CultureInfo.InvariantCulture);
int months = Math.Abs((x2.Month - x1.Month) + 12 * (x2.Year - x1.Year));
这是从溶液中的代码片段贴在MSDN(link):
DateTime oldDate = new DateTime(2002,7,15);
DateTime newDate = DateTime.Now;
// Difference in days, hours, and minutes.
TimeSpan ts = newDate - oldDate;
// Difference in days.
int differenceInDays = ts.Days;
应年/月工作,以及(类似如下):
int differenceInMonths = (ts.Years *12 + ts.Months);
希望这会有所帮助。 Rgds,AB
我不认为你可以从时间上直接得到几个月 – Greg 2013-05-08 01:50:59
@Greg:谢谢你的意见。我已经为这个特殊情况添加了关于TimeSpan对象使用情况的说明。 – 2013-05-08 02:12:12
你基本上可以拆分字符串。
int a = 201410;
int b= 20139;
int year1 = int.Parse(a.ToString().Substring(0,4));
int year2 = int.Parse(b.ToString().Substring(0,4));
int month1 = int.Parse(a.ToString().Substring(4));
int month2 = int.Parse(b.ToString().Substring(4));
//now construct a date for each
DateTime date1 = new DateTime(year1, month1, 1);
DateTime date2 = new DateTime(year2, month2, 1);
//then subtract them and make it months
int numberOfMonths = ((date1.Year - date2.Year) * 12) + date1.Month - date2.Month;
首先我假设你的问题:
- 单日个月内将有个位数
- 年+月的值是一个字符串(如果它是一个int,抛一个ToString()在下面代码中的值)
因此,您的值将是5-6位数字的长度。通过使用获得Date.Now
我们可以得到当前的日期只能作为当月同比:我会添加额外的代码,使这个更加清晰 - 你可以在更短的行执行下面的代码,但原谅我冗长的答案
// Just want the month/year
DateTime currentDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
现在我们可以为你要测试的使用方法子本年度/月(记住我的假设,我们正在处理一个字符串值,并且转换的ToString()如果不是)日期。
// breaking out test date to year/month portions and saving as a new date time
string testDateValue = "20119";
int testDateYear = Convert.ToInt32(testDateValue.Substring(0, 4));
int testDateMonth = Convert.ToInt32(testDateValue.Substring(4));
DateTime testDate = new DateTime(testDateYear, testDateMonth, 1);
现在让我们得到的区别:
// get month dif - remove abs() if want negative if test date in future
int numberOfMonths = Math.Abs(((currentDate.Year - testDate.Year) * 12) +
(currentDate.Month - testDate.Month));
现在 - 如果你想2天比较在yyyym格式,而不是使用当前的日期,只是年/月转换上面再上市对此执行month dif公式。
您可以使用类则DateDiff从Time Period Library for .NET类,计算个月:
// ----------------------------------------------------------------------
public void CalcMonths(DateTime epoch)
{
DateDiff dateDiff = new DateDiff(DateTime.Now, epoch);
Console.WriteLine("{0} months", dateDiff.Months);
// > 1 Year 4 Months 12 Days 12 Hours ago
} // CalcMonths
- 1. 差异日期计算
- 2. 计算日期差异
- 3. 计算日期差异XSL
- 4. 两个日期之间的月份差异计算lastdate月
- 5. 以月为单位的日期差异
- 6. 使用Groovy计算两个日期之间的月差异
- 7. 带月份差异的JavaScript日期计算
- 8. 计算一个月份的日期和日期之间的PHP差异
- 9. php日期差异和整数计算
- 10. 计算时间和日期差异
- 11. 计算日期差异在Postgresql中
- 12. 计算日期之间的差异 - Postgres
- 13. 计算R中组的日期差异
- 14. C计算日期之间的差异
- 15. 日期计算差异(JavaScript VS .NET)
- 16. 日期计算的差异Java SimpleDateFormat
- 17. 计算和显示日期的差异
- 18. Java中的日期差异计算
- 19. 在jQuery中计算日期差异
- 20. 计算java时出错日期差异
- 21. 计算循环中的日期差异
- 22. VB 2010:如何计算日期差异?
- 23. 计算POSIXct列的日期差异(BUG?)
- 24. 计算Java中的日期差异
- 25. 预计月份和日期的日期差异?
- 26. 日期年月计算
- 27. 如何计算Javascript中两个日期之间的年,月和日差异?
- 28. Perl:计算日期差异和平均日期
- 29. jQuery的日期选择器+日期差异计算
- 30. 计算日期和当前日期之间的差异
你尝试过这么远吗?如果你还没有尝试过任何东西,看看DateTime对象的[AddMonths(http://msdn.microsoft.com/en-us/library/system.datetime.addmonths.aspx)方法。 – 2013-05-08 01:42:52