如何区分fromhth与fromyear相比tomonth与toyear [英] How to Get Difference between frommonth with fromyear compared to tomonth with toyear

查看:133
本文介绍了如何区分fromhth与fromyear相比tomonth与toyear的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何区分frommonth与fromyear相比tomonth与toyear ....

例如,一个人有工作经验,从3月和2006年以及6月和2007年的持续时间应为1一年又三个月...也会有多种工作经验然后所有必须总结在一起......如果不到一年,我想在几个月内展示..

How to Get Difference between frommonth with fromyear compared to tomonth with toyear....
For instance one has work experience with frommonth March and fromyear 2006 and tomonth June and fromyear 2007 duration should be 1 year and 3 months...Also there would be multiple work experience then all must be summed together...I want to show in months if less than one year..

推荐答案

这是一个基本的逻辑问题,如果你只是把它写出来就很容易解决。



从你给出的例子中,我假设你是工作从月份开始到月末。即2013年1月至2013年1月为1个月; 2012年1月至2012年12月为1年; 2012年1月至2013年1月为1年1个月。



(请注意,对于此代码,如果您的月份是1或0-无关紧要基于)。



This is a basic logic problem, which is easy to work out if you simply write it out.

From the example you have given, I assume you are working from month start to month end. I.e. Jan 2013 to Jan 2013 would be 1 month; Jan 2012 to Dec 2012 would be 1 year; Jan 2012 to Jan 2013 would be 1 year 1 month.

(Note that for this code it doesn''t matter if your months are 1-based or 0-based).

int years = toYear - fromYear;
int months = toMonth - fromMonth + 1; // Add one, because we''re assuming beginning of "fromMonth" to end of "toMonth" - i.e. if same month and year given for from and to, then this is 1 month.

// If toMonth is earlier than fromMonth then we''ll have a negative number.
// Subtract a year and add 12 to the months.
// E.g. Dec 2012 to Jan 2013 = 1 month.  2013 - 2012 = 1; 1 - 12 (or 0 - 11) = -11 => 0 years, -11 + 12 = 1 month.
if (months < 0)
{
    --years;
    months += 12;
}
// Example tests:
// fromYear = 2006, fromMonth = Mar, toYear = 2007, toMonth June => 1 year 3 months.  (6 - 3 = 3; 2007 - 2006 = 1)
// fromYear = 2012, fromMonth = Mar, toYear = 2012, toMonth = Jun => 3 months. (6 - 3 = 3; 2012 - 2012 = 0)
// fromYear = 2012, fromMonth = Dec, toYear = 2013, toMonth = Jan => 1 month.  (1 - 12 + 1 = -11 => +12 = 1; 2013 -2012 - 1 = 0)
// fromYear = 2010, fromMonth = June, toYear = 2013, toMonth = Feb => 2 years 9 months. (2 - 6 + 1 = -3 => +12 = 3; 2013 - 2012 - 1 = 2)
// fromYear = 2010, fromMonth = Jan, toYear = 2013, toMonth = Jan => 3 years 1 month.  (1 - 1 + 1 = 1; 2013 - 2012 = 3)
// fromYear = 2010, fromMonth = Feb, toYear = 2013, toMonth = Jan => 3 years.  (1 - 2 + 1 = 0; 2013 - 2012 = 3)





之后你需要弄清楚你想要显示的方式这基于年和月。一个非常简单的例子(注意我没有在这里使用任何全球化等等,你可能想做一些事情,比如检查单数年和月等):





After that you need to work out how you want to display this based on "years" and "months". A very simple example (note I''ve not used any globalization here, etc, and you would probably want to do things like check for the singular "year" and "month", etc.):

if (years > 0)
{
    if (months > 0)
    {
        display = string.Format("{0} years {1} months", years, months);
    }
    else
    {
        display = string.Format("{0} years", years);
    }
}
else
{
    display = string.Format("{1} months", months);
}





问候,

Ian。



Regards,
Ian.


阅读 TimeSpan 。您需要找到时间和时间之间的差异。使用Timespan,您可以获得小时/分钟/秒等的差异。



参考:MSDN:TimeSpan Structure [ ^ ]
Read about TimeSpan. You need to find the difference between the to time and the from time. Using Timespan, you get the difference in terms of hours/minutes/seconds, etc.

Refer: MSDN: TimeSpan Structure[^]


这篇关于如何区分fromhth与fromyear相比tomonth与toyear的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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