有没有办法在给定日期的情况下找回一年中的季节?对于地球上的任何地方?
这是基于时区还是半球?
请注意,在南半球,夏季仍处于温暖的月份.
编辑:
为了澄清,我说的是天文季节.
rio*_*fly 11
你可以使用这个简单的代码:
private int getSeason(DateTime date) {
float value = (float)date.Month + date.Day / 100; // <month>.<day(2 digit)>
if (value < 3.21 || value >= 12.22) return 3; // Winter
if (value < 6.21) return 0; // Spring
if (value < 9.23) return 1; // Summer
return 2; // Autumn
}
Run Code Online (Sandbox Code Playgroud)
为了包括南半球的季节,代码可以变成:
private int getSeason(DateTime date, bool ofSouthernHemisphere) {
int hemisphereConst = (ofSouthernHemisphere ? 2 : 0);
Func<int, int> getReturn = (northern) => {
return (northern + hemisphereConst) % 4;
};
float value = (float)date.Month + date.Day / 100f; // <month>.<day(2 digit)>
if (value < 3.21 || value >= 12.22) return getReturn(3); // 3: Winter
if (value < 6.21) return getReturn(0); // 0: Spring
if (value < 9.23) return getReturn(1); // 1: Summer
return getReturn(2); // 2: Autumn
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6054 次 |
| 最近记录: |