如何从整数中提取日,月和年(遵循1900日期系统) [英] How to extract day, month and year from integer (following 1900 date system)

查看:314
本文介绍了如何从整数中提取日,月和年(遵循1900日期系统)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经给出了一个任务,我以整数格式给出数据日期,例如

I have given a task in which I am given data dates in integer format e.g

39083 (= 1/1/2007)



I需要检查同月的日期以查找月平均值。我只能使用


I need to check for dates of same month to find monthly average. I have to only solve it using

cmath.h

函数解决它。其他不允许与日期/时间相关的内置功能。我正在使用视觉工作室。



我尝试过:



我无法想到它的逻辑。

functions. other built in functions related to date/time are not allowed. i am using visual studio.

What I have tried:

I am unable to think a logic for it.

推荐答案

假设整数值是自191年1月1日以来的天数,那么你必须将其转换为一个约会。

所以每个闰年有366天,每个非闰年有365个。闰年是那些在1900到2099年范围内被4整除的年份,所以我创建了一个年份数

Assuming that the integer value is "days since 1/1/1900", then you have to convert that to a date.
So each leap year has 366 days, and each non-leap year has 365. Leap years are all those years divisible by 4 in the range 1900 to 2099, so I'd create an array of "year counts"
0, 365, 365+365, 366+365+365, 365+365+365+366 ...

(因为1900年不是闰年 - 世纪结束修正 - 但2000年是 - 千禧年结束修正)。

然后我用它来检查年份数但减去数组中的每个值日期代码:它是减去正数或零的最后一年。

减去的数值是一年中的日期数,所以只需对月份进行相同的处理(允许跳跃)年,两个累积月数组是最简单的方法)

离开的值是月中的某一天。

(Because 1900 was not a leap year - century end correction - but 2000 was - millenium end correction).
I'd then use that to check on the year number but subtracting each value in the array from the date code: it's the last year with a positive or zero subtraction.
The value you get as the subtraction is the day number in the year, so just do the same with the months (allowing for leap years, two "cumulative months" arrays is the easiest way)
The value that leaves is the day of the month.


Quote:

如何从整数中提取日,月和年(在1900日期系统之后)

How to extract day, month and year from integer (following 1900 date system)



你没有提取,计算或转换。


You don't extract, you calculate or convert.

39083 (= 1/1/2007)



这是寻找就像Excel使用的编码一样。

这种日期编码只是计算参考日期和你想要的日期之间的天数。


This is looking like the encoding used by Excel.
This kind of date coding is simply counting the number of days between a reference date and the date you want.

引用:

我无法想到它的逻辑。

I am unable to think a logic for it.



一年中的天数已知,闰年的规则是已知的,一个月的天数是已知的。

你刚才适用规则。

用一张纸和一支铅笔训练自己,手工解决样本日期,你使用的方法是你的算法。



HomeWork问题是你在现实生活中必须解决的问题的简化版本,他们的目的是学习和练习



我们不做你的家庭作业。

HomeWork不会在乞求别人做你的工作时测试你的技能,它会让你思考和帮助您的老师检查您对所学课程的理解,以及您应用这些课程时遇到的问题。

你的任何失败都会帮助你的老师发现你的弱点并设定补救措施。

你的任何失败都会帮助你了解什么有效,什么无效,被称为'试错'学习。

所以,试一试,重读课程并开始工作。如果您遇到特定问题,请显示您的代码并解释这个问题,我们可能会提供帮助。



作为程序员,您的工作是创建算法解决特定问题,你不能依赖别人永远为你做,所以有一段时间你必须学会​​如何。而且越快越好。

当你要求解决方案时,就像试图通过培训其他人来学习开车一样。

创建算法基本上是找到数学并做出必要的调整以适应你的实际问题。


The number of days in a year is known, the rules for leap year are known, the number of days in a month are known.
you just have apply the rules.
Train yourself with a sheet of paper and a pencil, solve sample dates by hand, the method you use is your algorithm.

HomeWork problems are simplified versions of the kind of problems you will have to solve in real life, their purpose is learning and practicing.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.


这篇关于如何从整数中提取日,月和年(遵循1900日期系统)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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