如何编程减去两个日期函数并以dd / mm / yy得到输出? [英] How to program to subtract two date functions and get the output in dd/mm/yy?

查看:73
本文介绍了如何编程减去两个日期函数并以dd / mm / yy得到输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两个日期功能如

2016年4月26日 - 31/08 / 1998 =年/月/年YYYY。

请有人编程。

我尝试了几种逻辑但没有成功。



我尝试了什么:



我尝试过多次逻辑但没有成功

Two date function like
26/04/2016 - 31/08/1998= dd/mm/YYYY.
Please anyone program it.
I have tried several logics but didn't succeed.

What I have tried:

I have tried several logics but didn't succeed

推荐答案

首先你需要解析输入字符串以获得数字日期可以减去的价值。使用哪种解析函数取决于操作系统和可选的框架。



使用Linux,您可以使用 strptime(3) - Linux man页面 [ ^ ]并转换结果 struct tm time_t 使用 mktime(3) - Linux手册页 [ ^ ]。那些 time_t 值可以减去并转换回其他日期类型,最后格式化为字符串。



随着在Windows和MFC应用程序中,您可以使用 COleDateTime类 [ ^ ]提供 ParseDateTime function,减法运算符(还有一个版本返回 COleDateTimeSpan类 [ ^ ])和格式化函数。



另请注意,减去两个日期会产生一个时间跨度,通常不会格式化为dd / mm / yy,而是天数,月数或年数(或n年和m天之类的组合)。



因此,在您没有告诉我们使用的操作系统和框架时提供代码是没有意义的,以及如何显示结果。
At first you have to parse the input strings to get a numeric date value that can be subtracted. Which parsing function to use depends on the operating system and the optionally used framework.

With Linux you might use strptime(3) - Linux man page[^] and convert the resulting struct tm to time_t using mktime(3) - Linux man page[^]. Those time_t values can then be subtracted and converted back to other date types and finally formatted as string.

With Windows and MFC applications, you can use the COleDateTime Class[^] which provides the ParseDateTime function, the subtraction operator (there is also a version returning a COleDateTimeSpan Class[^] ), and format functions.

Note also that subtracting two dates results in a time span which is usually not formatted as "dd/mm/yy" but as number of days, months, or years (or a combination like "n years and m days").

So it makes no sense to provide code while you did not tell us about the used OS and framework, and how to display the result.


这是一个适合您的解决方案: c ++ - 确定它们之间的区别日期 - 堆栈溢出 [ ^ ]
Here is a solution for you: c++ - Determining the difference between dates - Stack Overflow[^]


你不能减去两个日期并得到一个日期:这在逻辑上是不可能的。

当你减去两个日期时,你会得到一个时间跨度 - 几年,几个月,或者它们之间的天数。

这个天数可能代表什么 - 例如你的年龄,但它本身不是一个日期,它不能转换为没有日期引用另一个日期。



想想你想要做什么,并确切地确定你需要什么 - 这不会起作用。
You can't subtract two dates and get a date: it isn't logically possible.
When you subtract two dates, you get a time span - a number of years, months, or days between them.
That number of days may represent something - your age for example but it isn't of itself a date, and it can't be converted to a date without being referenced to another date.

Think about what you are trying to do, and decide exactly what you need - this is not going to work.


这篇关于如何编程减去两个日期函数并以dd / mm / yy得到输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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