如何在c#中找出一个季度和另一个之间有多少个季度
我创建了一个名为 Kwartal(翻译为 Quarter)的类,用于我的程序:
public sealed class Kwartal {
private DateTime _poczatekKwartalu;
private DateTime _koniecKwartalu;
private int _numerKwartalu;
private int _rok;
public Kwartal(int numer, DateTime dataod, DateTime datado) {
_numerKwartalu = numer;
_koniecKwartalu = datado;
_poczatekKwartalu = dataod;
_rok = dataod.Year;
}
public Kwartal() { }
public int Numer {
get { return _numerKwartalu; }
set { _numerKwartalu = value; }
}
public DateTime DataPoczatkowa {
get { return _poczatekKwartalu; }
set { _poczatekKwartalu = value; }
}
public DateTime DataKoncowa {
get { return _koniecKwartalu; }
set { _koniecKwartalu = value; }
}
public int Rok {
get { return _rok; }
set { _rok = value; }
}
}
它基本上是 Quarter 的定义。通常我这样定义它:
Kwartal kwartal1 = new Kwartal(1, new DateTime(year, 1, 1), new DateTime(year, 3, 31));
Kwartal kwartal2 = new Kwartal(2, new DateTime(year, 4, 1), new DateTime(year, 6, 30));
现在我想知道如何对这些进行数学计算。例如,我有 2011 年的第 1 季度,然后有 2012 年的第 3 季度。我想知道第 1 季度和第 1 季度之间有多少个季度 第三季度。
就像kwartal2 - kwartal1 = 5
I've created a class called Kwartal (which translates into Quarter) for use in my program:
public sealed class Kwartal {
private DateTime _poczatekKwartalu;
private DateTime _koniecKwartalu;
private int _numerKwartalu;
private int _rok;
public Kwartal(int numer, DateTime dataod, DateTime datado) {
_numerKwartalu = numer;
_koniecKwartalu = datado;
_poczatekKwartalu = dataod;
_rok = dataod.Year;
}
public Kwartal() { }
public int Numer {
get { return _numerKwartalu; }
set { _numerKwartalu = value; }
}
public DateTime DataPoczatkowa {
get { return _poczatekKwartalu; }
set { _poczatekKwartalu = value; }
}
public DateTime DataKoncowa {
get { return _koniecKwartalu; }
set { _koniecKwartalu = value; }
}
public int Rok {
get { return _rok; }
set { _rok = value; }
}
}
It's basically definition for Quarter. Usually i define it like this:
Kwartal kwartal1 = new Kwartal(1, new DateTime(year, 1, 1), new DateTime(year, 3, 31));
Kwartal kwartal2 = new Kwartal(2, new DateTime(year, 4, 1), new DateTime(year, 6, 30));
Now i was wondering how I can do math on those. For example I've got Quarter1 in 2011 and i then have Quarter3 in 2012. I would like to find out how many quarters are there between Quarter1 and
Quarter3.
Like kwartal2 - kwartal1 = 5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您无法在任何地方定义一年中的季度数 - 如果
Kwartal
对象上没有设置某些属性/常量,您如何知道减法运算的基值应该是多少?一旦您设置了该基值,操作就会相当简单,您可以创建绝对季度计数,例如
,您可以从另一年中减去一个整数。
Nowhere do you define the number of quarters in the year - without there being some property/constant set on the
Kwartal
object how can you know what the base value should be for your subtraction operation?Once you've set that base value, the operation would be reasonably easy, you can create an absolute quarter count, e.g.
then you have an integer to substract from the other year.
根据您的回答,这就是我要做的:
我认为这会给您以下答案:
(Year1, Quarter1) - (Year2, Quarter2) = Difference
(2012, 2) - (2011, 1) = (2011 - 2012 )*4 + (1 - 2) = -4 + (-1) = -5 => (2011, 1) 比 (2012, 2) 早 5 个季度 (
2014, 1) - (2018,3) = (2018 - 2014)*4 + (3 - 1) = 16 + 2 = 18 => (2018,3) 是 (2014,1) 后 18 个月
From your answer, this is what I would do:
I think this would give you the following answers:
(Year1, Quarter1) - (Year2, Quarter2) = Difference
(2012, 2) - (2011, 1) = (2011 - 2012)*4 + (1 - 2) = -4 + (-1) = -5 => (2011, 1) is 5 quarters before (2012, 2)
(2014, 1) - (2018,3) = (2018 - 2014)*4 + (3 - 1) = 16 + 2 = 18 => (2018,3) is 18 months after (2014,1)
您可以使用 .NET 时间段库的 DateDiff 类,尊重日历文化:
You can use the DateDiff class of the Time Period Library for .NET, respecting the calendar culture:
首先,您应该定义计算的结果应该是什么。创建一个包含一些数据和大量边缘情况的矩阵/表,并手动计算结果应该是什么。
然后,您可以找出用于计算它的算法。并基本实现了这个算法。并测试您编写的数据。
哦,然后你开始考虑时区、夏令时等。你应该阅读此博客条目来自 Jon Skeet。
First, you should define what the result of the calculation should be. Create a matrix/table with some data, and a lot of edge cases, and calculate by hand what the result should be.
Then, you could figure out an algorithm to use to calculate it. And basicly implement this algorithm. And test on your data you made up.
Oh, and then you start thinking about time-zones, daylight saving, etc. And you should read this blog entry from Jon Skeet.
我不确定这是否是您想要实现的结果
This returns 4 .. 就像您所期望的那样
I am not sure whether this is what you want to achieve
This returns 4 .. something like what you expect
如果将年份/季度转换为十进制,并进行一些数学运算,就可以计算出差异。
If you convert the year / quarters to decimal, and do a little math, you can calculate the difference.
这就是我最终这样做的方式。似乎工作正常。如果你有更好的方法让我知道:-)
This is how i ended up doing this. Seems to be working fine. If you've got a better way lemme know :-)