2013-05-08 137 views
0

我有一个字段返回年份和月份值。例如,20119(2011年是9年,9月是9月)。我怎么能比较这与当前的一年和一个月来获得几个月的差异?例如,在同一个格式的当前年份和月份是20135,所以我要寻找的值是20。20135零下20个月将是20119.不知道如何构建公式动态计算以月为单位的区别也许使用日期函数。以月计算日期差异

+1

你尝试过这么远吗?如果你还没有尝试过任何东西,看看DateTime对象的[AddMonths(http://msdn.microsoft.com/en-us/library/system.datetime.addmonths.aspx)方法。 – 2013-05-08 01:42:52

回答

5

试试这个

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)); 
+0

格式应该是“yyyyM”而不是“yyyym”(几个月而不是几分钟)?当我尝试这给了我x1 = 01/01/2011 12:09:00 AM – Greg 2013-05-08 01:57:11

+0

@Greg,是的,它应该是M – 2013-05-08 01:57:51

+0

感谢rs。有没有简单的方法将当前日期转换为“20135”格式?而不必将当前日期的年份部分与当前日期的月份部分连接起来。 – obautista 2013-05-08 02:07:47

1

为什么不将年份乘以一年中每个日期字段的月份数然后返回差值?

+0

不知道我是否理解你的权利。你是说:date1 = 20139和date2 = 20138(1个月差异)。 2013 * 9-2013 * 8 = 2013。 – Greg 2013-05-08 01:54:09

+0

(2013 * 12 + 9) - (2013 * 12 + 8) – 2013-05-08 01:58:06

+0

啊 - 这样更有意义。非常聪明。 – Greg 2013-05-08 02:01:25

0

这是从溶液中的代码片段贴在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

+0

我不认为你可以从时间上直接得到几个月 – Greg 2013-05-08 01:50:59

+0

@Greg:谢谢你的意见。我已经为这个特殊情况添加了关于TimeSpan对象使用情况的说明。 – 2013-05-08 02:12:12

0

你基本上可以拆分字符串。

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; 
1

首先我假设你的问题:

  • 单日个月内将有个位数
  • 年+月的值是一个字符串(如果它是一个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公式。

1

您可以使用类则DateDiffTime 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